0% found this document useful (0 votes)
10 views2 pages

2.wap To Calculate Factorial of A Number

Uploaded by

adipandey845
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

2.wap To Calculate Factorial of A Number

Uploaded by

adipandey845
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

2.WAP TO CALCULATE FACTORIAL OF A NUMBER.

#include <iostream>

using namespace std;

// Function to calculate factorial

int factorial(int n) {

int fact = 1;

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

fact *= i; // Multiplying current number to the result

return fact;

int main() {

int num;

// Input a number from the user

cout << "Enter a number: ";

cin >> num;

// Validate input to ensure it is a non-negative number

if (num < 0) {

cout << "Factorial is not defined for negative numbers." << endl;

} else {
// Calculate factorial

int result = factorial(num);

// Display the result

cout << "The factorial of " << num << " is: " << result << endl;

return 0;

Output:-

Enter a number: 5

The factorial of 5 is: 120

You might also like