Muhammad Talha Rana Lab7
Muhammad Talha Rana Lab7
by
Submission Lab-07
Submitted to : Ms. Warda Aslam
Date : 25/11/2023
DEPARTMENT OF COMPUTER SCIENCE,
AIR UNIVERSITY ISLAMABAD
TASK #1
Write a function in C++ language that takes four integers as
arguments and returns the largest of the four
numbers. Use this function in a C++ program which takes four
integers from user and displays the largest on
screen.
Source Code:
#include <iostream>
using namespace std;
int main() {
int x, y, z, w;
cout << "Enter four integers:\n";
cin >> x >> y >> z >> w;
cout << "The largest among " << x << "," << y << "," << z << "," << w << " is "
<< big(x, y, z, w) << ".\n";
return 0;
}
RESULT:
TASK #2
Write a function in C++ language that takes an integer ‘num’ as argument and returns
the sum of all numbers
between 1 to num. Use this function in a C++ program which takes an integer from
user, stores it in a variable
named ‘N’ and displays the result of the following function.
F(N) = 1 + (1+2) + (1+2+3) + ......+ (1+2+...N)
Source Code:
#include <iostream>
using namespace std;
return sum;
}
int main() {
int N;
return 0;
}
RESULT:
TASK #3
Write a function ‘swap’ in C++ language that swaps the values of two variables passed as
arguments to this
function. Write a C++ program which takes values of two integer variables from the user and
swaps their
values using function ‘swap’.
Source Code:
#include <iostream>
using namespace std;
int main() {
int x, y;
swap(x, y);
cout << "The two numbers have been swapped." << endl;
cout << "The first number is now " << x << "." << endl;
cout << "The second number is now " << y << "." << endl;
return 0;
}
RESULT:
TASK #4
Write a C++ program which prompts the user to enter an integer, calculates the square and
cube of this
number and prints them on screen. You must define macros for calculating squares and cubes.
Source Code:
#include <iostream>
using namespace std;
int main() {
int n;
int sq = SQR(n);
int cu = CUB(n);
cout << "The square of " << n << " is: " << sq << endl;
cout << "The cube of " << n << " is: " << cu << endl;
return 0;
}
RESULT:
TASK #5
Write a recursive function that displays alphabets from Z to A. Use this function in a C++
program to verify
that your function works correctly.
Source Code:
#include <iostream>
using namespace std;
void reverse(char c) {
if (c >= 'A') {
cout << c << " ";
reverse(c - 1);
}
}
int main() {
cout << "Alphabets from Z to A: ";
reverse('Z');
cout << endl;
return 0;
}
RESULT: