Programming Fundamentals LAB
Lab-07 (27-11-2024)
Student Name: Muhammad Bilal Roll No:NUM-BSCS-2024-47
Task 1: CLO2
What would be the output of the following programs:
main( )
{
cout<< "Only stupids use C++" <<endl ;
display( ) ;
}
display( )
{
cout<< "Fool too use C++" <<endl ;
main( ) ;
}
solution:
the program will give infinite loop its explaination is as follows.
1: when The main function is called, which prints Only stupids use C++ and then calls the
display function.
2: The display function which print Fool too use C++ and then calls the main function again.
3: This cycle repeats indefinitely, causing the program to continuously print Only stupids use
C++ and then print again and again Fool too use C++.
Output:
Task 2: CLO1
Write a function to calculate the factorial value of any integer entered through the
keyboard.
Solution:
#include <iostream>
using namespace std;
int factorial(int a)
{
int product=1;
for(int i=1;i<=a;i++)
{
product=product*i;
}
return product;
}
main( )
{
int k;
cout<<"enter any number:";
cin>>k;
cout<<"product of the given number is:"<<factorial(k);
return 0;
}
Output:
Task 3: CLO1
Write a function power (a, b), to calculate the value of a raised to b.
Solution:
#include<iostream>
using namespace std;
int power(int a,int b)
{
int c=1;
for(int i=1;i<=a;i++)
{
c=c*b;
}
return c;
}
int main()
{
int k,j;
cout<<"enter any number which is used for power:";
cin>>k;
cout<<"enter any number which is used for base:";
cin>>j;
cout<<power(k,j);
return 0;
}
Output:
Task 4: CLO1
Write a function which receives a float and an int from main(), finds the product
of these two and returns the product which is printed through main( ).
Solution:
#include <iostream>
using namespace std;
float product(int a,float b)
{
float c=a*b;
return c;
}
int main( )
{
int k; float m;
cout<<"enter any number in k:";
cin>>k;
cout<<"enter any number in m:";
cin>>m;
cout<<"solution of the product of k and m is:"<<product(k,m);
return 0;
}
Output:
Task 5: CLO1
Write a function that receives 5 integers and returns the sum, average and standard
deviation of these numbers. Call this function from main( ) and print the results in
main( )
Solution:
#include<iostream>
#include <cmath>
using namespace std;
int sum(int a,int b,int c,int d,int e)
{
int sum;
sum=a+b+c+d+e;
return sum;
}
int average(int a,int b,int c,int d,int e)
{
int average;
average=(a+b+c+d+e)/5;
return average;
}
float standard_deviation(int a,int b,int c,int d,int e)
{
int sum,mean;
sum=a+b+c+d+e;
mean=sum/5;
int dev1=a-mean;
int dev2=b-mean;
int dev3=c-mean;
int dev4=d-mean;
int dev5=e-mean;
int dev1_sq=dev1*dev1;
int dev2_sq=dev2*dev2;
int dev3_sq=dev3*dev3;
int dev4_sq=dev4*dev4;
int dev5_sq=dev5*dev5;
double sum_sq=dev1_sq+dev2_sq+dev3_sq+dev4_sq+ dev5_sq;
double variance=sum_sq/4;
double std_dev=sqrt(variance);
return std_dev;
}
int main()
{
int a,b,c,d,e;
cout<<"enter any number in a:";
cin>>a;
cout<<"enter any number in b:";
cin>>b;
cout<<"enter any number in c:";
cin>>c;
cout<<"enter any number in d:";
cin>>d;
cout<<"enter any number in e:";
cin>>e;
cout<<"sum of all numbers is:"<<sum(a,b,c,d,e)<<endl;
cout<<"average of all numbers is:"<<average(a,b,c,d,e)<<endl;
cout<<"standard deviation of all numbers is:"<<standard_deviation(a,b,c,d,e)<<endl;
return 0;
}
Output:
Task 6: CLO1
A 5-digit positive integer is entered through the keyboard, write a function to
calculate sum of digits of the 5-digit number without using Recursion.
Solution:
#include <iostream>
using namespace std;
int main()
{
int a;int n;
cout<<"enter any number:";
cin>>a;
int sum=0;
if(a>=100000&&a<=999999)
{
while(a>0)
{
n=a%10;
a=a/10;
sum=sum+n;
}
cout<<sum;
}
else
cout<<"invalid number";
return 0;
Output:
Task 7: CLO1
Design a function to swap two numbers without using a temporary variable.
Solution:
#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 any number in x:";
cin>>x;
cout<<"enter any number in y:";
cin>>y;
cout << "Before swap: x = " << x << ", y = " << y << endl;
swap(x, y);
cout << "After swap: x = " << x << ", y = " << y << endl;
return 0;
}
Output:
Task 8: CLO3
Develop a C++ program with a function to check if a string is a palindrome.
Solution:
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(string a)
{
int n = a,length();
for (int i = 0; i < n / 2; i++)
{
if (a[i] != a[n - i - 1])
{
return false;
}
}
return true;
}
int main() {
string k;
cout << "Enter a string: ";
cin >> k;
if (isPalindrome(k)) {
cout << k << " is a palindrome." << endl;
} else {
cout << k << " is not a palindrome." << endl;
}
return 0;
}
Output:
Task 9: CLO3
Develop a program that checks whether a given number is an Armstrong number
or not using a function.
For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.
Solution:
#include <iostream>
using namespace std;
int armstrong(int a)
{
int sum=0;
while(a>0)
{
int n=a%10;
int product=1;
for(int i=1;i<=3;i++)
{
product=product*n;
}
a=a/10;
sum=sum+product;
}
return sum;
}
int main()
{
int k;
cout<<"enter any number to know is it armstronge or not:";
cin>>k;
if(armstrong(k)==k)
cout<<"it is an armstrong number";
else
cout<<"it is not armstrong number";
return 0
Output:
Task 10: CLO1
Write a program that calculates the Greatest Common Divisor (GCD) and Least
Common Multiple (LCM) of two numbers using functions.
For example:
The GCD of 8 and 12 is 4 because 4 is the largest number that divides both 8 and
12 evenly.
The LCM of 3 and 5 is 15 because 15 is the smallest number that is divisible by
both 3 and 5.
Solution:
#include <iostream>
using namespace std;
int gcd(int a, int b)
{
while (b != 0)
{
int j = b;
b = a % b;
a = j;
}
return a;
}
int lcm(int a, int b)
{
return (a * b) / gcd(a, b);
}
int main()
{
int m,n;
cout << "Enter first number: ";
cin >>m;
cout << "Enter second number: ";
cin>>n;
cout << "The GCD of two entered number is:"<<gcd(m,n)<<endl;
cout << "The LCM of the above two entered number is:"<<lcm(m,n)<<endl;
return 0;
}
Out put: