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

Under what Vietnamese dynasty did Buddhism become the state religion?
Is 4/8 bigger than 4/6
Magnesium oxide added to water forms magnesium hydroxide
Which of the following best describes the United States just before the election of Franklin Roosevelt in 1932? A. In the midst of economic boom B. Just recove
Which best describes the trends in electronegativity on the periodic table?
Which type of bond forms protein chains? A. Actinide B. Beta sheet C. Lanthanide D. Peptide
Which theme is conveyed in this excerpt from "The Tell-Tale Heart" by Edgar Allan Poe? TRUE!—nervous—very, very dreadfully nervous I had been and am; but why w
How much force is required to accelerate an 8kg mass at 5m/s^2
What can be said about the polarity of water
Explain in a situation in which the reactant might weigh more than the product