Pages

When Programming is Life

Wednesday, 4 February 2015

Find Factorial of a Number in C programming

Find Factorial of a Number in C programming
Find Factorial of a Number in C programming


hello guys. welcome to my second C programming tutorial in Standard Programming. Today I am gonna share the code and the logic behind a very popular, easy C programming Exercise. Every beginner does this program, finding factorial of a Number in his C programming Journey. So here I am gonna share that code.

Finding the Factorial of a number can be done in two ways. First one is by using Loops and second one is by using recursive function. It's up to you which process seems to be easy to you. Both will same effect on the program.

By using For Loop

First we will take a integer from user, factorial of which is about to be found out by us, via scanf. Then we will run a For loop with count from 1 to the number itself. Every time we will put the multiplication result in a variable. When the Loop ends, we will output the result to the screen. Simple Enough huh? xD


#include<stdio.h>
#include<conio.h>
int main(){

printf("Enter a Integer: ");
int n;
scanf("%d",&n);
int i;
int fac=1;
for(i=1;i<=n;i++){

  fac *=i;

}
printf("The Factorial of the number is: %d",fac);
getch();
return 0;
}


By using Recursive Function

Now we will use recursion to calculate the Factorial of the number. Here we will create a recursion function which takes the integer as parameter. Then we create a base case, where we return 1 in case the given integer is 1. In the else case we continue to do that process again and again with n-1 as parameter. As example if we take the integer as 5, then the function will be 5*fact(n-1), here fact is considered the function name. In later case, it will be 4*fact(n-1). It will continue till it goes to 1 which our base case and which returns 1. Simple! 

#include<stdio.h>
#include<conio.h>

int fac(int);

int main(){


printf("Enter a Number");
int a;
scanf("%d",&a);
int result;
result=fac(a);
printf("The factorial of the given number is: %d",result);

getch();
return 0;
}
int fac(int n){
int fact;
if(n==1){
    return (1);
} else{
fact=n*fac(n-1);
}
return fact;
}


Hope You have got the logic and the code I have used to calculate the factorial of the number in C programming. But this process is not so effective. Why? There is a reason which makes it that but I won't tell that to you here. I leave that up to you to think why it is not so effective. Need some clue? ok two word. Think BIG. Enough Hint I guess. 

There is another way by which we can calculate the factorial of a number without using Loops or Recursion. Can you say what's the method? Can't? Don't worry. In my following Tutorials I will be covering that also. Till then stay tuned with Standard Programming.!!  
Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
Posts RSSComments RSSBack to top
© 2011 Standard Programming ∙ Designed by BlogThietKe
Released under Creative Commons 3.0 CC BY-NC 3.0