14. Program that inputs a number from the user and displays its factorial. It asks the user whether he wants to calculate another factorial or not.
Chapter 6:
Looping Structures
Programming Exercise
Problem # 14:
Write a program that inputs a number from the user and displays its factorial. It asks the user whether he wants to calculate another factorial or not. If the user inputs 1, it again inputs number and calculates factorial. If user inputs 0, program terminates.
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
long int n, c, f, num;
do
{
cout<<endl<<"Enter a number:";
cin>>n;
c=f=1;
while(c<=n)
{
f=f*c;
c++;
}
cout<<"Factorial of "<<n<<" is:"<<f;
cout<<endl<<"if you want to calculate another factorial input 1 and if not input 0.";
cin>>num;
}
while(num==1);
return 0;
}
#include<conio.h>
using namespace std;
int main()
{
long int n, c, f, num;
do
{
cout<<endl<<"Enter a number:";
cin>>n;
c=f=1;
while(c<=n)
{
f=f*c;
c++;
}
cout<<"Factorial of "<<n<<" is:"<<f;
cout<<endl<<"if you want to calculate another factorial input 1 and if not input 0.";
cin>>num;
}
while(num==1);
return 0;
}
Let me know in the comment section if you have any question.
Previous Post:
13. Program that inputs a number from the user and displays all prime numbers which are less that the input number using any loop.
Comments
Post a Comment