Site icon T4Tutorials.com

Factorial of a number by using user-defined functions and structure C++

Factorial of a number by using user-defined functions and structures.

Following concepts are used in this program

structure

For example “factorial”.

function definition

For example;

void print_factorial(factorial f)
 {
 	cout<<"Display result"<<endl;
 		cout<<"f actorial of a number"<<f.num<<" "<<"is="<<f.fac;
 }

function declaration

For example; “void print_factorial( factorial );”.

function Calling

For example;

print_factorial( f);

for loop

For example;

for(int i=1;i<=f.num;i++)

Source code of structures and user define functions of Factorial of a number

//Factorial of a number user define
#include<iostream>
using namespace std;
struct factorial
{
	int fac=1,num;
};
 void print_factorial( factorial );
 int main()
 {
 	factorial  f;
 	cout<<"Enter a number:";
 	cin>>f.num;
 	for(int i=1;i<=f.num;i++)
 	{
 		f.fac=f.fac*i;
 	}
 		print_factorial( f);
	 
 	return 0;
 }
void print_factorial(factorial f)
 {
 	cout<<"Display result"<<endl;
 		cout<<"factorial of a number"<<f.num<<" "<<"is="<<f.fac;
 }

 

Exit mobile version