Friday, April 10, 2015

8. Write a C program to print the 10 positive integers and their factorials.


#include<stdio.h>
#include<conio.h>
long int factorial(int);
void main()
{
int num[10],i;
long int result;
clrscr();
printf("\n Input any 10 numbers:-");
for(i=0;i<10;i++)
  {
 printf("\n Number [%d]:-",i);
scanf("%d",&num);
if(num<0)
  printf("\n Invalid number");
else
{
result=factorial(num[i]);
printf("\n Factorial of %d is %1d",num[i],result);
}
}
getch();
}
long int factorial(int n)
{
int i;
long int fact=1;
if(n==0||n==1)
return(1);
else
{
for(i=1;i<=n;i++)
 fact=fact*i;
return(fact);
}
}

1 comment:

  1. // another way
    #include

    int fac(int x){
    if(x == 1){
    return 1;
    }else{
    return x* fac(x-1);
    }
    }

    int main(){
    int count = 0;

    for(int i=1; i<1000; i++){
    if(i%2 == 0){;
    printf("\n%d Factorial = %d",i,fac(i));
    count++;
    }
    if(count == 10){
    break;
    }

    }
    }

    ReplyDelete