You're online now.

Hurray! you are online now.

Write C program to calculates the factorial of a given number

This C program calculates the factorial of a given number entered by the user. The main function is returning an integer value of 0, which indicates that the program has executed successfully.

#include<stdio.h>
    int main(){
        int n, fact = 1;
        printf("Enter the number : ");
        scanf("%d", &n);
        for(int i = 1; i <= n; i++){
            fact *= i;
        }
        printf("Factorial : %d", fact);
        return 0;
    }
#include<stdio.h>

Includes the standard libarary for using the input/output inbuild operation.

int main(){

Entry point of the program, whcih called in c program main() function.

int n, fact = 1;

Declared n as integer and set fact = 1 as integer.

printf("Enter the Number : ");

Prompt to the entter the number using the printf() function.

scanf("%d", &n);

Reads n from the user using the scanf() function which include in stdio.h library.

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

Factorial of any number is the multiplication from to n which called the factorial. Here we performs the same logic using the for loop, which iterates from 1 to n (which given from the user) andd in the loop body fact = i (fact = fact * i) multiple from 1 to n and store the result in fact variable.

printf("Factorial : %d", fact);

After calculate the factorial print the factorial on the console/termiinal.

return 0;
    }

At the end return 0, which means that program is successfully executed.

Algorithm

1. Start
2. Decalres n Set fact = 1
3. Takes input number from USER
4. for 1 to n performs fact *= i or fact = fact * i
5. Display Factorial result
6. Exit

Pseudocode

1. INCLUDE stdio.h
2. FUNCTION main()
    1. DECLARE n, SET fact = 1
    2. PROMPT "Enter the Number"
    3. READ n FROM USER
    4. for 1 to n
        fact *= i
    5. PRINT "Factorial"
    6. RETURN 0
🖤 0
Buy me coffee ☕

Comments

Oops!

No comments here...