URI Online Judge |
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
S = 1 + 1/2 + 1/3 + … + 1/100
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.
The value should be printed with two digits after the decimal point.
Solution in Java:
package URI_Problems_solution;
public class URI_1155 {
public static void
main(String[] args) {
//S = 1 + 1/2 + 1/3 + … + 1/100
float S = 0;
for (float i = 1; i <= 100; i++) {
S += (1 / i);
}
System.out.printf("%.2f\n",S);
}
}
Solution in C:
- #include <stdio.h>
- int main()
- {
- double d = 0, i;
- for (i = 1; i <= 100; ++i)
- d += 1/i;
- printf("%.2lf\n", d);
- return 0;
- }
Solution in C++:
- #include <cstdio>
- using namespace std;
- int main()
- {
- double d = 0, i;
- for (i = 1; i <= 100; ++i)
- d += 1/i;
- printf("%.2lf\n", d);
- return 0;
- }
No comments:
Write commentsTo know more about the problem, give us your valuable commment. We'll try to help you. Thanks