Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Title Would you please provide an answer to this question using c++ A loop within a loop while statement for statement. And what exactly the

Title

Would you please provide an answer to this question using c++

A loop within a loop

while statement

for statement.

And what exactly the professor asks for. higly appreciated !!

Topics

A loop within a loop

while statement

for statement

Description

Write a program that computes the factorial of a positive whole number. The program asks the user to enter a positive whole number. It computes the factorial of the number provided and displays the result. The program does this repeatedly. In one execution of the program the user may obtain the factorial of several numbers one after the other.

/*

Java users

*/

The user quits the program by clicking cancel when prompted for a number.

/*

C++ users

*/

The user quits the program by entering -1 when asked for a number.

Notes

The factorial of a number n is the product of all numbers from 1 to n. The factorial of a few numbers is presented below:

Factorial of 0 = 1 (by definition).

Factorial of 1 = 1

Factorial of 2 = 1 * 2 = 2

Factorial of 3 = 1 * 2 * 3 = 6

Factorial of 4 = 1 * 2 * 3 * 4 = 24

Factorial of 5 = 1 * 2 * 3 * 4 * 5 = 120

Testing

Test Run

(User input is shown as bold).

Enter a number:

5

The factorial of 5 is 120

Enter a number:

6

The factorial of 6 is 720

Enter a number:

0

The factorial of 0 is 1

Enter a number:

7

The factorial of 7 is 5040

Enter a number:

(Java users, push Cancel)

(C++ users, enter -1)

Sample Code

Input loop

/*

Java users

*/

String in;

in = JOptionPane.showInputDialog( Enter a number);

while (in != null)

{

//compute factorial using a For loop

//display factorial

in = JOptionPane.showInputDialog( Enter a number);

}

/*

C++ users

*/

int num;

cout << Enter a number << endl;

cin >> num;

while (num >= 0)

{

//compute factorial using a For loop

//display factorial

cout << Enter a number << endl;

cin >> num;

}

For loop for computing factorial

int count, num, factNum;

factNum = 1;

for (count=1; count <=num; count++)

{

factNum = factN * count;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What is topology? Explain with examples

Answered: 1 week ago