Given an int, number, calculate the factorial for number. c++ format with loop or while loop can not use return more than once

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter7: Arrays
Section: Chapter Questions
Problem 7PP: (Numerical) Using the srand() and rand() C++ library functions, fill an array of 1000 floating-point...
Question

Given an int, number, calculate the factorial for number. c++ format with loop or while loop can not use return more than once

Expert Solution
Step 1: Algorithm:

1. Start

2. Initialize an integer variable 'number' to store the input number.
3. Prompt the user to enter a number and store it in 'number'.

4. Check if the 'number' is less than 0:
   - If yes, display an error message "Factorial is not defined for negative numbers." and exit.
   
5. Initialize an integer variable 'factorial' to 1 to store the factorial result.
6. Initialize an integer variable 'i' to 1, which will be used as a loop counter.

7. Enter a while loop with the condition (i <= number):
   - While the value of 'i' is less than or equal to 'number', repeat the following steps.

   8. Multiply 'factorial' by 'i' and update 'factorial' with the result.
   9. Increment 'i' by 1 to move to the next iteration of the loop.

10. Exit the while loop when the condition (i <= number) is no longer met.

11. Display the result by printing "Factorial of 'number' is 'factorial'."

12. End.


steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Types of Loop
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:
9780357392676
Author:
FREUND, Steven
Publisher:
CENGAGE L
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning