How can I use c programming to find the factorial of a number,n?
It should be written in the c programming language. The program should use a while loop. It should prompt the user for the value of n and display its corresponding factorial value. Can anyone give me the source code for the program?
Public Comments
- Take a c programming course to find out. It's a lot of work, but you can get a credit for it if you pass.
- #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <iostream> using namespace std; int fact(int n) { int i; long int x=1; for (i=1;i<=n;i++){ x=x*i; } return x; } int main() { int a; while (1) { printf("\n Enter a number "); scanf("%d",&a); printf("\n %10d! = %5ld \n",a,fact(a)); //to wait and see the answer system("pause"); } return 0; }
- //USING WHILE LOOPS FOR FINDING FACTORIAL IS NOT RECOMMENDED.. //STANDARD METHOD IS BY RECURSION // THIS PROGRAM FINDS FACTORIAL BY 1)RECURSION 2)WHILE LOOP #include<stdio.h> long int factorial(int n) { if(n==1) return 1; else return(n*factorial(n-1)); } void main() { int n,i=1; long int fact=1; printf("\nEnter a Number:"); scanf("%d",&n); printf("\n\nFactorial of %d by recursion algorithm: %ld",n,factorial(n)); //using while loop while(i<=n) { fact=fact*i; i++; } printf("\n\nFactorial of %d by while loop : %ld",n,fact); }
Powered by Yahoo! Answers