0% found this document useful (0 votes)
1 views1 page

C++ Factorial Using Loop

The document provides a C++ program that calculates the factorial of a number using a for loop. It includes an explanation of the code, detailing the purpose of each variable and the loop's function. An example input and output are also provided to illustrate how the program works.

Uploaded by

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

C++ Factorial Using Loop

The document provides a C++ program that calculates the factorial of a number using a for loop. It includes an explanation of the code, detailing the purpose of each variable and the loop's function. An example input and output are also provided to illustrate how the program works.

Uploaded by

ayushvartha4
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.

You might also like