lundi 16 mars 2015

FCTRL 2 - c++ factorial program (codechef)


You are asked to calculate factorials of some small positive integers. Input


An integer t, 1<=t<=100, denoting the number of testcases, followed by t lines, each containing a single integer n, 1<=n<=100. Output


For each integer n given at input, display a line with the value of n! Example


Sample input: 4 1 2 5 3 Sample output: 1 2 120 6


Please tell me whats the mistake in my block of code ...whenever i try the test cases as specified in the problem, the output of the factorial of the last number 3 is incorrect (120 is the output for factorial of 3) Here is my block of code:



#include <iostream>

using namespace std;

/*
*
*/
int main(int argc, char** argv) {
int testcase, n, i = 1;
long fctrl = 1;
scanf("%d", &testcase);
while(testcase--) {
cin >> n;
while(i <= n) {
fctrl *= i;
i++;
}
cout << fctrl << endl;
}

return 0;
}



Aucun commentaire:

Enregistrer un commentaire