Assigment 2
Assigment 2
Practical No.68
Task: Write a program that accept a number from the user as an input and calculates the
square of a given number using function.
#include <iostream>
int square(int);
int main()
int numb=0,result=0;
cin>>numb;
result=square(numb);
return 0;
return(number*number);
cout << "My name is Muhammad Musab Mumtaz and my registration number is
BSEE01153257" << endl;
return 0;
Practical No. 69
Task: Write a program that by defining a function “even_odd”, to test whether a given
integer is even or odd. Pass an integer value as an argument to a function.
#include <iostream>
int evenodd(int);
int main()
int num ;
cin>>num;
evenodd(num);
return 0;
if(num%2==0)
else
Practical No. 70
Task: Write a program that accepts three numbers from the user as an input and display
minimum of three numbers using function.
#include <iostream>
using namespace std;
int main()
int num1,num2,num3;
cin>>num1;
cin>>num2;
cin>>num3;
mini(num1,num2,num3);
return 0;
else
{
Practical No. 71
Task: Write a program that calculates the area of the Ring using a single function.
#include <iostream>
int main()
double radius1,radius2;
cin>>radius1;
cin>>radius2;
ring(radius1,radius2);
return 0;
{
double area1;
area1= 3.142*radius1*radius1;
double area2;
area2= 3.142*radius2*radius2;
Practical No. 72
Task: Write a function integerPower ( base, exponent ) that returns the value of
base exponent
#include <iostream>
int main()
double result,x;
int power;
cin>>x;
cin>>power;
raisetopower(x,power);
return 0;
for(int i=0;i<power;i++)
result= result*x;
Practical No. 73
Task: Write an integer number is said to be a perfect number if the sum of its factors,
including 1 (but not the number itself), is equal to the number.
#include <iostream>
using namespace std;
int perfect(int);
int main()
int number;
for(number=1;number<=1000;number++)
perfect(number);
return 0;
int sum = 0;
for(int i =1;i<=number/2;i++)
if(number%i==0)
sum = sum+ i;
if(sum==number)
}
Practical No. 74
Task: A positive integer is entered through the keyboard. Write a function to obtain the
prime factors of this number.
For example: Prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are 5 and 7.
#include <iostream>
#include<stdio.h>
int main()
int number;
scanf("%d", &number);
primefactor(number);
prime(int num)
int i, ifprime;
if (num%i==0)
ifprime=0;
else
ifprime=1;
return (ifprime);
primefactor(int num)
int factor,ifprime;
prime(factor);
if (ifprime)
if (num%factor==0)
num=num/factor;
continue;
else
factor++;
return 0;
}}
Practical No. 75
Task: Write a function that takes an integer value and returns the number with its digits
reversed.
For example, given the number 7631, the function should return 1367.
#include <iostream>
int reverse(int);
int main()
{
int number;
cin>>number;
reverse(number);
return 0;
int number1,number2,number3,number4;
number1 = (number%10);
cout<<number1<<number2<<number3<<number4;
Practical No. 76
Task: The greatest common divisor (GCD) of two integers is the largest integer that evenly
divides each of the numbers. Write a function gcd that returns the greatest common
divisor of two integers.
#include <iostream>
#include<stdio.h>
int main()
int a, b;
return 0;
if(a == b)
return a;
while(b!= 0 || a!= 0)
if( b != 0)
a %= b;
else
return a;
if( a != 0)
b %= a;
else
return b;
Practical No. 77
#include <iostream>
#include<stdio.h>
#include<math.h>
int main()
float sum,x,m,n,g,i,c,h;
scanf("%f",&x);
sum=x;
c=1;
i=3;
m=2;
ab:
m=m*i;
h=pow((-1),c);
g=pow(x,i);
sum=sum+(h*g)/m;
i=i+2;
c=c+1;
m=m*(i-1);
if(i<=n)
goto ab;
printf("\n Sum=%f",sum);
Practical No. 78
Task: Write a program using function to calculate the factorial value of any integer
entered by the user as an input.
#include <iostream>
Practical No. 79
Task: A 5-digit positive integer is entered through the keyboard, write a function to
calculate sum of digits of the 5-digit number:
#include <iostream>
Practical No. 80
Task: Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence. In
a Fibonacci sequence the sum of two successive terms gives the third term. Following are
the first few terms of the Fibonacci sequence:
#include <iostream>
int main()
for(next=0, last=1;last<=100;)
cout<<last<<"\t";
s = next+last;
next=last;
last=s;
return 0;
Practical No. 81
Task: Write a C++ program that uses an inline function circleArea to prompt the user for
the radius of a circle and to calculate and print the area of that circle.
Practical No. 82
Task: Write a C++ program that uses an inline function pound_kg to prompt the user for
the weight in pound and to calculate and print the equivalent in kilogram.
Practical No. 83
Task: Write a program by defining a function swap to exchange the values by passing
argument by reference to the function.
#include <iostream>
int main()
int num1,num2;
cin>>num1;
cin>>num2;
swap(num1,num2);
return 0;
int temp=0;
temp=num1;
num1=num2;
num2=temp;
return 0;
}
Practical No. 84
Task: Computers are playing an increasing role in education. Write a program that will
help an elementary school student learn multiplication. Use rand to produce two positive
one-digit integers. It should then type a question such as:
The student then types the answer. Your program checks the student's answer. If it is
correct, print "Very good!", and then ask another multiplication question. If the answer is
wrong, print "No. Please try again." and then let the student try the same question again
repeatedly until the student finally gets it right.
Practical No. 85
Task: Write a program that plays the game of “guess the number” as follows: Your
program chooses the number to be guessed by selecting an integer at random in the range
1 to 1000. The program then types:
The player then types a first guess. The program responds with one of the following:
If the player's guess is incorrect, your program should loop until the player finally gets the
number right. Your program should keep telling the player Too high or Too low to help
the player “zero in” on the correct answer.
#include <iostream>
using namespace std;
int main()
int z , b ;
int arr[1000];
arr[i] = rand();
cin>>z;
if(z==arr[i]) b = 1;
if(b==1)
else
cin>>ch;
if(ch=='y' || ch=='Y')
j=1;
if(ch=='n' || ch=='N')
break;
return 0;
Practical No. 86
Task: Write a complete C++ program with the two alternate functions specified below, of
which each simply triples the variable count defined in main. Then compare and contrast
the two approaches. These two functions are
a) Function tripleCallByValue that passes a copy of count call-by-value, triples the copy
and returns the new value.
int TripleCallByValue(int );
int main()
int count;
cin>>count;
return 0;
return 0;
Practical No. 87
Task: Write a C++ program that demonstrates the concept of function overloading by
calculating the cube of any number. The function cube calculate cube of different data
type having same or different parameter.
Practical No. 88
Task: Write a C++ program that demonstrates the concept of default argument in
calculating the volume of the box. This function calculate volume of the box 4 time having
default values at first and changing them until having no default vales in last call.
Arrays
Array: An Array is a set of contigious memory locations i.e. all the elements of an array are stored
contigiously / continuously in memory. All the elements of array have same name as that of array
but have different indexes or subscripts. So every element of array is referenced by
1. Array Name
2. Corresponding index or Subscript
That’s why array is also known as "subscripted variable". Moreover the length of array is always
fixed (defined at the time of declaration) and all the elements of an array have same data type e.g.
int, float, long etc...
Note that the index of an array starts from 0 (instead of 1) and ends at N-1 where N is the size of
array. i.e. an array of 5 variables will have its indexes from 0 to 4 as a[0],a[1],a[2],a[3] and a[4].
Practical No.88
Task: Write a program that uses an array of five elements. It just accepts the elements of
array from the user and displays them in the same order using for loop.
#include <iostream>
int main()
int arr[5],i;
for(i=0;i<5;i++)
{
cout<<"Enter value at position "<<i+1<<" : ";
cin>>arr[i];
cout<<"\tReverse values"<<endl;
for(i=0;i<=4;i++)
cout<<arr[i]<<endl;
return 0;
Practical No.89
Task: Write a program that uses an array of five elements. It just accepts the elements of
array from the user and displays them in the reverse order using for loop.
#include <iostream>
int main()
{
int arr[5],i;
for(i=0;i<5;i++)
cin>>arr[i];
cout<<"\tReverse values"<<endl;
for(i=4;i>=0;i--)
cout<<arr[i]<<endl;
return 0;
Practical No.90
Task: Write a program that uses an array of 8 elements entered by user and then find out
total number of odd and even values entered by the user in a one dimensional array.
#include <iostream>
cin>>arr[num];
if(arr[num]%2==0)
num1= num1+1;
else
num2= num2+1;
return 0;
}
Practical No.91
Task: Write a program to enter the data in two linear arrays, add the two arrays and store
the sum in third array.
#include <iostream>
int main()
int A[7],B[7],C[7],i;
for(i=0;i<7;i++)
cin>>A[i];
for(i=0;i<7;i++)
cin>>B[i];
for(i=0;i<7;i++)
C[i]=A[i]+B[i];
for(i=0;i<7;i++)
{
cout<<C[i]<<"\n";
return 0;
Practical No.92
Task: Write a program that calculates the sum of square of numbers stored in an array of
10 elements.
#include <iostream>
int main()
int A[10],i;
for(i=0;i<10;i++)
{
cin>>A[i];
for(i=0;i<10;i++)
A[i]=A[i]*A[i];
for(i=0;i<10;i++)
{cout<<A[i]<<"\n";
return 0;
Practical No. 93
Task: Write a program that accepts five elements from a user in an array and find their
maximum and minimum.
Logic: Main Logic of this program is
1. We assume that first element (0) of array is maximum or minimum.
2. We compare the assumed max / min with the remaining (1... N-1) elements of the arrays and if a
number greater than max or less than min is found ,then that is put in the max or min variable,
overwriting the previous value.
#include <iostream>
cout << "My name is Muhammad Musab Mumtaz and my registration number is BSEE01153257" << endl;
return 0;
}
Practical No. 94
Task: Write a program that should accepts five elements from the user in an array and
search a particular element (entered by the user) in it. If the element is found in an array
its index and contents (value) is displayed, else (not found) a massage “Element not found
in an array” is displayed. (Using Linear /Sequential Search Algorithm)
#include <iostream>
int A[5],i,p=0,num;
for(i=0;i<5;i++)
cin>>A[i];
cin>>num;
cout<<endl;
for(i=0;i<5;i++)
if(num==A[i])
p=i+1;
if(p==0)
else
return 0;
}
Practical No. 95
Task: Write a program that should accepts five elements from the user in an array and
search a particular element (entered by the user) in it. If the element is found in an array
its all (apparent) and total number of occurrences are displayed, else (not found) a
massage “Element not found in an array” is displayed.
#include <iostream>
int main()
int a[5],i,num,s,x=0;
for(i=0;i<5;i++)
cin>>a[i];
cin>>num;
cout<<endl;
for(i=0;i<5;i++)
{
if(num==a[i])
x=x+1;
return 0;
Practical No. 96
Task: Write a program that should accepts 10 elements from the user in an array and
search a particular element (entered by the user) in it. If the element is found in an array
its index is displayed, else (not found) a massage “Element not found in an array” is
displayed. (Using Binary Search Algorithm)
#include <iostream>
int main()
{
int a[10];
int i,loc,num,mid,beg=0,end=9;
for(i=0;i<10;i++)
cin>>a[i];
cin>>num;
while(beg<=end)
mid=(beg+end)/2;
if(num==a[mid])
loc=mid;
break;
else if (num<a[mid])end=mid-1;
else if(num>a[mid])beg=mid+1;
else
return 0;
}
Practical No. 97
Task: Write a program that accepts ten elements from a user in an array and sorts them in
ascending order using:
1. Bubble sort
#include <iostream>
int main()
int a[10],i,j,t=0;
for(i=0;i<10;i++)
cin>>a[i];
for(i=0;i<9;i++)
for(j=0;j<9-i;j++)
{
if(a[j]>a[j+1])
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
for(i=0;i<10;i++)
cout<<a[i]<<"\t";
return 0;
Practical No. 98
Task: Write a program that accepts ten elements from a user in an array and sorts them in
Descending order.
#include <iostream>
using namespace std;
int main()
int a[10],i,j,t=0;
for(i=0;i<10;i++)
cin>>a[i];
for(i=0;i<9;i++)
for(j=0;j<9-i;j++)
if(a[j]<a[j+1])
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
cout<<"Sorted Array";
cout<<endl;
for(i=0;i<10;i++)
{
cout<<a[i]<<"\t";
return 0;
Practical No. 99
Task: Write a program that accepts ten elements from a user in an array and sorts first five
elements in ascending order and rest of the five elements in descending order using
bubble sort.
#include <iostream>
int main()
{
int a[10],i,j,t=0;
for(i=0;i<10;i++)
{
cout<<"Enter Array Element at "<<i<<"= ";
cin>>a[i];
}
for(i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(i=5;i<9;i++)
{
for(j=5;j<9-i;j++)
{
if(a[j]<a[j+1])
{
t=a[j];a[j]=a[j+1];
a[j+1]=t;
}
}
}
cout<<"Sorted Array";
cout<<endl;
for(i=10; i>=0; i--)
{
cout<<a[i]<<"\t";
}
return 0;
}
Task: Write a program that accepts two arrays (A and B) of ten elements each from the
user and merges them in a single array (c) of twenty elements.
#include <iostream>
int main()
int a[10],b[10],c[20],i,j,m,k,l;
cin>>a[i];
for(j=0;j<10;j++)
cin>>b[j];
cout<<"Emerging Array"<<endl;
for(k=0;k<10;k++)
c[k]=a[k];
for(m=0;m<10;m++)
c[m+10]=b[m];
for(l=0;l<20;l++)
cout<<c[l]<<"\t";
return 0;
}
Practical No. 101
#include <iostream>
int main()
{
int array[11] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
int num = 0;
int data,i;
cout<<"Values of Array Before Inserting : ";
for(i=0;i < 11 ;i++)
cout<<array[i]<<",";
cout<<endl;
do
{
cout << "Where do you want to insert an element from 0-10 "<<endl;
cin >> num;
}
while (num < 0 || num > 10);
cout << "Enter data to insert : "<<endl;
cin >> data;
for(i = 9; i > num - 1; i--)
array[i + 1] = array[i];
array[num] = data;
cout << "Final Array: "<<endl;
for(i = 0; i < 11; i++)
cout << array[i] <<endl;
return 0;
}
Practical No. 102
Task: Write a program that takes values from user to fill a two-dimensional array (Matrix)
having two rows and three columns and display the values in row column format.
#include <iostream>
int main()
int a[2][3],i,j;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
cout<<endl<<"a["<<i<<"]["<<j<<"]::";
cin>>a[i][j];
}
cout<<endl<<"Number you Entered are in Matrix Form"<<endl;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
cout<<a[i][j]<<"\t";
cout<<endl;
return 0;
Task: Write a program that takes values from user to fill a two-dimensional array (Matrix)
having three rows and three columns and display it’s contents and also display the
flipping the same matrix (i.e. reversing the row order) using functions.
int main(int)
{
int a[maxRows][maxCols];
readMatrix(a);
cout << "\n\n" << "The original matrix is: " << '\n';
displayMatrix(a);
cout << "\n\n" << "The flipped matrix is: " << '\n';
displayFlippedMatrix(a);
return 0;
cout << "\n" << "Enter " << row << ", " << col << " element: ";
}
cout << '\n';
}
Practical No. 105
Task: Write a program that should accepts five elements from the user in a 2D-array and
search a particular element (entered by the user) in it. If the element is found in an array
its indexes are displayed, else (not found) a massage “Element not found in an array” is
displayed (Using Linear /Sequential Search Algorithm).
#include <iostream>
int main()
int mata[2][3],num,i,j,index1,index2,check=0;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
cin>>mata[i][j];
cin>>num;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
if(num==mata[i][j])
check=1;index1=i;index2=j;
}
if(check==1)
else
return 0;
Task: Write a program that takes values from user to fill a 2 two-dimensional array
(Matrix) having three rows and three columns and then add these two matrixes and store
the result in another matrix and display all matrixes values in row column format
(Addition of two matrixes).
#include <iostream>
int main()
int Mata[3][3],Matb[3][3],Matc[3][3],row,col;
{
for(col=0; col < 3; col ++)
cout << "\n" << "Enter " << row << "," << col << "element : ";
cout << "\n" << "Enter " << row << "," << col << "element : ";
Matc[row][col]=Mata[row][col] + Matb[row][col];
cout<<Matc[row][col]<<"\t";
cout<<"\n";
}
return 0;
Task: Write a program that takes values from user to fill a 2 two-dimensional array
(Matrix) having any order and then multiply these two matrixes if possible (i.e. Checking
multiply rule first) and store the result in another matrix and display all matrixes values
in row column format (Multiplication of two matrixes).
#include <iostream>
int main()
int Mata[3][3],Matb[3][3],Matc[3][3]={0},row,col,k;
cout<<"\n Enter Element of Matrix A"<< endl;
cout << "\n" << "Enter " << row << "," << col << "element : ";
cout << "\n" << "Enter " << row << "," << col << "element : ";
cout<<Matc[row][col]<<"\t";
cout<<"\n";
return 0;
Task: Write a program that takes values from user to fill a two-dimensional array (Square
Matrix) then calculate the transpose of that matrix and display its contents in matrix form.
#include <iostream>
using namespace std;
int main()
int A[3][3],i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cout<<"A["<<i<<"]["<<j<<"]::";
cin>>A[i][j];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cout<<A[i][j]<<"\t";
cout<<endl;
for(j=0;j<3;j++)
for(i=0;i<3;i++)
cout<<A[i][j]<<"\t";
cout<<endl;
}
return 0;
Task: Write a program use a single-subscripted array to solve the following problem.
Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is
read, print it only if it is not a duplicate of a number already read. Provide for the “worst
case” in which all 20 numbers are different. Use the smallest possible array to solve this
problem.