Posted By:
23 Jun, 2013 1:15 pm PDT

Finding factorial of a number in C

How to find the factorial of a number using functions and pointers?
Attachment:None


A factorial of a number is found by the following logic:

n! = n*(n-1)*(n-2)*(n-3)...3.2.1

 

For example factorial of 3 =3*2*1=6

Please see the video for more details

Was it useful?  



#include<stdio.h>

void main()

{

    int a,i,fact=1;   

    clrscr();   

    printf("Enter the number please: ");   

    scanf("%d",&a);   

    for(i=1;i<=a;i++)   

    {       

        fact=fact*i;     

    }   

    printf("\nThe factorial of %d is %d.",a,fact);   

    getch();   

}


Was it useful?  



Hi , we can use functions and pointers to find the factorial of a given number  . 

The logic of finding the factorial is 

 for(i=1;i<=a;i++)   

    {       

        fact=fact*i;     

    }   

the following is the code for finding the factorial of a given number using functions.

#include <stdio.h>

#include <conio.h>

void main()

{

int n,x;

int fun(int);

printf("enter  any no...");

scanf("%d",&n);

x=fun(n);

printf("factorial of %d is %d ",n,x);

getch();

}

fun(int z)

{

int fact=1;

for(;z!=0;z--)

  {

fact*=z ;

  }

 return(fact);

}

 

Please find attached the program for finding the factorial of number using pointers .

Attachment: FactPoint

Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud