Ad Code

Responsive Advertisement

Finding Factorial of Numbers In C With Different Ways...........



Finding Factorial of Numbers In C With Different Ways

What is Fictorial?

  Factorial of a number that multiplies that number with the numbers comes before it till 1. It is denoted- in Mathematics- by '!' For Example 2! 3! etc...

In Mathematics It is explained by the following: 

  • 10!= 10*9*8*7*6*5*4*3*2*1=3628800
  • 20!= 20*19*18*.......*2*1=2.432902e+18

And so on


Finding Factorials by using C...

In C we use different codes or programs to find factorials. Factorials have vital importance in

 the C language course, especially in the 2nd Year's course. 

Here I will teach different methods and programs to find Factorials


We will find Factorial by following ways:

    • By While Loop
    • By Do While loop
    • By For loop

Before start let us Talk about the loops

What is Loop?
The statement or set of statements that are executed repeatedly is known as Loop
and the structure that repeats the statements called Looping contract.
while, do-while and for are types of Loops.
While Loop:
"It executes the statements while a given condition is true"
Its Syntax is:

while(condition)
{
statement 1;
statement 2;
.
.
statement N;
}
Do-While Loop:
"It is similar to while loop but 'while' comes after 'Do' and the condition is in while "
Its Syntax is:


Do
{
  statement 1;
statement 2;
.
.
statement N;
}
while(condition);


For Loop:
"It executes the statements for a specific number of times. This loop is also called a counter-controlled loop. It is more flexible so most programmers use for loop"
Its Syntax is:


for(initialization;condition;increment/decrement)
{
statement 1;
statement 2;
.
.
statement N;
}

Lets Start

Factorial By While Loop:



#include<stdio.h>

#include<conio.h>
main()
{
    int a,b,f;
    b=1;
    f=1;
    printf("Enter a number");
    scanf("%d",&a);
    while(b<=a)
    {
        f=f*c;
        c++;
    }
    printf("Factorial of %d is %d",a,f);

}




Factorial By Do While loop:

#include<stdio.h>
#include<conio.h>
main()
{
    int a,b,f;
    b=1;
    f=1;
    printf("Enter a number");
    scanf("%d",&a);
  do
    {
        f=f*b;
        b++;
    }
      while(b<=a);
    printf("Factorial of %d is %d",a,f);

}
Factorial by while loop


Factorial By For Loop:

#include<stdio.h>
#include<conio.h>
main()
{
    int a,b,f;
    f=1;
    printf("Enter a number");
    scanf("%d",&a);
    for(b=1;b<=a;b++)
        f=f*b;
    printf("Factorial of %d is %d",a,f);

}
Factorial by For loop

****************************
In any case of confusion tell me in a comment
****************************
Reactions

Post a Comment

3 Comments