Problem Statement:- Program to Find the sum of series 1/1!+2/2!+3/3!.....+N/N!.
Data requirement:-
Input Data:- n
Output Data:-Sum
Additional Data:- i, fact
Program in C
Here is the source code of the C Program to Find the sum of series 1/1!+2/2!+3/3!.....+N/N!.
Code:
#include <stdio.h>
int main()
{
int n, i, fact = 1;
double sum = 0.0;
printf("Enter the range of number:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
//Calculating Factorial of each ith Number
fact *= i;
sum = sum + ((double)i / (double)fact);
}
printf("The sum of the series =%0.2lf", sum);
}
Enter the range of number:5
The sum of the series = 2.71
Program in C++
Here is the source code of the C++ Program to Find the sum of series 1/1!+2/2!+3/3!.....+N/N!.
Code:
#include <iostream>
using namespace std;
int main()
{
int n, i, fact = 1;
double sum = 0.0;
cout << "Enter the range of number:";
cin >> n;
for (i = 1; i <= n; i++)
{
fact *= i;
sum = sum + ((double)i / (double)fact);
}
cout << "The sum of the series = " << sum;
}
Input/Output:
Enter the range of number:6
The sum of the series = 2.71667
Program in Java
Here is the source code of the Java Program to Find the sum of series 1/1!+2/2!+3/3!.....+N/N!.
Code:
import java.util.Scanner;
public class Sum_of_Series1 {
public static void main(String[] args) {
Scanner cs = new Scanner(System.in);
int n, i, fact = 1;
double sum = 0.0;
System.out.println("Enter the range of number:");
n = cs.nextInt();
for (i = 1; i <= n; i++)
{
fact *= i;
sum = sum + ((double) i / (double) fact);
}
System.out.println("The sum of the series = " + sum);
cs.close();
}
}
Enter the range of number:
30
The sum of the series =
2.718281874018304
Program in Python
Here is the source code of the Python Program to Find the sum of series 1/1!+2/2!+3/3!.....+N/N!.
Code:
n = int(input("Enter the range of number:"))
sum = 0.0
fact = 1
for i in range(1, n+1):
fact *= i
sum += i/fact
print("The sum of the series = ", sum)
Enter the range of
number:6
The sum of the series
= 2.7166666666666663
Most
Recommend
Questions:-
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box