0% found this document useful (0 votes)
26 views8 pages

Muhammad Talha Rana Lab7

The document contains the source code and results for 5 programming tasks completed by Muhammad Talha Rana. The tasks include: 1) Writing a function to find the largest of 4 numbers. 2) Writing a function to calculate the sum of integers from 1 to a given number. 3) Writing a function to swap two variable values. 4) Writing a program to calculate the square and cube of a number using macros. 5) Writing a recursive function to display alphabets from Z to A.

Uploaded by

sekinic187
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)
26 views8 pages

Muhammad Talha Rana Lab7

The document contains the source code and results for 5 programming tasks completed by Muhammad Talha Rana. The tasks include: 1) Writing a function to find the largest of 4 numbers. 2) Writing a function to calculate the sum of integers from 1 to a given number. 3) Writing a function to swap two variable values. 4) Writing a program to calculate the square and cube of a number using macros. 5) Writing a recursive function to display alphabets from Z to A.

Uploaded by

sekinic187
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/ 8

Programming Fundamentals-LAB

by

Muhammad Talha Rana (231632)


BSCS 1-A

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 big(int a, int b, int c, int d) {


int l = a;
if (b > l) l = b;
if (c > l) l = c;
if (d > l) l = d;
return l;
}

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;

int s(int num) {


int sum = 0, values = 0;

for (int i = 1; i <= num; i++)


{
values += i;
sum += values;
if (i < num) {
cout << values << " + ";
}
else {
cout << values << " = ";
cout << sum << endl;
}
}

return sum;
}

int main() {
int N;

cout << "Enter the value of N: ";


cin >> N;

int result = s(N);

cout << "F(" << N << ") = ";


cout << result << endl;

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;

void swap(int a, int b) {


a = a + b;
b = a - b;
a = a - b;
}

int main() {
int x, y;

cout << "Enter the first value: ";


cin >> x;

cout << "Enter the second value: ";


cin >> 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;

#define SQR(x) ((x) * (x))


#define CUB(x) ((x) * (x) * (x))

int main() {
int n;

cout << "Enter a number: ";


cin >> 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:

You might also like