Write a program to calculate simple interest using function , square using function , area of circle , even-odd logic

इसे पहले article मे , user define function को discuss किया गया है | अब इस article मे यूजर define function के कुछ उदाहरनो को discuss करेगे जिससे अप सभी के user define function के concept को अच्छी तरह से समज सके |उदाहरण 1 :
Write a program to calculate simple interest using function .
इस उदाहरण मे , user define function से simple intrest को calculate करने के लिए function को बनाया गया है |

Expalnation
1.सबसे पहले header feild मे int SimpleIntrest(int ,int ,int ) को declare किया गया है | इस declartion से ये define होता है की function SimpleIntrest() से एक value return होती है जिसका त्य्पोए integer है | और इस function मे pass argument की सख्या ‘3’ है और type integer है |
2.उसके बाद main() function मे तीन variable principal , rate , time को declare किया गया है इसमें यूजर द्वारा input की गयी value को assign किया गया है |
3.यूजर द्वारा इन value को input करा लेते है और इन value को  variable principal , rate , time मे assign करा देते है |
4.उसके बाद एक और variable intrest को declare करते है | जिसमे function SimpleIntrest() के द्वार return की value को असिग्न किया जाता है |
5.उसके बाद  intrest की value को display कर देते है |

SimpleIntrest() मे ,
1.सबसे पहले function definition मे SimpleIntrest(int p ,int r, int t) को define किया जाता है | यह पर ‘p’ ‘r’ ‘t’ formal parameter है जिसमे actual parameter principal , rate , time से intial किया जाता है |
2.उसके बाद si= p*r*t ; से simple intrest को calculate कर लेते है |
3.return statement से si की value को main () मे pass कर देते है |

Source Code
#include <iostream>
#include<conio.h>
using namespace std;
// Function prototype (declaration)
int SimpleIntrest(int, int , int );
void  main()
{
int  principal , rate , time ;
cout<<“Enters principal : “;
cin >> principal;
cout<<“Enters rate : “;
cin >> rate;
cout<<“Enters time : “;
cin >> time;
// Function call
float intrest = SimpleIntrest(principal , rate , time);
cout<<“Simple Intrest :”<<intrest;
getch();
}
// Function definition
int SimpleIntrest ( int p ,int r, int t )
{
float si;
si = p*r*t / 100;
return si ;
}

इसका आउटपुट होगा :
Enters principal : 1000
Enters rate : 10
Enters time : 1
Simple Intrest : 100

उदाहरन 2 :
Write a program to calculate square using function .
इस उदाहरण मे , user define function से square को calculate करने के लिए function को बनाया गया है |

Expalnation
1.सबसे पहले header feild मे int square(int) को declare किया गया है | इस declartion से ये define होता है की function square() से एक value return होती है जिसका  type integer है | और इस function मे pass argument की सख्या ‘1’ है और type integer है |
2.उसके बाद main() function मे एक variable number को declare किया गया है इसमें यूजर द्वारा input की गयी value को assign किया गया है |
3.यूजर द्वारा  value को input करा लेते है और इन value को  variable number मे assign करा देते है |
4.उसके बाद एक और variable ‘o’ को declare करते है | जिसमे function square() के द्वार return की value को assign किया जाता है |
5.उसके बाद ‘o’ की value को display कर देते है |

SimpleIntrest() मे ,
1.सबसे पहले function definition मे square(int n ) को define किया जाता है | यह पर ‘n’ formal parameter है जिसमे actual parameter number से intial किया जाता है |
2.उसके बाद output = n*n ; से square को calculate कर लेते है |
3.return statement से output की value को main () मे pass कर देते है |

Source Code
#include <iostream>
#include<conio.h>
using namespace std;
// Function prototype (declaration)
int square(int);
void  main()
{
int  number ;
cout<<“Enter number : “;
cin >> number;
// Function call
int o  = square(number);
cout<<“Square of:”<<number << “is”<<intrest;
getch();
}
// Function definition
int square ( int n)
{
int output;
output = n*n;
return output ;
}

