Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that accepts a positive long integer between 2 to 15, and t

ID: 3695405 • Letter: W

Question

Write a Java program that accepts a positive long integer between 2 to 15, and then using the number, calculate its factorial (n!). Using do-while statement, make sure it only accepts a valid number (between 2 and 15) – if an invalid number is entered, program should display the original instruction again, using while loop. You can submit a source code only - .java file. Hint: all variables should be declared as long. Hint: After do-while loop for input validation, use for-loop to get the factorial *** Sample output Enter a between number 2 and 15: 15 Factorial of 15 is 1307674368000

Explanation / Answer

I think we should use || operator insted of && coz the '&&' inside the do-while loopt will terminate the loop if we input number greater than 15.

This might help you.

#include <iostream>
using namespace std;
int main () {

int number=1;//Initialize the value of 'number' either <=2 or >=15
int i;
long fact=1; //Declare fact as long since it has to hold bigger values;
do
{
if(number>=3 && number<=14)
break;
cout<<"Enter a number between 2 and 15 ";
cin>>number;
}
while(number<=2 || number>=15);
for(i=number;i>=1;i--)
fact*=i;
cout<<"The factorial of "<<number<<" is "<<fact;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote