# Programming Exercise

Functions Programming Exercise P-1

Pointers programming exercise p-2

#### Program 10:Write a program that accepts two integer values and passes these values to a function by reference. The function swaps/exchanges these values. The swapped / exchanged values should be displayed in the calling function.

```#include<iostream.h>
#include<conio.h>
main()
{
void swap(int&, int&);
int a, b;
clrscr();
cout<<"Enter value of a ? ";
cin>>a;
cout<<"Enter value of b ? ";
cin>>b;
swap(a,b);
cout<<"Values of a & b after swapping\n";
cout<<"Value of a ="<<a<<endl;
cout<<"Value of b ="<<b<<endl;
getch();
}

// definition of swap() function
void swap(int& x, int& y)
{
int temp;
temp = x;
x = y;
y = temp;
}```

#### Program 11:Write a program that gets three numbers and passes these numbers to a function. The function finds out the largest number and returns this number to the calling function.

```#include<iostream.h>
#include<conio.h>
main()
{
int max(int, int, int); //function prototype
int a, b, c;
clrscr();
cout<<"Enter first number: ";
cin>>a;
cout<<"Enter second number: ";
cin>>b;
cout<<"Enter third number: ";
cin>>c;
cout<< "Largest number is: "<<max(a, b, c);
getch();
}

// definition of max() function
int max(int x, int y, int z)
{
int mx = x;
if (y > mx)
mx = y;
if (z > mx)
mx = z;
return mx;
}```

#### Program 12:Write a function GCD that has two input parameters and returns the greatest common divisor of the two numbers passed to it. Write a complete C++ program that inputs two numbers and calls the function GCD to compute the greatest common divisor of the numbers entered.

```#include<iostream.h>
#include<conio.h>
main()
{
int GCD(int, int);
int a, b, cd;
clrscr();
cout<<"Enter first integer value ? ";
cin>>a;
cout<<"Enter second integer value ? ";
cin>>b;
cd = GCD(a, b);
cout<<"Greatest common divider is : "<<cd;
getch();
}

// definition of GCD() function

int GCD(int x, int y)
{
int i, g = 1;
for(i = 2;i<=x; i++)
if(x%i== 0 && y%i== 0)
g = i;
return g;
}```

#### Program 13:Write a program by defining a function “qualitypoints” that takes average marks of a student and returns the value as:// 4 if average is between 90 and 100.// 3 if average is between 70 and 89. // 2 if average is between 50 and 69.// 1 if average is between 40 and 49.// 0 if average is lower than 40.

```#include<iostream.h>
#include<conio.h>
main()
{
int qualitypoints(int);
int avg;
clrscr();
cout<<"Enter average marks ? ";
cin>>avg;
cout<<"Student's quality points = " <<qualitypoints(avg);
getch();
}

// definition of qualitypoints() function

int qualitypoints(int average)
{
if (average >= 90)
else if (average >= 70)
else if (average >= 50)
else if (average >= 40)
else
}```

#### Program 14:Write a program by defining a function “grade” that takes average marks of a student and returns his/her grade. The function finds out the grade of the student on the basis of following criteria:// Grade is ‘A’ if average is between 90 and 100.// Grade is ‘B’ if average is between 70 and 89. // Grade is ‘C’ if average is between 50 and 69.// Grade is ‘D’ if average is between 40 and 49.// Grade is ‘F’ if average is lower than 40.

```#include<iostream.h>
#include<conio.h>
main()
{
int avg;
clrscr();
cout<<"Enter average marks ? ";
cin>>avg;
getch();
}

{
char gr;
if (average >= 90)
gr ='A';
else if (average >= 70)
gr ='B';
else if (average >= 50)
gr ='C';
else if (average >= 40)
gr ='D';
else
gr ='F';
return gr;
}```

#### Program 15:Write a program that prompts the user to enter a number and calls a function Factorial() to compute its factorial. Write the function Factorial() that has one input parameter and returns the factorial of the number passed to it.

```#include<iostream.h>
#include<conio.h>
void main(void)
{
long Factorial(int), ans;
int x;
clrscr();
cout<<"Enter integer value ? ";
cin>>x;
ans = Factorial(x);
cout<<"Factorial of "<<x<< "is"<<ans;
getch();
}

// definition of Factorial() function

long Factorial(int n)
{
long f=1;
for(; n>=1; n--)
f = f*n;
return f;
}```