Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Sunday, July 31, 2022

C program to display the factorial of a non-negative number.

 C program to display the factorial of a non-negative number.


C program to display the factorial of a non-negative number.



Introduction:

In programming, a factorial is a mathematical function that is used to calculate the product of all positive integers less than or equal to a given number. It is often used in algorithms and mathematical calculations. In this article, we will explore a 'C' program that displays the factorial of a non-negative number.


The Program:

The first step in writing this program is to prompt the user to enter a non-negative integer. We will then use a for loop to calculate the factorial of the number entered by the user.



#include<stdio.h>
int main()
{
int n,fact=1,num;
printf("Enter any number\n");
scanf("%d",&num);
for(n=1;n<=num;n++)
{
fact=fact*n;
}
printf("The factorial of the given number is: %d",fact);
return 0;
}


Output:




Conclusion:

In conclusion, this 'C' program is a great example of how to calculate the factorial of a non-negative number using a for loop. The program prompts the user to enter a non-negative integer and then calculates its factorial using a for loop. It is a simple yet powerful example of how programming can be used to perform mathematical calculations efficiently. As a programmer, it is essential to have a good understanding of mathematical functions and algorithms, as they are a fundamental part of many programming applications.


You can also Read:





No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages