Advertisement
naeem043

C Harmonic & Fibo for Sweden Nasir

Nov 13th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.04 KB | None | 0 0
  1. #include <stdio.h>
  2. double harmonic(int n)
  3. {
  4.     double i, s = 0.0;
  5.     for (i = 1; i <= n; i++){
  6.         s = s + 1/i;
  7.     }
  8.     return s;
  9. }
  10.  
  11. int harmonic_exceed(float n)
  12. {
  13.     int i = 1;
  14.     float sum;
  15.     sum = harmonic(1);
  16.     while(sum <= n){
  17.         i++;
  18.         sum = harmonic(i);
  19.         //sum = (int)sum;
  20.     }
  21.     printf("\n%d terms needed to reach %.2f",i, n);
  22.     return 0;
  23. }
  24.  
  25. int fibonacci()
  26. {
  27.     int i, n, f1 = 0, f2 = 1, temp;
  28.     printf("Enter the value of n for nth fibonacci series: ");
  29.     scanf("%d", &n);
  30.     printf("Fibonacci Series: ");
  31.     for (i = 1; i <= n; ++i)
  32.     {
  33.         printf("%d, ", f1);
  34.         temp = f1 + f2;
  35.         f1 = f2;
  36.         f2 = temp;
  37.     }
  38.     return 0;
  39. }
  40. int main()
  41. {
  42.     int n, i;
  43.     float x;
  44.     printf("Give an integer: ");
  45.     scanf("%d", &n);
  46.     for(i = 1; i <= n; i++)
  47.     {
  48.         printf("%d  %f\n",i, harmonic(i));
  49.     }
  50.     fibonacci();
  51.     while(1){
  52.         printf("\nEnter the value of x terms: ");
  53.         scanf("%f", &x);
  54.         harmonic_exceed(x);
  55.     }
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement