Factorial Loop

PHOTO EMBED

Sun Apr 21 2024 11:23:02 GMT+0000 (Coordinated Universal Time)

Saved by @meanaspotato #c

#include <stdio.h>

int main(void)
{
    int number;
    int result = 1;

    // Prompt the user to input a non-negative whole number
    printf("Enter a non-negative whole number: ");
    scanf("%d", &number);

    // Check if the input is negative
    if (number < 0)
    {
        printf("\nBad Input! %d is negative...\n", number);
        return 1; // Terminate the program with an error code
    }

    // Compute the factorial
    for (int i = number; i >= 1; i--)
    {
        result *= i;
    }

    // Display the factorial result
    printf("\n%d! is %d\n", number, result);

    return 0;
}
content_copyCOPY