Create a C++ Program that prompts the user to enter a number and compute for the factorial value.
SAMPLE OUTPUT:
ENTER A NUMBER: -5
zero & positive numbers only!!!
ENTER A NUMBER: 5
Factorial of 5 : 5*4*3*2*1 = 120
DO NOT USE FUNCTION FOR THIS PROBLEM. THANK YOU
//c++ program for factorial
#include<iostream>
using namespace std;
int main()
{
int n,i;
unsigned long long factorial = 1;
cout << "ENTER A NUMBER : ";
cin >> n;
if(n==0) //if n==0 printing 1
{
cout << "Factorial of " << n << " =
" << factorial;
}
else if(n>0)
{
for(i = 1; i<=n; ++i)
{
factorial=factorial*i; //calculating factorial
}
cout << "Factorial of " << n << " = " <<
factorial;
}
else
cout<<"zero & positive numbers
only!!!";
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.