Swsan7715 Swsan7715
  • 03-06-2021
  • Computers and Technology
contestada

3- Write a C++ program by using for loop that will ask for an integer as a parameter and display its factorial. Hint factorial (5)=5^ * 4^ * 3^ * 2^ * 1

Respuesta :

tonb
tonb tonb
  • 03-06-2021

Answer:

#include <iostream>

using namespace std;

int main() {  

 int n;

 cout << "Enter number: ";

 cin >> n;

 int fact = 1;

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

   fact *= i;

 }

 cout << n << "! = " << fact << endl;

}

Explanation:

Another cool way to do this is to write a recursive function. But here specifically a for loop was asked.

Answer Link

Otras preguntas

vigyan doctor 3 meter kise kahate Hain​
What law is very important for a small business?
What do the ions K+, Ca2+, and Cl- have in common? a. They have the same number of protons. b. They will form covalent bonds with oxygen. c. They have the same
Who is the chief minister of Assam?
what is science??????​
Which statement is correct? A. The opposite of the opposite of 2 is 2. B. The opposite of the opposite of 3 is . C. The opposite of the opposite of 1 is −
21. Why manyproperties of berylliumresemble aluminum, and boron resemblesilicon? This is due to A.amphoteric property C. allotropic relationship B. diagonal
What is the common difference in the sequence 10, 20, 30, 40, 50,... ? A. 10 B. -10 C. 1/2D. -1/2
The root operation for the adjustment of position of pacemaker. A) Reposition B) Revision C) Removal D) Repair
Explain who settled each of the british colonies and why