Develop a serial program to calculate a factorial using a for loop. No recursion. Time it.
to be done in c language.
1
Expert's answer
2013-02-27T04:20:55-0500
#include <iostream> using namespace std; void main() { int enteredNumber; long result = 1; printf("%s","enter the number from 1 to 16\n"); /*entered number can not exceed 16, otherwise the program will give the wrong result, because the results size will exceed of the 8 bytes*/ scanf("%d",&enteredNumber); if(enteredNumber > 16) { printf("%s","entered number can not exceed 16,otherwise the program will give the wrong result,because the results size will exceed of the 8 bytes\n"); return; } for( int i = 1; i<=enteredNumber; i++ ) { if(enteredNumber == 1)break; result *= i; } printf("%d",result); }
Comments
Leave a comment