Algorithm
Problem Name: beecrowd | 1155
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1155
S Sequence
Adapted by Neilor Tonin, URI Brazil
Timelimit: 1
Write an algorithm to calculate and write the value of S, S being given by:
S = 1 + 1/2 + 1/3 + … + 1/100
Input
There is no input in this problem.
Output
The output contains a value corresponding to the value of S.
The value should be printed with two digits after the decimal point.
Input Sample | Output Sample |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
#define FIXED_FLOAT(x) std::fixed << std::setprecision(2) << (x)
using namespace std;
int main(){
float s = 1;
for (int i = 2; i < = 100; i++){
s += (1.0 / i);
}
cout << FIXED_FLOAT(s) << endl;
return 0;
}
Copy The Code &
Try With Live Editor
#2 Code Example with Java Programming
Code -
Java Programming
public class Main{
public static void main(String[] args){
float s = 1;
for (int i = 2; i < = 100; i++){
s += (1.0 / i);
}
System.out.printf("%.2f\n", s);
}
}
Copy The Code &
Try With Live Editor
#3 Code Example with Javascript Programming
Code -
Javascript Programming
var s = 1.0;
for (var i = 2; i < = 100; i++){
s += (1.0 / i);
}
console.log(s.toFixed(2));
Copy The Code &
Try With Live Editor
#4 Code Example with Python Programming
Code -
Python Programming
s = 1.0
for i in range(2, 101):
s += (1 / i)
print("{:.2f}".format(s))
Copy The Code &
Try With Live Editor