C Program To Find Sum of Series 1/1! + 2/2! + 3/3! + …. + n/n!

Lets write a C program to add first seven terms of the following series 1 / 1! + 2 / 2! + 3 / 3! + ….. + 7 / 7!

We’ve also written the C program to ask the user to enter the number of terms of the series that has to be added. You can find code to both of it below.

Related Read:
For Loop In C Programming Language
while loop in C programming
C Program To Find Factorial of a Number using For Loop

C Program To Find Sum of Series 1/1! + 2/2! + 3/3! + …. + n/n!


[youtube https://www.youtube.com/watch?v=Nok-OqhKpDY]

YouTube Link: https://www.youtube.com/watch?v=Nok-OqhKpDY [Watch the Video In Full Screen.]


Source Code: C Program To Find Sum of Series 1/1! + 2/2! + 3/3! + …. + 7/7!

 

int main()
{
    int num = 1, count;
    float sum = 0.0, fact;

    while(num <= 7)
    {
        fact = 1;
        for(count = 1; count <= num; count++)
        {
            fact = fact * count;
        }

        sum = sum + (num / fact);

        num++;
    }

    printf("Sum of series is %f\n", sum);

    return 0;
}

Output:
Sum of series is 2.718056

In above code, while loop iterates from 1 to 7. For each iteration for loop calculates the factorial of the selected number – which is present in variable num. Outside for loop, we add the individual series elements. At the end of while loop execution, variable sum will have sum of 7 terms of the series.

Source Code: C Program To Find Sum of Series 1/1! + 2/2! + 3/3! + …. + n/n!

 

int main()
{
    int num = 1, count, limit;
    float sum = 0.0, fact;

    printf("Enter the number of terms\n");
    scanf("%d", &limit);

    while(num <= limit)
    {
        fact = 1;
        for(count = 1; count <= num; count++)
        {
            fact = fact * count;
        }

        sum = sum + (num / fact);

        num++;
    }

    printf("Sum of %d terms of series is %f\n", limit, sum);

    return 0;
}

Output 1:
Enter the number of terms
7
Sum of 7 terms of series is 2.718056

Output 2:
Enter the number of terms
8
Sum of 8 terms of series is 2.718254

In above code, we ask the user to enter the number of terms in the series that needs to be added. While loop iterates until the user entered number of times. Inside for loop we calculate the factorial of the selected number(number is selected by while loop and it’ll be present in variable num). Outside the for loop we add the individual series element. At the end of execution of while loop we’ll have sum of the series until user entered number of terms in the series.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Factorial of a Number using For Loop

Write a C program to find Factorial of a user input number, using for loop.

Related Read:
For Loop In C Programming Language
C Program To Find Factorial of a Number using While Loop

Example:
Factorial of 5 is 120 (1 x 2 x 3 x 4 x 5 = 120).

In general, n objects can be arranged in n(n – 1)(n – 2) … (3)(2)(1) ways. This product is represented by the symbol n!, which is called n factorial. By convention, 0! = 1.

Source Code: C Program To Find Factorial of a Number using For Loop

 
#include<stdio.h>
int main()
{
    int num, count, fact = 1;

    printf("Enter a number to find its Factorial\n");
    scanf("%d", &num);

    for(count = 1; count <= num; count++)
    {
        fact = fact * count;
    }

    printf("Factorial of %d is %d\n", num, fact);

    return 0;
}

Output 1:
Enter a number to find its Factorial
5
Factorial of 5 is 120

Output 2:
Enter a number to find its Factorial
4
Factorial of 4 is 24

Output 3:
Enter a number to find its Factorial
3
Factorial of 3 is 6

Output 4:
Enter a number to find its Factorial
6
Factorial of 6 is 720

Output 5:
Enter a number to find its Factorial
0
Factorial of 0 is 1

C Program To Find Factorial of a Number using For Loop


[youtube https://www.youtube.com/watch?v=0YENXAwrlSI]

YouTube Link: https://www.youtube.com/watch?v=0YENXAwrlSI [Watch the Video In Full Screen.]


Logic To Find Factorial of a Number using For Loop

If user enters num as 4. Then here are the values of variable count and fact for each iteration of for loop.

Iteration 1
count = 1;
fact = 1;

fact = fact * count;
fact = 1 * 1;

count = 2; // value of count increments by 1 for each iteration.
fact = 1;

Iteration 2
count = 2;
fact = 1;

fact = fact * count;
fact = 1 * 2;

count = 3; // value of count increments by 1 for each iteration.
fact = 2;

Iteration 3
count = 3;
fact = 2;

fact = fact * count;
fact = 2 * 3;

count = 4; // value of count increments by 1 for each iteration.
fact = 6;

Iteration 4
count = 4;
fact = 6;

fact = fact * count;
fact = 6 * 4;

count = 5; // value of count increments by 1 for each iteration.
fact = 24;

Now the value of count is 5, which is greater than the user input number 4. So the control exits for loop. We print the value present inside variable fact as the Factorial of the number. So in this case, 24 is the Factorial of number 4.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Factorial of a Number

Write a C program to find Factorial of a user input number, using while loop.

Related Read:
while loop in C programming
Assignment Operators in C

Factorial of a number is the product of all the numbers preceding it. For example, Factorial of 5 is 120 (1 x 2 x 3 x 4 x 5 = 120).

In general, n objects can be arranged in n(n – 1)(n – 2) … (3)(2)(1) ways. This product is represented by the symbol n!, which is called n factorial. By convention, 0! = 1.

Source Code: C Program To Find Factorial of a Number

 
#include<stdio.h>

int main()
{
    long int num, count = 1, fact = 1;

    printf("Enter a number to find factorial\n");
    scanf("%d", &num);

    while(count <= num)
    {
        fact = fact * count; // fact *= count;
        count++;
    }

    printf("Factorial of %d is %d\n", num, fact);

    return 0;
}

Output 1:
Enter a number to find factorial
4
Factorial of 4 is 24

Output 2:
Enter a number to find factorial
5
Factorial of 5 is 120

Output 3:
Enter a number to find factorial
6
Factorial of 6 is 720

Output 4:
Enter a number to find factorial
8
Factorial of 8 is 40320

Output 5:
Enter a number to find factorial
10
Factorial of 10 is 3628800

C Program To Find Factorial of a Number using While Loop


[youtube https://www.youtube.com/watch?v=43RdmXXcr6Y]

YouTube Link: https://www.youtube.com/watch?v=43RdmXXcr6Y [Watch the Video In Full Screen.]


Logic To Find Factorial of a Number

If user enters num as 4. Then here are the values of variable count and fact for each iteration of while loop.

Iteration 1
count = 1;
fact = 1;

fact = fact * count;
fact = 1 * 1;

count = 2; // value of count increments by 1 for each iteration.
fact = 1;

Iteration 2
count = 2;
fact = 1;

fact = fact * count;
fact = 1 * 2;

count = 3; // value of count increments by 1 for each iteration.
fact = 2;

Iteration 3
count = 3;
fact = 2;

fact = fact * count;
fact = 2 * 3;

count = 4; // value of count increments by 1 for each iteration.
fact = 6;

Iteration 4
count = 4;
fact = 6;

fact = fact * count;
fact = 6 * 4;

count = 5; // value of count increments by 1 for each iteration.
fact = 24;

Now the value of count is 5, which is greater than the user input number 4. So the control exits while loop. We print the value present inside variable fact as the Factorial of the number. So in this case, 24 is the Factorial of number 4.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Find the Factorial of a Number: C

This video tutorial illustrates finding factorial of a given number using C programming.

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

Ex:
if user enters 5, then the factorial is 1 * 2 * 3 * 4 * 5 i.e., factorial = 120
This logic must be handled with a c program.

Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include < stdio.h >
#include < conio.h >
 
void main()
{
int num, i, fact = 1;
clrscr();
 
printf("Enter a number\n");
scanf("%d", &num);
 
for( i = 1; i < = num; i++ )
 fact = fact * i;                // fact *= i;
 
printf("\nFactorial of %d is %d", num, fact);
 
getch();
 
}

Here the for loop stars from 1 and not 0. As anything multiplied by 0 would also be zero.

Note: Factorial of 0 is 1. i.e., 0! = 1

factorial-cpp


fact *= i; is the compact representation of fact = fact * i;

Video Tutorial: Factorial in c


[youtube https://www.youtube.com/watch?v=PYLaSrO84nU]

YouTube Link: https://www.youtube.com/watch?v=PYLaSrO84nU [Watch the Video In Full Screen.]



Output:
Enter a number
5
Factorial of 5 is 120

Also Watch/Read: Find the Factorial of a Number: C++

Find the Factorial of a Number: C++

Video tutorial to find the factorial of a number: if the user enters 3, then the factorial is 1 * 2 * 3 i.e., factorial = 6
This logic must be handled with a c++ program.

Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream .h>
#include<conio .h>
 
void main()
{
  int fact = 1, N;
  clrscr();
 
  cout< <endl<<"Enter a number\n";
  cin>>N;
 
  for( int i=1; i< =N; i++ )
   fact = fact * i;  // OR fact *= i;
 
  cout<<endl<<endl<<"Factorial of "<<N<<" is "<<fact;
 
  getch();
}

Here the for loop stars from 1 and not 0. As anything multiplied by 0 would also be zero.

factorial-cpp


fact *= i; is the compact representation of fact = fact * i;

Video Tutorial: Factorial in cpp


[youtube https://www.youtube.com/watch?v=ovrdIQZ6Hu8]

YouTube Link: https://www.youtube.com/watch?v=ovrdIQZ6Hu8 [Watch the Video In Full Screen.]



Output:
Enter a number
5
Factorial of 5 is 120