Calculate factorial of user entered number in c programming language

 Calculate factorial of user entered number


            In this example, we will understand to Calculate factorial of user entered number using for loop and know how to write, compile and debug it in C language and also learn how to implement it in c.

        
In order to do this you will need
  • Basic knowledge of c programming language.
  • As well as know how to operate codeblocks.   

The for statement: 
• The for loop is another entry-controlled loop that provides a more concise loop control
structure.

• The general form of the for loop is:

for(initialization; test-condition; increment)
{
     body of the loop
}

So let's begin with our program

1.Open a codeblocks software.

2.Open new empty file.

3.Copy and paste the code from below. 

The execution of the for statement is as follows:
  •  Initialization of the control variables is done first.
  •  The value of the control variable is tested using the test-condition.
  •  When the body of the loop is executed, the control is transferred back to the for statement after evaluating the last statement in the loop. Now, the control variable is either incremented or decremented as per the condition.

Input code-

// Program to calculate factorial of user entered number.

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

  printf("Enter a number: ");
  scanf("%d",&num);

  for(i=1;i<=num;i++)
      f=f*i;

  printf("Factorial of %d is: %d",num,f);
  return 0;
}



Now simply build the code. Look for any error. Now run the code.

For output enter any number you want to Calculate factorial of user entered number and press the enter key.

Calculate factorial of user entered number in c programming language.in c,cprogram


Click the following button to download Calculate factorial of user entered number in c programming language.




Thats it.Thank you for scrolling.


Post a Comment

If you have any doubt or suggestions.please let me know.

Previous Post Next Post

Ads by google

Ads by google