Factorial of a Number - Non recursive -c program

Program:

(gcc)

#include<stdio.h>
main ()
{
      int i,n,fact=1;
      printf("Enter the number: ");
      scanf("%d",&n);
      for(i=1;i<=n;i++)
     {
       fact = fact*i;
     }
      printf("Factorial=%d\n",fact);

}

Output:
nn@linuxmint ~ $ gcc c1.c
nn@linuxmint ~ $ ./a.out
Enter the number: 5
Factorial=120
nn@linuxmint ~ $


(Turboc)

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,f=1;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
if(n<0)
printf("sorry.......");
else if(n<=1)
printf("Factorial =1.");
else
    {   
        for(i=1;i<=n;i++)
        {
            f*=i;
        }
    printf("Factorial=%d",f);
    }
getch();
}




0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...