Functions Exercise 

Programming Exercise

Functions Programming Exercise P-7

What is big data and hadoop?

Q.24 Write a program that uses a function Multiple(int, int) to determine whether the second integer is a multiple of the first. The function should take two integer arguments and return TRUE( or 1) if second is a multiple of the first and FALSE ( or 0) otherwise.

#include<iostream.h>
#include<conio.h>
main()
{
int Multiple(int, int), a, b, res;
clrscr();
cout<<"Enter first number ? ";
cin>>a;
cout<<"Enter second number ? ";
cin>>b;
res = Multiple(a, b);
if(res == 0)
cout<<"First number is a multiple of second ";

else
cout<<"First number is not a multiple of second ";
getch();
}


// definition of Multiple() function

int Multiple(int x, int y)
{
return x % y;
}

Q.25 Write a program that inputs an integer value and character in the main() function and passes them to a user-defined function. The function displays a square of input character. For example, if user enters 4 and ‘#’, the function displays the square of character ‘#’ as under:
// # # # #
// # # # #
// # # # #
// # # # #

#include<iostream.h>
#include<conio.h>
main()
{
void ch_square(int, char);
int n;
char ch;
clrscr();
cout<<"Enter a number ? ";
cin>>n;
cout<<"Enter a character ? ";
cin>>ch;
ch_square(n, ch);
getch();
}


// definition of ch_square() function

void ch_square(int x, char c)
{
int i, j;
for(i = 1;i<=x; i++)
{
for(j = 1;j<=x; j++)
cout<<c;
cout<<endl;
}
}

Q.26 Write a program that prompts the user to enter a number and then reverses it. Write a function Reverse to reverse the number. For example, if the user enters 2765, the function should reverse it so that it becomes 5672. The function should accept the number as an input parameter and return the reversed number.

#include<iostream.h>
#include<conio.h>
main()
{
long Reverse(long);
long n;
clrscr();
cout<<"Enter an integer value ? ";
cin>>n;
cout<<"The reversed number is : "<<Reverse(n);
getch();
}


// definition of Reverse() function

long Reverse(long x)
{
long r, rev_no = 0;
while(x!=0)
{
r = x % 10;
rev_no = (rev_no*10) + r;
x = x/10;
}
return rev_no;
}

Q.27 Write a program that inputs an integer and passes it to a function. The function should return the number of digits in the integer. For example, if the integer is 68, function should return 2 and if it is 22356, function should return 5.

#include<iostream.h>
#include<conio.h>
main()
{
int digits(int);
long n;
clrscr();
cout<<"Enter an integer value ? ";
cin>>n;
cout<<"Number of digits : "<<digits(n);
getch();
}
// definition of digits() function

int digits(int x)
{

int i = 0;
while(x!=0)
{
x = x /10;
i++;
}

return i;
}

Q.28 Write a function BalerAvg() that calculates and displays the average of player (Baler). Call this function in the main() function. Take input of runs given and ball delivered from the user in the main() function. The average may be calculated by formula:
// Average =(Total runs given *60) /(Total number of balls delivered)

#include<iostream.h>
#include<conio.h>
main()
{
double BalerAvg(int, int);
int ball, run;
clrscr();
cout<<"Enter total runs given ? ";
cin>>run;
cout<<"Enter number of balls delivered ? ";
cin>>ball;
cout<<"The average of the player is :"<<BalerAvg(run, ball);
getch();

}
// definition of BalerAvg() function

double BalerAvg(int r, int b)
{
return r*60/b;
}

Q.29 Write a function pow() that takes a double value for ‘n’ and an integer value for ‘p’. It finds the power ‘p’ of the ‘n’. The default value for ‘p’ should be 2 so that if no value for ‘p’ is given, it should give square of ‘n’. The values should be passed from the main() function.

#include<iostream.h>
#include<conio.h>
main()
{
double pow(double, int = 2), n;
int p;
clrscr();
cout<<"Enter an integer value ? ";
cin>>n;
cout<<"Enter value for power ? ";
cin>>p;
cout<<"The result is :"<<pow(n, p);
getch();

}
// definition of pow() function

double pow(double x, int y)
{

double res =1;
int c = 1;
while(c<=y)
{
res = res * x;
c=c+1;
}

return res;
}

 

Leave a Reply