English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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.

2007-02-16 21:50:27 · 4 answers · asked by bluemiano 2 in Computers & Internet Programming & Design

Can anyone give me the source code for the program?

2007-02-16 22:33:42 · update #1

4 answers

//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
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);

}

2007-02-16 23:09:59 · answer #1 · answered by KillingJoke 3 · 1 0

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.

2007-02-16 21:54:20 · answer #2 · answered by Anonymous · 0 0

The int style does not provide you a huge sufficient variety of values; double will help you compute greater factorials: #contain #contain double factorialR(unsigned); double factorialI(unsigned n); int considerable(int argc, char *argv[]) {     int x;     double y;     if ((argc < 2) || (sscanf(argv[one million],"%d",&x) != one million)) {           printf("nusage: %s ",argv[0]);           return -one million;       }     if (x < 0) x *= -one million;     printf("%d! = %.40gn",x,y = factorialI(x));     assert(y == factorialR(x));     return 0; } /* Iterative */ double factorialI(unsigned n) {     double Fn = one million;     mutually as (n != one million) Fn *= n--;     return Fn; } /* Recursive */ double factorialR(unsigned n) {     if (n == one million) return n;     return n * factorialR(n-one million); }

2016-10-15 12:17:37 · answer #3 · answered by Anonymous · 0 0

#include
#include
#include


#include
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;

}

2007-02-16 23:06:45 · answer #4 · answered by iyiogrenci 6 · 1 0

fedest.com, questions and answers