1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
rodikova [14]
3 years ago
9

Write a program which reads in a single integer 0 <= n < 30 and prints out the factorial of n. Factorials get very big ver

y quickly and can easily exceed the limits of a signed int that uses 4 bytes. If the value of factorial of n is too large to be stored in an int (>2,147,483,647), your program should print Can't handle this.

Computers and Technology
2 answers:
Alexus [3.1K]3 years ago
8 0

Answer:

The solution code is written in C++

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.    int num;
  6.    signed int factorial = 1;
  7.    
  8.    cout<<"Input a number: ";
  9.    cin>> num;
  10.    
  11.    if(num >= 0 && num <30){
  12.        
  13.        for(int i = num; i > 0; i--){
  14.            
  15.            if(factorial * i < 2147483647 && factorial > 0){
  16.                
  17.                factorial *= i;
  18.                
  19.            }else{
  20.                
  21.                cout<<"Can't handle this";
  22.                return 0;
  23.            }
  24.        }
  25.        cout<<factorial;
  26.    }
  27.    return 0;
  28. }

Explanation:

Firstly, let's create variable num and factorial to hold the value of input number and the factorial value. The variable factorial is defined with a signed int data type (Line 7-8).

Next prompt user input a number and do input validation (Line 10 -13). Then create a for-loop to calculate the factorial of input number (Line 15 - 26). Within the loop, create an if-else statement to check if the factorial of n is more than the limit, if so display the message "Can't handle this". Otherwise, the program will just print out the factorial after completing the loop (Line 28).

kaheart [24]3 years ago
7 0

Answer:

Python code along with step by step comments is provided below.

Python Code with Explanation:

# get input from the user

num = int(input("Enter a number: "))

factorial = 1

# check if the number is within the allowed range  

if num>=0 and num<=30:

# if number is within the range then run a for loop

   for i in range(1,num + 1):

# this is how a factorial is calculated

       factorial = factorial*i

# now check if the result is withing the limit or not

       if factorial>2147483647:

# if result is not within the limit then terminate the program

           print("Sorry, cant handle the result")

           exit()

# if result is within the limit then print the result

   print("The factorial of",num,"is",factorial)  

else:

# if input is not withing the range then print wrong input

   print("Sorry, Wrong input")

Output Results:

Enter a number: 10

The factorial of 10 is 3628800

Enter a number: 15

Sorry, cant handle the result

Enter a number: 35

Sorry, Wrong input

You might be interested in
01000111 01110101 01100101 01110011 01110011 00100000 01110111 01101000 01100001 01110100 00111111
Natasha_Volkova [10]

12435 425 2 52 35 Guess what?

8 0
3 years ago
How to check if serial interface is dte ot DCE?
Anna11 [10]

If you can see the DTE/DCE cable, you can tell by looking which router has the DCE interface connected to it - the letters "DTE" or "DCE" will either be molded into the connector itself, or if it's an older cable there should be a little piece of tape on the cable that tells you what the interface type is.

5 0
3 years ago
What is the component on the motherboard that confirms all devices are in working order once the computer is turned on?
Oduvanchick [21]

Answer: Bios chip which starts the motherboard check

Explanation: the BIOS (pronounced bye-oss) is a ROM chip found on motherboards that allows you to access and set up your computer system at the most basic level.

7 0
3 years ago
Which of the following is the correct style tag for having the web page's text be aligned to the right?
uysha [10]

Answer:

B. style=text-align: right

Explanation:

text-align is a CSS property having 5 types of values. Each of values define below.

left - Aligns content to left.

right - Aligns content to right.

center - Centers the content.

justify - Depends on the width of the content, it will align itself to both left and righ

inherit - It specifies the the value of text-align should be taken from parent element.

8 0
3 years ago
Read 2 more answers
Match each definition with the term it describes. A(n) ______ provides identification and addressing information for computers a
earnstyle [38]

Answer:

IP address, LAN, WAN

Explanation:

6 0
3 years ago
Other questions:
  • Which of the following self-contained APs are autonomous, or independent, because they are separate from other network devices a
    5·1 answer
  • You work as the Network Administrator for Perfect solutions Inc. The company has a Linux- based Network. You are a root user on
    15·1 answer
  • "The OSI model has seven layers and the DoD has four. At which layer does SMTP works in both models"?
    15·1 answer
  • if you want to presents slides to fellow sudents or co-workers witvh productively software should you use to create them?
    6·1 answer
  • Which of the following is considered proper typing technique?
    15·1 answer
  • When saving a PowerPoint presentation as a show which object will not be saved with the presentation
    13·2 answers
  • How is an API different from a web application?
    10·1 answer
  • Select the correct answer from each drop-down menu.
    8·2 answers
  • What is real time os ?​
    13·1 answer
  • Besides entering a URL to go directly to a website, what else can you enter in a browser address bar to explore the internet?
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!