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

2 answers

hi there !
Note: replace "_" with space
//version for C
#include

void main(void)
{

__ int i,n;
__ int k=1;

__ printf("\nNumber =\t");
__ scanf("%d",&n);//read the number

___if(n >= 0 && n <= 19)// n is an integer that so should be <20
____{

_____for(i=1;i<=n;i++)
_________ k=k*i;
_____ printf("\nResult =\t%i", k);
____ }
___ else printf("\n\a Error !! please enter a positive number less than 20");

}

//Version for MS Visual C++
#include "stdafx.h"
#include


int _tmain(int argc, _TCHAR* argv[])
{

____ int nr,k,i;

_____k=1;

_____ std::cout << "Number:\t" ;
_____ std::cin >> nr ;

_____ if( nr >=0 && nr <= 19)
______{
________for(i = 1 ; i <= nr ; i++)
_____________k=k*i;
_______std::cout << "\nResult:\t" << k << '\n';
______}
______else
______std::cout << "\a Error ! Please enter a positive number less than 20!\n";

_____return 0;
}

2007-01-21 15:34:14 · answer #1 · answered by dand370 3 · 0 0

First input the number you want the factorial of using whatever method you prefer, then use a for loop.

If the variable you're using to store the number is x, and the variable to store the factorial is y:

int y = 1;
for(;x>1;x--) {
y=y * x;
}
printf("%d", y);

I think that's right. Haven't touched C in a while though.

2007-01-19 02:56:41 · answer #2 · answered by Jack 3 · 0 0

fedest.com, questions and answers