Problem statement:- Program to find the sum of series 1+X+X^2/2...+X^N/N
Data requirement:-
Input Data:- n, x
Output Data:-Sum
Additional Data:-i
Program in C
Here is the source code of the C Program to find the sum of series 1+X+X^2/2...+X^N/N.
Code:
#include<stdio.h>
#include<math.h>
int main()
{
int n,i=1,x;
double sum=1.1;
printf("Enter the range of number:");
scanf("%d",&n);
printf("Enter the value of x:");
scanf("%d",&x);
while(i<=n)
{
sum+=pow(x,i)/i;
i++;
}
printf("The sum of the series = %0.2lf",sum);
}
#include<math.h>
int main()
{
int n,i=1,x;
double sum=1.1;
printf("Enter the range of number:");
scanf("%d",&n);
printf("Enter the value of x:");
scanf("%d",&x);
while(i<=n)
{
sum+=pow(x,i)/i;
i++;
}
printf("The sum of the series = %0.2lf",sum);
}
Input/Output:
Enter the range of number:4
Enter the value of x:2
The sum of the series = 11.77
Program in C++
Enter the value of x:2
The sum of the series = 11.77
Program in C++
Here is the source code of the C++ Program to find the sum of series 1+X+X^2/2...+X^N/N.
Code:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,i=1,x;
double sum=1.0;
cout<<"Enter the range of number:";
cin>>n;
cout<<"Enter the value of x:";
cin>>x;
while(i<=n)
{
sum+=pow(x,i)/i;
i++;
}
cout<<"The sum of the series = "<<sum;
}
#include<cmath>
using namespace std;
int main()
{
int n,i=1,x;
double sum=1.0;
cout<<"Enter the range of number:";
cin>>n;
cout<<"Enter the value of x:";
cin>>x;
while(i<=n)
{
sum+=pow(x,i)/i;
i++;
}
cout<<"The sum of the series = "<<sum;
}
Input/Output:
Enter the range of number:5
Enter the value of x:3
The sum of the series = 86.35
Program in Java
Enter the value of x:3
The sum of the series = 86.35
Program in Java
Here is the source code of the Java Program to find the sum of series 1+X+X^2/2...+X^N/N.
Code:
import java.util.Scanner;
public class p11 {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int n,i=1,x;
double sum=1.0;
System.out.println("Enter the range of number:");
n=cs.nextInt();
System.out.println("Enter the value of x:");
x=cs.nextInt();
while(i<=n)
{
sum+=Math.pow(x,i)/i;
i++;
}
System.out.println("The sum of the series = "+sum);
cs.close();
}
}
public class p11 {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int n,i=1,x;
double sum=1.0;
System.out.println("Enter the range of number:");
n=cs.nextInt();
System.out.println("Enter the value of x:");
x=cs.nextInt();
while(i<=n)
{
sum+=Math.pow(x,i)/i;
i++;
}
System.out.println("The sum of the series = "+sum);
cs.close();
}
}
Input/Output:
Enter the range of number:
7
Enter the value of x:
5
The sum of the series = 14606.297619047618
Program in Python
7
Enter the value of x:
5
The sum of the series = 14606.297619047618
Here is the source code of the Python Program to find the sum of series 1+X+X^2/2... +X^N/N.
Code:
print("Enter the range of number:")
n=int(input())
print("Enter the value of x:")
x=int(input())
sum=1.0
i=1
while(i<=n):
sum+=pow(x,i)/i
i+=1
print("The sum of the series = ",sum)
n=int(input())
print("Enter the value of x:")
x=int(input())
sum=1.0
i=1
while(i<=n):
sum+=pow(x,i)/i
i+=1
print("The sum of the series = ",sum)
Input/Output:
Enter the range of number:
5
Enter the value of x:
2
The sum of the series = 18.066666666666666
5
Enter the value of x:
2
The sum of the series = 18.066666666666666
Most Recommend Questions:-
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box