Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- double harmonic(int n)
- {
- double i, s = 0.0;
- for (i = 1; i <= n; i++){
- s = s + 1/i;
- }
- return s;
- }
- int harmonic_exceed(float n)
- {
- int i = 1;
- float sum;
- sum = harmonic(1);
- while(sum <= n){
- i++;
- sum = harmonic(i);
- //sum = (int)sum;
- }
- printf("\n%d terms needed to reach %.2f",i, n);
- return 0;
- }
- int fibonacci()
- {
- int i, n, f1 = 0, f2 = 1, temp;
- printf("Enter the value of n for nth fibonacci series: ");
- scanf("%d", &n);
- printf("Fibonacci Series: ");
- for (i = 1; i <= n; ++i)
- {
- printf("%d, ", f1);
- temp = f1 + f2;
- f1 = f2;
- f2 = temp;
- }
- return 0;
- }
- int main()
- {
- int n, i;
- float x;
- printf("Give an integer: ");
- scanf("%d", &n);
- for(i = 1; i <= n; i++)
- {
- printf("%d %f\n",i, harmonic(i));
- }
- fibonacci();
- while(1){
- printf("\nEnter the value of x terms: ");
- scanf("%f", &x);
- harmonic_exceed(x);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement