Factorial of a number by using array within the structure in C++.
Source code of Factorial of a number by using array within the structure in C++
Following concepts are used in this program
- structure = For example “factorial”.
- array within the structure= For example “struct factorial { int num[2]; }; “.
- for loop = For example;
- for(int i=0;i<=1;i++)
- for( int j=1;j<=fac.num[i];j++)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | //factorial of a number by using Array within structure #include<iostream> using namespace std; struct factorial { int num[2]; }; int main() { for(int i=0;i<=1;i++) { int f=1; factorial fac; cout<<"Enter a number:"<<endl; cin>>fac.num[i]; for( int j=1;j<=fac.num[i];j++) { f=f*j; } cout<<"factorial of a number"<<" "<<"is="<<f<<"\n"; } return 0; } |
Output
Enter a number:
7
factorial of a number is= 5040
Enter a number: