In: Computer Science
For the prelab assignment, you may not use the if statement nor may you use the if/else statement. You are to write a program that calculates factorials from 1 up to and including a maximum number given by the user. Because factorials get large quickly, use long unsigned int as the type for the number entered by the user as well as the factorials you are calculating. If the user enters a 0, print an error message. Then ask if the user wishes to continue the program or quit.
C Program:
#include <stdio.h>
int main()
{
//variable declaration
int i,choice;
unsigned long int n;
unsigned long int factorial = 1;
printf("Enter a number: ");
scanf("%lu", &n);
if(n == 0)
{
//if number is 0,then show error message and ask user to
continue
printf("Wrong input.Enter any number to continue.");
scanf("%d", &choice);
main(); //calling main method re-run from first
}else
{
//for loop for factorial calculation
for (i=1;i<= n;i++)
{
//calculating factorial
factorial=factorial*i;
//printing Factorial of number 'i'
printf("Factorial of %d is %lu ", i, factorial);
}
}
getch();
}
Output: