Sum of Harmonic Series

Problem Statement
Write  a c program to calculate sum of a harmonic series using recursion
you should take the input from the user

#include<stdio.h> float harsum(float start,float comm_dif,float end); int main() { float y,start,end,comm_dif; printf("enter starting point \n"); scanf("%f",&start); printf("enter common difference \n"); scanf("%f",&comm_dif); printf("enter the value of n upto which the harmonic series is to be calculaed"); scanf("%f",&end); y= harsum(start,comm_dif,end); printf("the sum is %f \n",y); return 0; } float harsum( float start,float comm_dif,float end) { float sum; if(start>end) { return 0; } else { sum= 1/start+ harsum(start+comm_dif,comm_dif,end); } return sum; }

No comments:

Post a Comment