All pastes #265815 Raw Edit

Something

public text v1 · immutable
·published 2006-12-03 17:52 UTC
rendered paste body
/* return_factorial.c - Script 7.3 */
#include <stdio.h>
unsigned long long int return_factorial (unsigned int num); /* Function prototype */

int main(void) { /* Beginning main function */

// Declaring required variables
char input[3]; // keyboard input string
int number; 	// number to work with

// Prompt the user
printf ("Enter a positive integer for which the factorial will be calculated: ");
// Read input and assign to number variables
fgets (input, sizeof(input), stdin); // read input
sscanf (input, "%d", &number); //Assign input to variable number

if (number > 0 ) { // Validating number for conditional

printf ("The factorial of %d is %lu.\n", number, return_factorial(number)); //Call upon prototype if true
} else {
	printf ("You must enter a positive integer!\n");
} // Completed conditional

//Complete main function
getchar();
return 0;
} // Completed main function

//Defining return_factorial function that takes a number and returns its factorial
unsigned long long int return_factorial (unsigned int num) {

// Define required variables
unsigned long long int sum = 1; // factorial sum for loop
unsigned int i; // Loop index
// Calculate factorial through for loop
for (sum = 1, i = 1; i <= num; ++i) { //Starting loop
	sum *= i;
}

return sum;
} // End of factorial function