Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
88 views

CP Lab 8 Ex 1 - 5 (Function Overloading & Recursion)

This document contains code for 5 exercises on function overloading and recursion in C++. The exercises include programs to calculate grades using overloaded functions, compare numbers using overloaded functions, print numbers in reverse order using recursion, calculate a sum using recursion, and print the Fibonacci series using recursion. The code provided includes int main() functions and user-defined functions implemented using function overloading and recursion.

Uploaded by

Yahya Faisal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

CP Lab 8 Ex 1 - 5 (Function Overloading & Recursion)

This document contains code for 5 exercises on function overloading and recursion in C++. The exercises include programs to calculate grades using overloaded functions, compare numbers using overloaded functions, print numbers in reverse order using recursion, calculate a sum using recursion, and print the Fibonacci series using recursion. The code provided includes int main() functions and user-defined functions implemented using function overloading and recursion.

Uploaded by

Yahya Faisal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

CSL-113: Computer Programming

Lab
Semester BS (CS) – 01
Yahya Faisal
02-134211-006
Lab 8 (Function Overloading &
Recursion)

Exercise 1
1. Write a C++ program for calculating grades of students by using int main() and one
user defined function Calgrades().
#include<iostream>
using namespace std;
char calgrades(int, int);
char calgrades(int, int, int);

int main()
{
int n;
cout << "Enter number of subjects: ";
cin >> n;
if (n == 3) {
int a, b, c;
cout << "Enter marks respectively: ";
cin >> a >> b >> c;
char r = calgrades(a, b, c);
cout << "GRADE: " << r;
}
if (n == 2)
{
int a, b;
cout << "Enter marks respectively: ";
cin >> a >> b;
char r = calgrades(a, b);
cout << "GRADE: " << r;
}
cout << endl;
system("pause");
}
char calgrades(int x, int y)
{
int temp = x + y;
int sum = temp / 2;
if (sum >= 50 && sum <= 64)
return 'D';
if (sum >= 65 && sum <= 74)
return 'C';
if (sum >= 75 && sum <= 86)
return 'B';
if (sum >= 87 && sum <= 100)
return 'A';
else
return 'F';
}
char calgrades(int x, int y, int z)
{

Department of Computer Sciences Semester BSCS1


CSL-113: Computer Programming Lab Journal
int sum, temp;
temp = x + y + z;
sum = temp / 3;
if (sum >= 50 && sum <= 64)
return 'D';
if (sum >= 65 && sum <= 74)
return 'C';
if (sum >= 75 && sum <= 86)
return 'B';
if (sum >= 87 && sum <= 100)
return 'A';
else
return 'F';

Department of Computer Sciences Semester BSCS1


CSL-113: Computer Programming Lab Journal
Department of Computer Sciences Semester BSCS1
CSL-113: Computer Programming Lab Journal
Exercise 2
2. Write a C++ program that contains int main() function and one user defined function
int comparison().
#include<iostream>
using namespace std;
void comp(int, int);
void comp(int, int, int);
void comp(int, int, int, int);
int main()
{
int n, a, b, c, d;
cout << "Enter choice from 2 to 4: ";
cin >> n;
if (n == 2) {
cout << "Enter 2 Positive Numbers: " << endl;
cin >> a >> b;
comp(a, b);
cout << "\n****************************************************\n";
}
else if (n == 3) {
cout << "Enter 3 Positive Numbers: " << endl;
cin >> a >> b >> c;
comp(a, b, c);
cout << "\n****************************************************\n";
}
else if (n == 4) {
cout << "Enter 4 Positive Numbers: " << endl;
cin >> a >> b >> c >> d;
comp(a, b, c, d);
cout << "\n****************************************************\n";
}
else cout << "Wrong Choice" << endl;
system("Pause");
}
void comp(int x, int y)
{
if (x > y) {
cout << "Largest number is " << x << endl << "Smallest number is " << y << endl;
}
else {
cout << "Largest number is " << y << endl << "Smallest number is " << x << endl;
}
}
void comp(int x, int y, int z) {
int num[3] = {x, y, z}, i;
float Largest = -INFINITY, Smallest = INFINITY;
for (i = 0; i < 3; i++) {
if (num[i] > Largest) {
Largest = num[i];
}
if (num[i] < Smallest) {
Smallest = num[i];

Department of Computer Sciences Semester BSCS1


CSL-113: Computer Programming Lab Journal
}
}
cout << "Largest number is " << Largest << endl << "Smallest number is " << Smallest;
}
void comp(int x, int y, int z, int w) {
int num[4] = { x, y, z, w}, i;
float Largest = -INFINITY, Smallest = INFINITY;
for (i = 0; i < 4; i++) {
if (num[i] > Largest) {
Largest = num[i];
}
if (num[i] < Smallest) {
Smallest = num[i];
}
}
cout << "Largest number is " << Largest << endl << "Smallest number is " << Smallest;
}

Department of Computer Sciences Semester BSCS1


CSL-113: Computer Programming Lab Journal
Department of Computer Sciences Semester BSCS1
CSL-113: Computer Programming Lab Journal
Exercise 3

3. Write a C++ recursive function that prints the numbers between 1 to n in a reverse
order.

#include <iostream>
using namespace std;
int recur(int x)
{
cout << x << " ";
if (x <= 1)
return 1;
else
return recur(x - 1);
}
int main()
{
int n;
cout << "Enter number to generate series in reverse order: ";
cin >> n;
recur(n);
cout << endl;
system("pause");
return 0;
}

Department of Computer Sciences Semester BSCS1


CSL-113: Computer Programming Lab Journal
EXERCISE 4
4. Write a C++ program that contain int main() and one user defined function int sum().
#include<iostream>
using namespace std;
int sum(int x)
{
if (x == 1)
return 1;
else
return x + sum(x - 1);
}
int main()
{
int n, temp, sum1 = 0;
cout << "Enter a Positive number: 1 to ";
cin >> n;
temp = n;

int result = sum(temp);


cout << "Sum of Positive N(" << n << ") is : " << result;
cout << endl;
system("pause");
}

Department of Computer Sciences Semester BSCS1


CSL-113: Computer Programming Lab Journal
EXERCISE 5
5. Write a C++ program that contain int main() function and one user defined function int
fab().

#include<iostream>
using namespace std;
int fib(int x)
{
if (x == 1 || x == 0)
{
return x;
}
else
return fib(x - 1) + fib(x - 2);
}
int main()
{
int n;
cout << "Enter a Positive Number : ";
cin >> n;
cout << "Fibonacci series is :";
for (int i = 0; i <= n; i++){
cout << " " << fib(i);
}
cout << endl;
system("pause");
}

Department of Computer Sciences Semester BSCS1


CSL-113: Computer Programming Lab Journal
Department of Computer Sciences Semester BSCS1
CSL-113: Computer Programming Lab Journal

You might also like