A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

C program to find the sum of Natural Number/Factorial of Number of all natural numbers from 1 to N
Q:

C program to find the sum of Natural Number/Factorial of Number of all natural numbers from 1 to N

0

C program to find the sum of Natural Number/Factorial of Number of all natural numbers from 1 to N.
Series: 1/1! + 2/2! + 3/3! + 4/4! + ... N/N!

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*  
This program will find the sum of Natural 
Number/Factorial of Number of all natural numbers from 1 to N.
*/

#include<stdio.h>
    
/*function to find factorial of the number*/
unsigned long factorial(int num)
{
	int i;
	/*always assign 1, if variable multiplies with values*/
	unsigned long fact=1;
	
	/*multiply num*num-1*num-2*..1*/
	for(i=num; i>=1; i--)
		fact= fact*i;
	
	/*return factorial*/
	return fact;
}

int main()
{
	int i,N;
	float sum;
	
	/*read value of N*/
	printf("Enter the value of N: ");
	scanf("%d",&N);
	
	/*set sum by 0*/
	sum=0.0f;
	
	/*calculate sum of the series*/
	for(i=1;i<=N;i++)
		sum = sum + ( (float)(i) / (float)(factorial(i)) );
	
	/*print the sum*/
	
	printf("Sum of the series is: %f\n",sum);
	
	return 0;
}
    Enter the value of N: 10
    Sum of the series is: 2.718282

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now