ChatGPT Free offer Share
New chat
Write a c++ program to find factorial of a no.
By using construct
Search chats
Images
To find the factorial of a number using a loop construct in C++, you can
Apps
use a for loop.
Codex
C++ Program
Projects
C++
Your chats
#include <iostream>
C++ Factorial using loop using namespace std;
GUI Components Explained int main() {
int n, i;
Anniversary Website Prompt long long factorial = 1;
Swap Fits Realistically cout << "Enter a number: ";
cin >> n;
Outfit Exchange
for(i = 1; i <= n; i++) {
Notion Fashion Page Setup factorial = factorial * i;
}
cout << "Factorial of " << n << " is " << factorial;
return 0;
}
Explanation
1. n → stores the number entered by the user.
2. factorial → initialized to 1 because factorial multiplication starts
from 1.
3. The for loop runs from 1 to n .
4. Each time the loop runs, factorial is multiplied by i .
5. Finally, the factorial value is printed.
Example
Input:
Enter a number: 5
Output:
Factorial of 5 is 120
Retro If you want, I can also show:
R
Free
Factorial using while loop
Ask anything
Claim offer Factorial using recursion (very common in exams).
ChatGPT can make mistakes. Check important info. See Cookie
Preferences.