Factorial Program with structures and pointers C++.
Following concepts are used in this program
- structure = For example “factorial”
- Pointers = For example “*fac”
- for loop = For example “for(int i=1;i<=(*fac).num;i++)”
//factorial of number by using pointer
#include<iostream>
using namespace std;
struct factorial
{
int f=1;
int num;
};
int main()
{
factorial *fac,n;
fac =&n;
cout<<"Enter a number for factorial:";
cin>>(*fac).num;
for(int i=1;i<=(*fac).num;i++)
{
n.f=n.f*i;
}
cout<<"DISPLAY RESULT"<<endl;
cout<<"factorial of a number:"<<fac->num<<" "<< "is=" <<n.f;
return 0;
}
Output
Enter a number for factorial:
4
DISPLAY RESULT
factorial of a number: is= 24
More Practice on Factorial problem in C++
- Factorial Program in C++
- factorial using single inheritance
- Factorial Program in C++ using Class Objects
- factorial using Multiple inheritances
-
C++ program for factorial using Constructor DestructorFactorial Of A Number By Using The Recursion - Factorial Program with structures and pointers C++
- Factorial Program with Nested Structure C++
- factorial of a no. by defining the member functions outside the class