Saturday, April 16, 2016

URI Online Judge Solution 1155 in Java, C, C++


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


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 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:



  1. #include <stdio.h>
  2. int main()
  3. {
  4. double d = 0, i;
  5. for (i = 1; i <= 100; ++i)
  6. d += 1/i;
  7. printf("%.2lf\n", d);
  8. return 0;
  9. }

Solution in C++:


  1. #include <cstdio>
  2. using namespace std;

  3. int main()
  4. {
  5. double d = 0, i;
  6. for (i = 1; i <= 100; ++i)
  7. d += 1/i;
  8. printf("%.2lf\n", d);
  9. return 0;
  10. }




No comments:
Write comments

To know more about the problem, give us your valuable commment. We'll try to help you. Thanks

All rights reserved ©2016 -URI ONLINE JUDGE SOLUTION | Developed by Maniruzzaman Akash

© 2016 URI ONLINE JUDGE SOLUTION. Developed by Maniruzzaman Akash | Distributed By Gooyaabi Templates
Powered by Blogger.