इसका आउटपुट होगा :
Enters number : 10
Square of 10 is 100

उदाहरन 2 :
Write a program to calculate area of circle using function .
इस उदाहरण मे , user define function से area of circle को calculate करने के लिए function को बनाया गया है |

Expalnation
1.सबसे पहले header feild मे int area(int) को declare किया गया है | इस declartion से ये define होता है की function area() से एक value return होती है जिसका  type integer है | और इस function मे pass argument की सख्या ‘1’ है और type integer है |
2.उसके बाद main() function मे एक variable radius को declare किया गया है इसमें यूजर द्वारा input की गयी value को assign किया गया है |
3.यूजर द्वारा  value को input करा लेते है और इन value को  variable radius मे assign करा देते है |
4.उसके बाद एक और variable ‘area’ को declare करते है | जिसमे function area () के द्वार return की value को assign किया जाता है |
5.उसके बाद ‘area ‘ की value को display कर देते है |

area () मे ,
1.सबसे पहले function definition मे area (int r ) को define किया जाता है | यह पर ‘r’ formal parameter है जिसमे actual parameter radius से intial किया जाता है |
2.उसके बाद output = 3.14 *r*r ; से  area को calculate कर लेते है |
3.return statement से output की value को main () मे pass कर देते है |

Source Code
#include <iostream>
#include<conio.h>
using namespace std;
// Function prototype (declaration)
int area (int);
void  main()
{
int  radius  ;
cout<<“Enter Radius : “;
cin >> radius ;
// Function call
float a  = area (radius );
cout<<“Area of Circle :”<< a ;
getch();
}
// Function definition
int area  ( int r)
{
float  output;
output = 3.14*r*r;
return output ;
}

इसका आउटपुट होगा :
Enters Radius : 7
Area of Circle : 154

उदाहरन 1 :
Write a program to calculate even-odd logic using function .
इस उदाहरण मे , user define function से even-odd logic को calculate करने के लिए function को बनाया गया है |

Explanation
1.सबसे पहले header feild मे int check(int) को declare किया गया है | इस declartion से ये define होता है की function check () से एक value return होती है जिसका  type integer है | और इस function मे pass argument की सख्या ‘1’ है और type integer है |
2.उसके बाद main() function मे एक variable number को declare किया गया है इसमें यूजर द्वारा input की गयी value को assign किया गया है |
3.यूजर द्वारा  value को input करा लेते है और इन value को  variable number मे assign करा देते है |
4.उसके बाद एक और variable ‘ flag’ को declare करते है | जिसमे function check () के द्वार return की logic value को assign किया जाता है |
5.उसके बाद flag की value को check किया जाता ही |अगर flag की value ‘1’ होती है तब number is even का message display हो जाता है | अन्यथा number is odd का message print हो जाता है |

check () मे ,
1.सबसे पहले function definition मे  check (int n) को define किया जाता है | यह पर ‘n’ formal parameter है जिसमे actual parameter number से intial किया जाता है |
2.उसके बाद even-odd के लॉजिक को check किया जाता है | अगर variable ‘n’ , 2 से divide होता है तब function से ‘1’ return होता है |
अन्यथा function से ‘0’ return होती है |

Source Code
#include <iostream>
#include<conio.h>
using namespace std;
// Function prototype (declaration)
int check(int);
void  main()
{
int  number ;
cout<<“Enter number : “;
cin >> number;
// Function call
int flag  = check(number);
if(flag == 1)
{
cout<<“Number is even. “;
}
else
{
cout<<“Number is odd. “;
}
getch();
}
// Function definition
int check ( int n)
{
if(n%2 == 0)
{
return 1;
}
else
{
return 0;
}
}

इसका आउटपुट होगा :
Enters number : 10
Square of 10 is 100)