Program to find sum of the series 1/1 + 1/2 + 1/3 +……….+ 1/n. This series also known as harmonic series. It is the inverse of a arithmetic progression. The terms in this series can be signified as 1/a, 1/(a + d), 1/(a + 2d), 1/(a + 3d), ….., 1/(a + nd).
What is the nth harmonic number?
As Nth term of AP is given as (a+(n–1)d). Consequently, Nth term of the series is complementary of Nth term of AP, which is 1/(a+(n–1)d), where "a" is the first term of AP and "d" is a constant difference(common difference).
How do you find the sum of a harmonic series?
- Take value of N from user. Where N is number of terms in the series.
- Use loop to display every terms and add them into sum variable. Where sum have initial value 0.
- Loop range will be from 1 to N. And sum variable must be float datatype or decimal point datatype.
- Display the sum of the series as result.
Program to find sum of the harmonic series.
- C program to find sum of series.
- C++ program to find sum of series.
- Java program to find sum of series.
Recursive program to find the sum of the harmonic series.
- C program to find sum of series using recursion.
- C++ program to find sum of series using recursion.
- Java program to find sum of series using recursion.
C program to find sum of series.
#include <stdio.h> // header file for standard input output
int main()
{
int n, i;
float sum=0;
printf("Type any number >>| ");
scanf("%d", &n); // take value of N
printf("\nHere is your Series and their sum...\n");
for (i = 1; i <= n; ++i) // loop range from 1 to N
{
printf("1/%d", i); // display series terms
sum = sum + (1.0 / i); // add series term values to sum variable
if (i < n)
printf(" + ");
}
printf("\n=| %f |\n", sum); // display series sum as result to user
getchar();
getchar();
}
Output :-
Type any number >>| 5
Here is your Series and their sum...
1/1 + 1/2 + 1/3 + 1/4 + 1/5
=| 2.283334 |
C++ program to find sum of series.
#include <iostream> // header file for input output stream
using namespace std;
int main()
{
int n, i;
float sum=0;
cout<<"Type any number >>| ";
cin>>n; // take value of N
cout<<"\nHere is your Series and their sum..."<<endl;
for (i = 1; i <= n; ++i) // loop range from 1 to N
{
cout<<"1/"<<i; // display series terms
sum = sum + (1.0 / i); // add series term values to sum variable
if (i < n)
cout<<" + ";
}
cout<<"\n=| "<<sum<<" |"<<endl; // display series sum as result to user
getchar();
getchar();
}
Output :-
Type any number >>| 6
Here is your Series and their sum...
1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6
=| 2.45 |
Java program to find sum of series.
public class harmonicSeries {
public static void main(String[] args) {
int n=7, i;
float sum=0;
System.out.println("Here value of N >> 7\n");
System.out.println("Here is your Series and their sum...");
for (i = 1; i <= n; ++i) // loop range from 1 to N
{
System.out.print("1/"+i); // display series terms
sum = sum + (1.0f / i); // add series term values to sum variable
if (i < n)
System.out.print(" + ");
}
System.out.println("");
System.out.println("=| "+sum+" |"); // display series sum as result to user
}
}
Output :-
Here value of N >> 7
Here is your Series and their sum...
1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7
=| 2.5928574 |
C program to find sum of series using recursion.
#include <stdio.h> // header file for standard input output
float harmonic(int n);
int main()
{
int n;
printf("Type any number >>| ");
scanf("%d", &n); // take value of N
printf("\nHere is your Series and their sum...\n");
printf("\n=| %f |\n", harmonic(n)); // display series sum as result to user
getchar();
getchar();
}
float harmonic(int n)
{
float sum=0;
if(n>0)
{
sum=(1.0/n)+harmonic(n-1); // add series term values to sum variable
printf("1/%d", n); // display series terms
printf(" + ");
}
return sum;
}
Output :-
Type any number >>| 5
Here is your Series and their sum...
1/1 + 1/2 + 1/3 + 1/4 + 1/5 +
=| 2.283334 |
C++ program to find sum of series using recursion.
#include <iostream> // header file for standard input output
using namespace std;
float harmonic(int n);
int main()
{
int n;
cout<<"Type any number >>| ";
cin>>n; // take value of N
cout<<endl<<"Here is your Series and their sum..."<<endl;
float sum=harmonic(n);
cout<<endl<<"=| "<<sum<<" |"<<endl; // display series sum as result to user
getchar();
getchar();
}
float harmonic(int n)
{
float sum=0;
if(n>0)
{
sum=(1.0/n)+harmonic(n-1); // add series term values to sum variable
cout<<"1/"<<n; // display series terms
cout<<" + ";
}
return sum;
}
Output :-
Type any number >>| 6
Here is your Series and their sum...
1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 +
=| 2.45 |
Java program to find sum of series using recursion.
public class harmonicSeries {
static float harmonic(int n) {
float sum = 0;
if (n > 0) {
sum = (1.0f / n) + harmonic(n - 1); // add series term values to sum variable
System.out.print("1/" + n); // display series terms
System.out.print(" + ");
}
return sum;
}
public static void main(String[] args) {
int n = 7;
System.out.println("Here value of N >> 7\n");
System.out.println("Here is your Series and their sum...");
float sum=harmonic(n);
System.out.println("");
System.out.println("=| " + sum + " |"); // display series sum as result to user
}
}
Output :-
Here value of N >> 7
Here is your Series and their sum...
1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 +
=| 2.5928574 |
Attention readers! Don't stop learning now. Check out our articles to gain more knowledge.
0 Comments
If you have any doubt. Let me know.