Functions Exercise

Programming Exercise

Functions Programming Exercise P-8

Conditional Structure Programming Exercise P-8

Q.30 Write a program using function to pass a value to function as argument. The function should test the number and print a message as:
// Print “Positive number” if number is greater than zero
// Print “Negative number” if number is less than zero
// Print “Equal to 0” if number is equal to zero

#include<iostream.h>
#include<conio.h>
main()
{
void number(int);
int n;
clrscr();
cout<<"Enter an integer value ? ";
cin>>n;
number(n);
getch();

}
// definition of number() function

void number(int num) 
{
if(num > 0)
cout<<"Positive number";
else if(num < 0)
cout<<"Negative number";
else if(num == 0)
cout<<"Equal to zero";
}

Q.31 Write a program using function to compute the roots of a quadratic equation.
// ax2 + bx + c =0

#include<iostream.h>
#include<conio.h>
#include<math.h>
main()
{
double roots(float, float, float);
float x, y, z;
clrscr();
cout<<"Enter value of x ? ";
cin>>x;
cout<<"Enter value of y ? ";
cin>>y;
cout<<"Enter value of z ? ";
cin>>z;
roots(x, y, z);
getch();

}
// definition of roots() function

double roots(float a, float b, float c)
{
float d, root1,root2;
d=b*b-4*a*c;

if(d==0)
{
root1=(-b)/(2*a);
root2=root1;
cout<<"Roots are real & equal";
}
else if(d>0)
{
root1=-(b+sqrt(d))/(2*a);
root2=-(b-sqrt(d))/(2*a);
cout<<"Roots are real & distinct";
}
else
{
root1=(-b)/(2*a);
root2=sqrt(-d)/(2*a);
cout<<"Roots are imaginary";
}

cout<<"\nRoot 1= "<<root1<<"\nRoot 2= "<<root2;
}

Q.32 Write a program using function to evaluate the formula as given below and return the calculated result to the calling function.
// y = (x -1)/x + 1/2 *((x-1)/x)^2 + 1/3 *((x-1)/x)^3
// Input value of x and pass to the function as argument.

#include<iostream.h>
#include<conio.h>
#include<math.h>
main()
{
double eva_for(double), x;
clrscr();
cout<<"Enter value of x ? ";
cin>>x;
cout<<"Result = "<<eva_for(x);
getch();

}
// definition of eva_for() function

double eva_for(double x)
{
double y;
y = (x-1)/x + 1/2.0*(pow((x-1)/x,2)) + 1/3.0*(pow((x-1)/x,3));
return y;
}

Q.33 Write a program using inline function to find out the value of E of the formula given below and return the calculated result to the calling function.
// E = mc2

#include<iostream.h>
#include<conio.h>
#include<math.h>
main()
{
long eva_for(int, int);
int m, c, E;
clrscr();
cout<<"Enter value of m ? ";
cin>>m;
cout<<"Enter value of c ? ";
cin>>c;
E = eva_for(m, c);
cout<<"Result = "<<E;
getch();

}
// definition of eva_for() function

long eva_for(int x, int y) 
{
return x*pow(y,2);
}

Q.34 Define the following functions:
// ‘Celsius’ to convert temperature in Celsius to Fahrenheit.
// “Fahrenheit” to convert temperature in Fahrenheit to Celsius.
// Using the above functions in the program print charts showing the Fahrenheit equivalents of all Celsius temperatures from 0 to 50 degrees, and Celsius equivalents of all Fahrenheit temperatures from 32 to 82 degrees.

#include<iostream.h>
#include<conio.h>
main()
{
float Celsius(int), Fahrenheit(int);
clrscr();
for (int c= 0; c<=50; c++)
cout<<c<< " Celsius = "<<Celsius(c)<<" Fahrenheit"<<endl;
getch();
for (int f = 32; f<=82; f++)
cout<<f<< " Fahrenheit = "<<Fahrenheit(f)<<" Celsius"<<endl;

getch();

}
// definition of Celsius() function

float Celsius(int C)
{
return 9.0/5.0*C+32;
}

// definition of Fahrenheit() function

float Fahrenheit(int F) 
{
return 5.0/9.0*(F-32);
}

Q.35 Write a program that finds out the sum of three numbers using reference parameters.

#include<iostream.h>
#include<conio.h>
main()
{
void sum(int&, int&, int&, int&);
int a, b, c, s;
clrscr();
cout<<"Enter first value ? ";
cin>>a;
cout<<"Enter second value ? ";
cin>>b;
cout<<"Enter third value ? ";
cin>>c;
sum(a,b, c, s);
cout<<"Sum of three values : "<<s;
getch();
}

// definition of sum() function
void sum(int& x, int& y, int& z, int& rs)
{
rs = x + y+ z;
}

 

Leave a Reply