Computer Science, asked by patilpriya16767, 7 hours ago

4. Write C++ Program to find factorial
of given number?​

Answers

Answered by yendavasudhagmailcom
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 anindyaadhikari13
1

\texttt{\textsf{\large{\underline{Solution}:}}}

The given co‎de 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