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.
Solution using Python:
S = 0
for i in range(1,101):
S = S + 1/i
print("%.2f"%S)
S = 0
for i in range(1,101):
S = S + 1/i
print("%.2f"%S)
Solution using C :
#include <stdio.h>int main() { int i; float S=0; for(i=1; i<=100; i++) { S += (float)1/(float)i; } printf("%.2f\n",S); return 0;}
Comments
Post a Comment