C++18: Function Examples
Design a function square() which will calculate and return the square of the number which is passed as arguments
#include <iostream>
using namespace std;//Value returning Functionint square(int n){ int sq = n*n; return(sq);}//Value not returning Functionvoid squ(int n){ int sq = n*n; cout<<"Square of "<<n<< " is "<<sq<<endl;
}main(){
int x =square(5); cout<<"Square is "<<x<<endl;
int z = 2*square(10); cout<<"z = "<<z<<endl;
squ(5); squ(10);}Output:
Square is 25
z = 200
Square of 5 is 25
Square of 10 is 1002. Design a two function volume and perimeter which() will calculate and print the result when L, B, H of rectangle are passes as arguments.
#include <iostream>
using namespace std;main(){
void volume(int l,int b,int h){
int vol = l*b*h;
cout<<"Volume is "<<vol<<endl;}void surfacearea(int l,int b,int h){ int sur = 2*(l*b + l*h + h*b);
cout<<"Surcace area is "<<sur<<endl;}
int main(){ int l,b,h;
cout<<"Enter Length , Breadth And Height of Rectangle: "<<endl; cin>>l>>b>>h;
volume(l,b,h);
surfacearea(l,b,h); return 0;}Output:
Enter Length , Breadth And Height of Rectangle:
20 10 30
Volume is 6000
Surcace area is 2200WAP to find the area and circumference of a circle.
#include <iostream>
using namespace std;void area(int r){ float a = 3.14*r*r; cout<<"Area of Circle is "<<a<<endl;}void circumference(int r){ float c = 2*3.14*r;
cout<<"Circumference of circle is "<<c<<endl;}
main(){
int r; cout<<"Enter radius of circle: "<<endl; cin>>r;
area(r); circumference(r);}Output:
Enter radius of circle:
20
Area of Circle is 1256
Circumference of circle is 125.6WAP to find area and perimeter of rectangle using void functions.
#include <iostream>
using namespace std;void area(int l, int b){ int ar = l * b; cout << "Area of circle is " << ar << endl;}void perimeter(int l, int b){ int p = 2 * (l + b);
cout << "Perimeter of Rectangle is " << p << endl;}
int main(){ int l, b;
cout << "Enter length and breadth of Rectangle: " << endl; cin >> l >> b;
area(l, b); perimeter(l, b);
return 0;}Output:
Enter length and breadth of Rectangle:
5 10
Area of circle is 50
Perimeter of Rectangle is 30Value returning Functions:
WAP to find volume and surface area of cuboid using value returning functions.
#include <iostream>
using namespace std;int volume(int l,int b,int h){ int vol = l*b*h; return(vol);}int surfacearea(int l,int b,int h){ int sur = 2*(l*b + l*h + h*b);
return(sur);}
int main(){ int l,b,h; int v,s; cout<<"Enter the length, breadth and height of cuboid: "<<endl; cin>>l>>b>>h;
v=volume(l,b,h); cout<<"Volume is "<<v<<endl;
s=surfacearea(l,b,h);
cout<<"Surface area is "<<s<<endl;
return 0;}Output:
Enter the length, breadth and height of cuboid:
34 23 34
Volume is 26588
Surface area is 5440WAP to find out area and perimeter of a rectangle using value returning functions.
#include <iostream>
using namespace std;int area(int l,int b){ int ar = l*b; return(ar);}int perimeter(int l,int b){ int p = 2*(l+b);
return(p);}
int main(){ int l,b; int a,p; cout<<"Enter the length and breadth of rectangle: "; cin>>l>>b;
a=area(l,b); cout<<"The area of rectangle is "<<a<<endl; p= perimeter(l,b); cout<<"The perimeter of rectangle is "<<p<<endl; return 0;}Output:
Enter the length and breadth of rectangle: 5 8
The area of rectangle is 40
The perimeter of rectangle is 26WAP to find out factorial using value returning functions.
#include <iostream>
using namespace std;int fact(int n){ int fact,temp;
for (int i = 0; i < n; i++) { temp = i*n; fact += temp;
} return(fact);}int main(){ int n,f;
cout<<"Enter a Number : "; cin>>n; f=fact(4);
cout<<"Factorial of "<<n<<" is "<<f<<endl; return 0;}Output:
Enter a Number : 5
Factorial of 5 is 50WAP to create sum function returning sum of three num and mean function returning mean of the three digit.
#include <iostream>
using namespace std;int sum(int a,int b,int c){ return(a+b+c); cout<<a+b+c;}float mean(int p,int q,int r){
float m=sum(p,q,r)/3;
return(m);
} int main(){ int a,b,c; int sm , me; cout<<"Enter three numbers: "; cin>>a>>b>>c;
sm = sum(a,b,c);
cout<<"Sum of entered numbers is : "<<sm<<endl; me = mean(a,b,c);
cout<<"Mean of entered numbers is: "<<me<<endl;
return 0;}Output:
Enter three numbers: 5 8 12
Sum of entered numbers is : 25
Mean of entered numbers is: 8WAP to create two functions is_Even and is_false returning bool as per condtion.
#include <iostream>
using namespace std;int is_Even(int n){ if (n % 2 == 0) return true; else return false;}int is_Odd(int n){ if (!n % 2 == 0) return true; else return false;}
int main(){ int n;
cout << "Enter a number :"; cin >> n;
cout << "is Even: " << is_Even(n) << endl; cout << "is Odd : " << is_Odd(n) << endl; return 0;}Output:
Enter a number :5
is Even: 0
is Odd : 1Comment if you have any questions!
Comments
Post a Comment