Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

3.Easy code of factorial in c program using if and else statement.

C
0
D
E

FACTORIAL IN C

CODE:

#include <stdio.h>


int main() {

    int n, factorial = 1;


    printf("Enter a positive integer: ");

    scanf("%d", &n);


    if (n < 0) {

        printf("Factorial is not defined for negative numbers.\n");

    } else {

        for (int i = 1; i <= n; i++) {

            factorial *= i;

        }


        printf("Factorial of %d is %d\n", n, factorial);

    }


    return 0;

}

OUTPUT:

Enter a positive integer: 5
Factorial of 5 is 120

Table of Contents

P R O G R A M M I N G

Factorial in c programming
factorial in c programming

E X E C U T E

factorial execution in c programming
factorial compilation in c programming

E X P L A N A T I O N

FACTORIAL IN C PROGRAM:

This code of factorial in  C program that calculates the factorial of a positive integer entered by the user. Let’s break it down step by step:

#include <stdio.h>

This line includes the standard input/output library (stdio.h) in your program. This library provides functions for input and output operations.

int main()
{
This is the beginning of the main function, which serves as the entry point for a C program. The int before main indicates that the function returns an integer value. The empty parentheses () indicate that this function takes no arguments.
 
int n, factorial = 1;
Here, two integer variables n and factorial are declared. n will store the positive integer input by the user, and factorial will store the calculated factorial value. factorial is initialized to 1

 

printf(“Enter a positive integer: “);

This line uses the printf function to display the message “Enter a positive integer: ” on the console, prompting the user to input a positive integer.

 scanf(“%d”, &n);
This line uses the scanf function to read an integer value from the user’s input and store it in the variable n. The %d in the format string specifies that scanf should expect an integer input. The & before n is used to provide the address of the variable n so that scanf can store the input value there.

if (n < 0) {
printf("Factorial is not defined for negative numbers.\n");
}
else
{

This is an if statement that checks if the value of n is less than 0, which would indicate a negative number. If n is negative, the program prints a message indicating that factorial is not defined for negative numbers. If n is non-negative, it proceeds to the else block.

for (int i = 1; i <= n; i++)
{

This line initiates a for loop, which will be used to calculate the factorial. A variable i is declared and initialized to 1. The loop will continue as long as i is less than or equal to n. The variable i is incremented by 1 in each iteration.

factorial *= i;

Within the for loop, this line updates the factorial variable by multiplying it with the current value of i. This accumulates the factorial calculation as the loop iterates through the values of i.

}

This is the closing brace of the for loop, indicating the end of the loop.

printf("Factorial of %d is %d\n", n, factorial);

}

Within the else block, this line uses printf to display the calculated factorial. It uses format specifiers to include both the input value n and the computed factorial. The \n at the end of the message represents a newline character, which moves the cursor to the next line after printing.

return 0;

Finally, this line marks the end of the main function and the program. It returns an integer value of 0 to the operating system, indicating that the program has run successfully. A return value of 0 typically means that no errors occurred during execution.

In summary, this factorial in C program begins by prompting the user to input a positive integer. It checks if the input is non-negative and then calculates the factorial of the input using a for loop. The result is displayed on the console, and the program returns 0 to indicate successful execution. If the input is negative, it informs the user that factorial is not defined for negative numbers. This program is a basic example of user input, conditional statements, loops, and output in C programming.

MORE PROGRAM :

BEST C PROGRAMMING BOOK :

 

Leave a Comment