Functions Exercise

Programming Exercise

Functions Programming Exercise P-5

Programming Exercise Looping Structure P-12

Q.10 Write a program using function that inputs a decimal number and converts it into binary digit.

#include<iostream.h>
#include<conio.h>
main()
{
void dec_bin(int);
int n;
clrscr();
cout<<"Enter an integer number ? ";
cin>>n;
dec_bin(n);
getch();
}
// definition of dec_bin() function

void dec_bin(int x)
{
int r;
cout<<"(";
while(x!=0)
{
r = x % 2;
cout<<r;
x = x / 2;
}
cout<<")2";
}

Q.11 Write a program using function that inputs a binary number and converts it into decimal number.

#include<iostream.h> 
#include<conio.h> 
#include<math.h> 
main()
{
int binary_decimal(int);
int b, d;
clrscr();
cout<<"Enter a binary number ? ";
cin>>b;
d = binary_decimal(b);
cout<<"binary number "<<b <<" in decimal is :"<<d;
getch();
}

// Function to convert binary to decimal.

int binary_decimal(int n) 
{
int decimal=0, i=0, rem;
while (n!=0)
{
rem = n%10;
n/=10;
decimal += rem*pow(2,i);
++i;
}
return decimal;
}

Q.12 Write a program that inputs a whole number and displays its predecessor and successor numbers using function.
// Hint: The predecessor of a whole number is one less than the given number.
// For example, the predecessor of 1 is 0; predecessor of 2 is 1; predecessor of 3 is 2 and so on. The whole number 0 does not have any predecessor.
// The successor of a whole number is the number obtained by adding 1 to it.
// For example, the successor of 0 is 1; successor of 1 is 2; successor of 2 is 3 and so on.
// We observe that every whole number has its successor.

#include<iostream.h> 
#include<conio.h> 
main()
{
void pre_suc(int);
int n;
clrscr();
cout<<"Enter a whole number ? ";
cin>>n;
pre_suc(n);
getch();
}

// Function to display predecessor and successor of a number

void pre_suc(int x) 
{
if (x == 0)
cout<<"The value 0 does not have any predecessor"<<endl;
else
cout<<"Predecessor of "<<x<<" is :"<<x-1<<endl;
cout<<"Successor of "<<x<<" is :"<<x+1<<endl;
}

Q.13 Write a program that inputs an integer in the main() function and passes this value to a function. The function should calculate the square of the integer and return the result to the main() function.

#include<iostream.h> 
#include<conio.h> 
main()
{
int square(int);
int a, s ;
clrscr();
cout<<"Enter first integer value ? ";
cin>>a;
s = square(a);
cout<<"Square of " <<a<< " is : "<<s;
getch();
}

// Function to calculate square of a number

int square(int n) 
{
return n * n;
}

Q.14 Write a program that inputs two integers in the main() function. It uses two functions: first function calculates the square of first integer and second function calculates the cube of second integer. The results of these two functions should return to the main() function. The main() function should add both these results or returned values and display on the screen.

#include<iostream.h> 
#include<conio.h> 
main()
{
int square(int), cube(int);
int a, b, s ;
clrscr();
cout<<"Enter first integer value ? ";
cin>>a;
cout<<"Enter second integer value ? ";
cin>>b;
s = square(a) + cube(b);
cout<<"Result : "<<s;
getch();
}

// Function to calculate square of a number

int square(int n) 
{
return n * n;
}

// Function to calculate cube of a number

int cube(int n) 
{
return n * n * n;
}

Q.15 Write a complete program using function. The function should take height of a student in centimeters as argument and return its height in inches.

#include<iostream.h> 
#include<conio.h> 
main()
{
float cm_inch(int);
int h;
clrscr();
cout<<"Enter height of a student in centimeters ? ";
cin>>h;
cout<<"Height of student in inches is : "<<cm_inch(h);
getch();
}

// Function to convert centimenters into inches

float cm_inch(int n) 
{
return n/2.54;
}

Q.16 Write a program that calculates the sum of the following series using function. The main() function should input a number and pass it to a function. The user-defined function should calculate the sum from 1 to the given number and return the result to the main() function.
// 1 + 1/1! + 1/2! +1/3! + .. . . . . + 1/n!
// Input the value of ā€˜nā€™ from user.

#include<iostream.h>
#include<conio.h>
main()
{
int n;
double sum = 1; 
unsigned long fact(int);
clrscr();
cout<<"Enter the value of n : ";
cin>>n;

for(int i = 1; i<=n; i++)
sum = sum + 1.0/fact(i); 
cout<<"Sum of series is : "<<sum;
getch();
}

// definition of the fact() function


unsigned long fact(int x)
{
long f = 1, c = 1;

while(c<=x)
{
f = f * c;
c = c + 1;
}
return f;
}

Leave a Reply