Program to find sum of series 1^1/1!+2^2/2!+3^3/3!...+n^n/n!

Problem statement:- Program to find the sum of series 1^1/1!+2^2/2!+3^3/3!...+n^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/1!+2^2/2!+ 3^3/3!...+n^n/n!.

Code:

#include<stdio.h>
#include<math.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++)
    {
     fact*=i;
      sum+=pow(i,i)/fact;
    }

    printf("The sum of the series = %0.2lf",sum);
}

Input/Output:
Enter the range of number:5
The sum of the series = 44.21

Program in C++
  
Here is the source code of the C++ Program to find the sum of series 1^1/1!+2^2/2!+ 3^3/3!...+n^n/n!.

Code:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n,i,fact;
    double sum=0.0;
    cout<<"Enter the range of number:";
    cin>>n;

    for(i=1;i<=n;i++)
    {
      fact*=i;
      sum+=pow(i,i)/fact;
    }
    cout<<"The sum of the series = "<<sum;
}

Input/Output:
Enter the range of number:7
The sum of the series = 272.41

Program in Java
  

Here is the source code of the Java Program to find the sum of series 1^1/1!+2^2/2!+ 3^3/3!...+n^n/n!.

Code:

import java.util.Scanner;
public class p20 {

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+=Math.pow(i,i)/fact;
    }
    System.out.println("The sum of the series = "+sum);
    cs.close();
}
}

Input/Output:
Enter the range of number:
9
The sum of the series = 1756.138318452381

Program in Python
  
Here is the source code of the Python Program to find the sum of series 1^1/1!+2^2/2!+ 3^3/3!...+n^n/n!.

import math
print("Enter the range of number:")
n=int(input())
sum=0.0
fact=1
for i in range(1,n+1):
    fact*=i
    sum += pow(i, i) / fact
print("The sum of the series = ",sum)

Input/Output:
Enter the range of number:
20
The sum of the series =  69257915.90725157



Post a Comment

0 Comments