Ask Question
22 January, 08:14

Write a program that lets the user enter a nonnegative integer then uses a loop to calculate the factorial of that number. Display the factorial.

+2
Answers (1)
  1. 22 January, 08:29
    0
    Following are the program in c+ + language

    #include / / header file

    using namespace std; / / namespace

    int main () / / main function

    {

    int num; / / variable declaration

    long int f=1; / / variable declaration

    do

    {

    cout<<"Enter the Positive value:";

    cin>>num;

    } while (num<0); / / i check whether number is non negative or not

    while (num>0) / / iterating over the loop

    {

    f=f*num; / / calculate the factorial

    num--; / / decrement the value of num by 1

    }

    cout<<" factorial is : "<
    return 0;

    }

    Output:

    Enter the Positive value:7

    factorial is : 5040

    Explanation:

    Following are the description of the program.

    Read the input by user in the "num" variable of "int" type ... The do-while will check the enter number is nonnegative number. While (n>0) loop is calculating the factorial in the "f" variable. Finally display the factorial.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a program that lets the user enter a nonnegative integer then uses a loop to calculate the factorial of that number. Display the ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers