Qs 3
Qs 3
Qs 3
Qs 1.Write two versions of a function ‘sum‘ that sums its two integer
parameters int sum( int x int y) and void sum(int x ,,int y) Call thse functions
in the main and display the output.What is the difference In calling of these
two functions in the main.
Answer:
#include<iostream>
return x+y;
cout<<”Sum is:”<<x+y<<endl;
int main()
sum(2,3);
return 0;
Output:
Sum is:5
Qs 3. Write two version of a function that multiply the next higher values of
its two integer parameters int prod(int x, int y) ( parameters passed by value ,
result returned after changing x to x+1 and y to y+1 within the function) and
int prod(int &x,int &y)(parameters passed by reference carrying out the same
multiplicatiojn as earlier).Call these function in th meain and disoplay the
output,Demonstarte the difference in the vlue of parmeters after calling of
these two function in the main.
Answer
#include <iostream>
x=x+1;
y=y+1;
return x*y;
x=x+1;
y=y+1;
return x*y;
int main()
int x=1,y=2;
cout<<”Calling by value:”<<prod(x,y);
cout<<”Calling by reference:”<<prod1(x,y);
return 0;
Output
Calling by value:2
Values after call by value: x=1 y=2
Calling by reference:2
Answer
#include<iostream>
return length*height;
return length*height;
{
cout<<”Area by void rectangle_
=”<< length*height<<endl;
int main()
rectangle_area(10.0,1);
return 0;
Output:
Answer:
#include<iostream>
return x1*x2+y1*y2;
return x1*x2+y1*y2+z1*z2;
Answer
Example:
#include<iostream>
return x+y;
int main()
add(x,y);
return 0;
Had inline not been used , this would have resulted in lot of jumping from
main to add , but after the addition of inline this problem has been solved as
the jumpes would not be required during function call.
Qs 7. Write inline functions square_ and cube_ which take int as parameters.
These functions return square and cube of integer respectively.Also write
function square_of_integer and cube_of_integer which take integer as
parameters and return square and cube of integer to main
respectively.Understand how these functions are called in the main.
Answer
#include<iostream>
return x*x;
return x*x*x;
int square_of_integer(int x)
return x*x;
int cube_of_integer(int x)
return x*x*x;
int main()
cout<<square_(1)<<endl;
cout<<cube_(2)<<endl;
cout<<square_of_integer(2)<<endl;
cout<<cube_of_integer(3)<<endl;
return 0;
Output
27
Answer:
1. All variables right to the first variable having default value must have
default values.
return c*c;
Answer
#include<iostream>
return l*b;
}
int main()
cout<<rectangle_area();
cout<<endl<<rectangle_area(10,5);
return 0;
Answer:
#include<iostream>
if(b==0)
return a;
return gcd(b,a%b);
int main()
{
cout<<”GCD of 15 and 20 is ”<<gcd(15,20)<<endl;
return 0;
Output:
GCD of 15 and 20 is 5
0! = 0
l! = I
Answer:
#include<iostream>
int factorial(int n)
if (n<0)
cout<<”Negative no entered"<<endl;
return -1;
if(n==0)
return 1;
return n*factorial(n-1);
int main()
for(int i=0;i<6;i++)
cout<<string(‘ ‘,10)<<i<<”!=”<<factorial(i)<<endl;
return 0;
Output:
0!=1
1!=1
2!=2
3!=6
4!=24
5!=120
Question 12.Write a function ‘sumDiff' which either adds two integers and
returns the sum or subtract: the two integers and returns the difference. The
function takes a bool parameter to decide action of summing or subtracting.
Write main to test the function.
Answer:
#include<iostream>
if(t==1)
return a+b;
return a-b;
int main()
return 0;
Output
Sum of 5 and 9 is 14