Question

Example 2: In this example we will simply find the factorial of a number which we will request from the user. More explicitly

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student,

As the programming language is not mentioned so by default i have written the program in C++ language, if you need the program in any other language, please let me know.

Below i have written the complete C++.

==================================================================

Program:

==================================================================

#include<iostream>

using namespace std;

//start of the main function

int main()

{

//variable declration

int num, fact = 1;

//validate that the input number is only positive number

do

{

cout<<"Enter a number: ";

cin>>num;

//if input number is negative throw a warning

if(num<0)

{

cout<<"Invalid input! please enter a positive number."<<endl;

}

}while(num<0);

int buf = num;

//if num is zero then factorial is 1

if(num == 0)

{

fact = 1;

}

//else find the factorial

else

{

while(num>0)

{

fact = fact * num;

num--;

}

}

//display the factorial

cout<<"Factorial of "<<buf<<" is = "<<fact<<endl;

return 0;

}

==================================================================

Sample Output:

nirmalsharma@ubuntu:~$ g++ fact.cpp nirmalsharma@ubuntu:~$ ./a.out Enter a number: -10 Invalid input! please enter a positive

==================================================================

Kindly Check and Verify Thanks...!!!

Add a comment
Know the answer?
Add Answer to:
Example 2: In this example we will simply find the factorial of a number which we...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a Factorial application that prompts the user for a number and then displays its factorial....

    Create a Factorial application that prompts the user for a number and then displays its factorial. The factorial of a number is the product of all the positive integers from 1 to the number. For example, 5! = 5*4*3*2*1. ( in Java)

  • USING PYTHON PLEASE Write a program that calculates the factorial value of a number entered by...

    USING PYTHON PLEASE Write a program that calculates the factorial value of a number entered by the user. Remember that x! =x* (x-1)* (x-2)*... *3+ 2* 1. Your program should check the value input by the user to ensure it is valid (i.e., that it is a number > =1). To do this, consider looking at the is digit() function available in Python. If the user enters an incorrect input, your program should continue to ask them to enter a...

  • 383 ng Assignments FACTORIAL CALCULATOR document and a Use Case Definition document and design a ...

    383 ng Assignments FACTORIAL CALCULATOR document and a Use Case Definition document and design a Windows application based on project shown in Figure 6-122 calculators have an operation called a factorial," which is shown on a calculator key as an idamation point. For example, 5! (5 factorial) multiplies 5 43'21 to calculate the resuls f120. Request that the user select a number from 1 to 12 and display all the factorials up to the valuc, including that value. Using loops...

  • 1) Create a main() method with a switch statement that calls either a sum() OR factorial()...

    1) Create a main() method with a switch statement that calls either a sum() OR factorial() method, depending on what selection the user of the program makes - ask the user to enter a selection (with System.out.println()), create a Scanner then read the user's input with a call to Scanner next(), then call the appropriate method in a switch (the String that was read from the call to Scanner.next() should be what you use as your switch condition). 2) Create...

  • The language has to be in C program 1. (a) rite a function, int factorial (int...

    The language has to be in C program 1. (a) rite a function, int factorial (int n), which returns n (the factorial of n, i e. 1 x2 x3 x...xn.) (b) Using int factorial (int n) above, write a program to compute 2. We want to find one of the roots of a cubic equation given by which is between 0 and 1 by an iterative method. Modify the equation above to 1 x Start with an appropriate initial value...

  • C++ Here, we want to enforce proper delimiter rules in a string. For example, if a...

    C++ Here, we want to enforce proper delimiter rules in a string. For example, if a user types something like this at the console: "This is my string, and it is properly delimited: 1*((3 + 4)*8)". Notice how the open parentheses are properly matching with closed parenthesis. We want to enable a user to create a string using more than one type of delimiter to bracket information into "blocks." For example, A string may use braces {}, parentheses (), and...

  • ASSEMBLY LANGUAGE (Mars MIPS) Starting code: Factorial: #Factorial Recursive function subu $sp, $sp, 4 sw $ra,...

    ASSEMBLY LANGUAGE (Mars MIPS) Starting code: Factorial: #Factorial Recursive function subu $sp, $sp, 4 sw $ra, 4($sp) # save the return address on stack beqz $a0, terminate # test for termination subu $sp, $sp, 4 # do not terminate yet sw $a0, 4($sp) # save the parameter sub $a0, $a0, 1 # will call with a smaller argument jal Factorial # after the termination condition is reached these lines # will be executed lw $t0, 4($sp) # the argument I...

  • This is a c++ question we are not using namespace std at the top Subtraction +...

    This is a c++ question we are not using namespace std at the top Subtraction + Decision and Loop This is a twist on the previous exercise that will help you review loops and decision structures. You will again ask the user to enter two numbers. However, you will ALWAYS subtract the smaller number from the larger number to ensure that you never get a negative number for an answer. You do this by checking the numbers and switching them...

  • Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, al...

    Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, allow the user to try up to 10 times to guess the number, and tell the user if each guess is too high, too low, or correct. The actual game portion must be a function...you can use more than one function in your program. The amount...

  • A complex number is a number in the form of a + bi, where a and...

    A complex number is a number in the form of a + bi, where a and b are real numbers and i is the square root of negative 1. Design and create a class called Complex for representing complex numbers. This class should have two attributes, a and b, which represent the parts of the complex number. This class should overload the +,-,*,/,++ and -- operators (both prefix and suffix) to perform basic complex number calculations according to the following...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT