4. Write C++ Program to find factorial
of given number?
Answers
Answered by
2
Answer:
#include <iostream>
using namespace std;
int main()
{
int i,fact=1,number;
cout<<"Enter any Number: ";
cin>>number;
for(i=1;i<=number;i++){
fact=fact*i;
}
cout<<"Factorial of " <<number<<" is: "<<fact<<endl;
return 0;
}
Explanation:
Output:
Enter any Number: 5
Factorial of 5 is: 120
Answered by
1
The given code is written in C++
#include <iostream>
using namespace std;
int main(){
int n,fact=1,i;
cout<<"Enter a number: ";
cin>>n;
for(i=2;i<=n;i++)
fact*=i;
cout<<"Factorial of "<<n<<" is: "<<fact;
return 0;
}
Logic:
- Ask the user to enter the number, n.
- Run for loop in the range 2 to n.
- Multiply each value in the range and store the result in factorial variable.
- Display the value of the 'factorial' variable.
Attachments:
Similar questions