Functions Exercise

Programming Exercise

Functions Programming Exercise P-2

Why should we declare a variable in programming?

Program 16:
Write a program that inputs base and height of a triangle and calculates the area of that triangle by passing base and height as parameters to a function. The function returns the result to the invoking point (calling function).
//Formula to compute area of a triangle = ½ (base *height)

#include<iostream.h>
#include<conio.h>
main()
{
float area_triangle(int, int);
float ans;
int b, h;
clrscr();
cout<<"Enter base of triangle ? ";
cin>>b;
cout<<"Enter height of triangle ? ";
cin>>h;
ans = area_triangle(b, h);
cout<<"Area of triangle = "<<ans;
getch();
}

// definition of area_triangle() function

float area_triangle(int base, int height)
{
return 0.5*base*height;
}

Program 17:
Write a function Is_Prime that has an input parameter, i.e. num, and returns a value of 1 if num is prime. Otherwise returns a value of 0.

#include<iostream.h>
#include<conio.h>
main()
{
int Is_Prime(int);
int num;
clrscr();
cout<<"Enter any number ? ";
cin>>num;
Is_Prime(num)? cout<<"Yes": cout<<"No";
getch();
}

// definition of Is_Prime() function 
int Is_Prime(int n)
{
int d;
for(d=2; d<= n/2; d++)
if (n%d ==0)
return 0;
return 1;
}

Program 18:
Write a complete C++ program that inputs two numbers and then prompts the user to enter his/her choice. If the user enters 1, the numbers are added, for the choice of 2, the numbers are subtracted, for the choice of 3, the numbers are multiplied, for the choice of 4, the numbers are divided. Write four functions Add(), Subtract(), Multiply() and Divide() to complete the task.

#include<iostream.h>
#include<conio.h>
void main(void)
{
float Add(float, float);
float Subtract(float, float);
float Multiply(float, float);
float Divide(float, float);
float x, y;
int op;
clrscr();
cout<<"Enter first number ? ";
cin>>x;
cout<<"Enter second number ? ";
cin>>y;
cout<<"1- Add Numbers \n";
cout<<"2- Subtract Numbers \n";
cout<<"3- Multiply Numbers \n";
cout<<"4- Divide Numbers \n";
cout<<"Select your choice ? ";
cin>>op;
switch(op)
{
case 1: cout<<x<<"+"<<y<<"="<<Add(x, y);
break;
case 2: cout<<x<<"-"<<y<<"="<<Subtract(x, y);
break;
case 3: cout<<x<<"*"<<y<<"="<< Multiply(x, y);
break;
case 4: cout<<x<<"/"<<y<<"="<< Divide(x, y);
break;
default:
cout<<"Invalid Option";
}
getch();
}

// definitions of functions Add(), Subtract(), Multiply() & Divide()
float Add(float a, float b)
{
return a + b;
}

float Subtract(float a, float b)
{
return a - b;
}

float Multiply(float a, float b)
{
return a * b;
}

float Divide(float a, float b)
{
return a / b;
}

Program 19:
Write a complete program that calculates the sum of the following series using a function. The main() function should input a number and pass it to the function. The user-defined function should calculate the sum from 1 to the given number and return the result to main() function.
// 1 + 1! + 2! +3! + .. . . . . + n!
//Input the value of ‘n’ from the user.

#include<iostream.h>
#include<conio.h>
main()
{
unsigned long fact(int);
long sum, n, f;
clrscr();
cout<<"Enter value for n ? ";
cin>>n;
sum = f = 1;
for(int i = 1; i<=n; i++)
{
f = fact(i);
sum = sum + f; 
}
cout<<"Sum of series is ="<<sum;
getch();
}

unsigned long fact(int x)
{
unsigned long fac = 1;
for(int c = 1; c<=x; c++)
fac = fac * c; 
return fac; 
}

Program 20:
Write a program that explains the concept of local variables.

#include<iostream.h>
#include<conio.h>
main()
{
int x, y, z;
int max (int, int);
x = 5;
y = 15;
z = max (x, y);
cout << "Largest value is: " << z;
getch(); 
}

// definition of max() function 
int max (int a, int b)
{
int mx;
mx = (a > b) ? a : b;
return mx;
}

Program 21:
Write a program that explains the concept of global variables.

#include<iostream.h>
#include<conio.h>
int x, y, s
void sum (int, int);

main()
{
x = 6;
y = 12;
sum(x, y);
cout<< "Sum = "<<s;
getch();
}


// definition of sum() function 
void sum(int m, int n)
{
s = m + n;
}

Leave a Reply