// Enter your name as a comment for program identification // Program assignment
ID: 3564413 • Letter: #
Question
// Enter your name as a comment for program identification // Program assignment Factorial.cpp
// Enter your class section, and time /* The program Factorial1.cpp is a program that prompts the user to enter a positive integer number. The program will then use a for loop to calculate and display the factorial value of that number. This program increments the counter in the for loop. A factorial number is a number multiplied by every factor between 1 and the number inclusive. For instance 3 factorial is 3 * 2 * 1. If the user enters a negative number, display a message indicating that the program calculates only positive numbers. Otherwise, display the result of the calculation. /* The input is a positive integer number. */ /* The factorial value of the number entered or a message indicating that the number is not positive. */ // header files
/* use the correct preprocessor directives for input/output */ // declare variables /* declare an integer variable number to input a value and a integer variable factorial initialized to 1 to calculate the factorial value of the number entered */
// prompt the user for a positive value // read in the number // check if the number entered is greater than 0. Use if statement. //calculate factorial. Use for loop. /* initialize the loop counter to number. Decrease the loop counter by 1 each loop iteration */ // display the value of the number and the factorial value. /* if number is less than 1, display a message that only numbers greater than 0 are calculated */
1. Use the following memory table to show what occurs in memory when the C++ code is executed. To fill out the memory table, use one or two lines for each variable. On one line, enter declaration information. Write the name of the declared variable, its data type, and the line number at declaration. Variable name| Data type | Value in memory | Line number at declaration| Line number when initialized
Explanation / Answer
#include<iostream>
int main()
{
int num,factorial=1;
cout<<" Enter Number To Find Its Factorial: ";
cin>>num;
if (num < 0)
{
cout<<"Factorial is calculated for positive numbers only";
}
for(int a=1;a<=num;a++)
{
factorial=factorial*a;
}
cout<<"Factorial of Given Number is ="<<factorial<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.