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
JAVA Code:
Burka [1]
Dear ,you should ask it on stackoverflow and geekforgeelks , not here
6 0
4 years ago
Amelia is going to school to get a degree in Early Childhood Education. She wants to open her own daycare after she graduates. W
Yanka [14]
She could volenteer at a day care at the weekend or study child development/care whilst she is still at school.
8 0
3 years ago
What effect does using quotas in file server resource manager have?
andrezito [222]
It limits the number of gigabytes allocated to a volume or folder
5 0
3 years ago
Give a real-world example of a selection control structure.
yuradex [85]

An example of selection structure is when a group of people wanted to know the exact  number of days through the use of a data set that has the daily high temperature which ranges from above 80 degrees, and as such, a programmer can use the if-end statement.

<h3>What is an if statement?</h3>

An IF statement is known to be a kind of statement that is executed due to the happening of a specific condition.

Note that IF statements must start with IF and end with the END.

Therefore, An example of selection structure is when a group of people wanted to know the exact  number of days through the use of a data set that has the daily high temperature which ranges from above 80 degrees, and as such, a programmer can use the if-end statement.

Learn more about  if-end statement from

brainly.com/question/18736215

#SPJ1

6 0
2 years ago
In relation to data science,advances in technology has made it more feasible to do what
Reika [66]
In recent years data science has become less and less about the data itself, and more about the technology and tools used to interact with it. Advanced solutions like AI, machine learning and robust analytics tools make it possible not just to process and understand massive stores of information but at unprecedented speeds.
3 0
3 years ago
Other questions:
  • Out of the following guidlines which should you use when writing a memo?
    10·2 answers
  • Video- sharing sites allow videos to be uploaded in which file format?
    5·2 answers
  • What is the best strategy to avoid paying interest on your credit card
    8·1 answer
  • A technician has verified that a video card to be added to a desktop machine will fit and so will a new sound card. What should
    15·1 answer
  • Which of the following is true about database queries?
    11·1 answer
  • #Write a function called alter_list. alter_list should have#two parameters: a list of strings and a list of integers.##The list
    11·1 answer
  • Which of the following is not a true statement? a. Data flow diagrams and flowcharts are difficult to prepare and revise using s
    10·1 answer
  • Bradley is working on a program that uses different classes in specific relationships. Help him remember what these relationship
    14·1 answer
  • Is a free verse a poetry that has no images
    5·2 answers
  • Source Code for TF2 - Please insert it here
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!