factorial with structures using pass by value and pass by reference
factorial with structures using pass by value and pass by reference.
Source code of factorial with structures in C++ by using pass by value
Following concepts are used in this program
- structure = For example “factorial”.
- Pass by value= For example “display(f); | void display( factorial fact )”.
- for loop = For example “for(int i=1;i<=f.num;i++)”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
//factorial of number by using pass by value #include<iostream> using namespace std; struct factorial { int fac; int num; }; void display( factorial fact ) { cout<<"factorial of a number"<<" "<<fact.num<<" is"<<" "<<fact.fac; }int main() { struct factorial f; f.fac=1; f.num=6; for(int i=1;i<=f.num;i++) { f.fac=f.fac*i; } display(f); return 0; } |
Output
factorial of a number 6 is 720
Source code of factorial with structures in C++ by using pass by reference
Following concepts are used in this program
- structure = For example “factorial”.
- Pass by reference= For example “display(&f); | void display(struct factorial *fact )”.
- for loop = For example “for(int i=1;i<=f.num;i++)”.
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 |
//factorial of number by using pass by reference
#include<iostream> using namespace std; struct factorial { int fac; int num; }; void display(struct factorial *fact ) { cout<<"Display Result"<<endl; cout<<"factorial of a number"<<" "<<fact->num<<"is"<<" "<<fact->fac; }int main() { struct factorial f; f.fac=1; f.num=5; for(int i=1;i<=f.num;i++) { f.fac=f.fac*i; } display(&f); return 0; } |
Output
Display Result
factorial of a number 5 is 120