C++ Basics for Beginners
C++ Basics for Beginners
“HELLO WORLD”.
# include <iostream>
using namespace std;
//main function-
//where the execution of the
program begins
int main ()
{
//print hello world
cout<<"Hello World";
return 0;
}
1
2
Q2 Write a C++ program to declare 2 variables
and perform addition and subtraction on them.
#include<iostream>
using namespace std;
//MAIN FUNCTION
//WHERE THE EXECUTION OF PROGRAM BEGINS
int main()
{
int num1 ; //DEFINING OF VARIABLES
int num2 ;
num1=10;
num2=15;
int sum;
int sub;
//PERFORMING ARITHMETIC OPERATIONS
//ADDITION OF TWO NUMBERS
sum=num1+num2;
cout<<"The sum of the two numbers is:"<<sum<<endl;
//SUBTRACTION OF TWO NUMBERS
sub=num1-num2;
cout<<"The difference of the two numbers is:"<<sub<<endl;
return 0;
}
3
4
Q3 Write C++ program to take input from
user and perform addition, subtraction,
multiplication and division.
#include <iostream>
using namespace std;
int main()
{
//define variables <data
type> <name of that variable>
int x,y,add,sub,multi,div;
cout<<"enter the value of x:";
cin>>x;
cout<<"enter the value of y:";
cin>>y;
add=x+y;
sub=x-y;
multi=x*y;
div=x/y;
cout<<"\n ADDITION of X
and Y is ::"<<add;
cout<<"\n Subtraction of X
and Y is ::"<<sub;
cout<<"\n Multiplication of X
ad Y is ::"<<multi;
5
cout<<"\n Division of X and Y
is ::"<<div;
return 0;
}
6
Q4 Write a C++ program to take different datatype
inputs from user.
#include<iostream>
using namespace std;
int main()
{
string name,carrots;
cout<<"do you have carrots?\n";
cin>>carrots;
float price;
cout<<"What is the price of
carrots?\n";
cin>>price;
int quantity;
cout<<"What is the quantity of
carrots in kg?\n";
cin>>quantity;
char quality;
cout<<"are the carrots in good
quality?\n";
cin>>quality;
bool answer;
cout<<"are they organic?\n";
cin>>answer;
7
return 0;
}
8
Q5 Write C++ program to calculate the
circumference and area of the circle.
#include <iostream>
using namespace std;
int main()
{
float radius, area,
circumference;
cout<<"\n Enter the radius of
circle";
cin>>radius;
float pi=3.14;
//finding circumference of
circle
circumference=2*pi*radius;
9
radius" <<radius<<"
"<<circumference;
return 0;
}
1
0
Q 6 Write C++ program to convert temperature in
Celsius from Fahrenheit.
#include<iostream>
using namespace std;
int main()
{
double temp_celsius,
temp_fahrenheit;
cout<<"enter temperature in
fahrenheit :";
cin>>temp_fahrenheit;
temp_celcius= (temp_fahrenheit-
32)*(100.0/180);
return 0;
}
1
1
1
2
Q7 Write a C++ program to convert degree into
radians.
#include<iostream>
using namespace std;
int main()
{
double radians, degree,
PI=3.14;
cout<<"enter in
radians :";
cin>>radians;
degree=radians*57.29577
9;
cout<<"in degree
is :"<<degree;
return 0;
}
1
3
1
4
Q8 Write a C++ program to calculate and print the
quotient and remainder. (Take numbers as User
Input).
#include<iostream>
using namespace std;
int main()
{
int num1;
int num2;
cout<<"\n enter first number
:";
cin>>num1;
cout<<"\n enter second
number :";
cin>>num2;
return 0;
1
5
}
1
6
Q9 Write a C++ program to calculate sum of three
numbers and then take average of them.
#include<iostream>
using namespace std;
int main()
{
int num1;
double num2,num3;
double avg,sum;
cout<<"\n enter first number :";
cin>>num1;
cout<<"\n enter second number :";
cin>>num2;
cout<<"\n enter third number :";
cin>>num3;
sum=num1+num2+num3;
avg=(num1+num2+num3)/3;
return 0;
}
1
7
1
8
Q10 Write C++ program to calculate the sum of
first n natural numbers and print it.
#include<iostream>
int main()
{
int i,n,sum=0;
cout<<"How many
numbers? ";
cin>>n;
for(i=1;i<=n; i++)
{
sum+=i;
}
cout<<"Sum="<<sum;
return 0;
}
1
9
2
0
Q11 : WAP to find whether the
user entered no. is
prime/odd/even/composite/nega
tive/positive.
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"\n This program finds
wheather the Number is
Odd,Even,Positive,Negative. ";
cout<<"\n\n Enter a Number : \n ";
cin>> n ;
if (n%2==0)
{
cout<< n <<" is even ";
}
else
{
cout<< n <<" is odd ";
}
if (n>0)
{
cout<<" and is positive ";
}
else
{
cout<<" and is negative ";
2
1
}
cout<<"\n\n";
int number,divisor;
bool isprime = true;
cout<<"enter the number to check
for prime or compsite"<<endl;
cin>>number;
if(number < 1)
cout<<"number needs to be greater
than 1";
else if (number == 1 )
cout<<"1 is neither prime or
composite";
else{
for(divisor = 2; divisor<=
(number/2); divisor++ ){
if( (number % divisor) == 0 )
{
isprime = false;
break;
}
}
if( isprime )
cout<<number<< "is a prime
number";
else
cout<<number<< "is a composite
number";
}
return 0;
2
2
}
2
3
Q12 Write C++ program
that test the value of an
integer num1 if value is
10 then square num1 if it
is 9 then read a new
value in num1 if it is 2 or
3 multiply num1 by 99 &
print the answer.
#include<iostream>
using namespace std;
int main()
{
int num, num1;
cout<<"Enter the value
of num:"<<endl;
cin>>num;
if (num==10)
cout<<"The square of
the number
is :"<<num*num<<endl;
else if (num==9)
2
4
{
cout<<"Enter any
value:";
cin>>num1;
cout<<"The updated
value is :"<<num1<<endl;
}
else if (num==2 ||
num==3)
cout<<"Multiply the
number by
99:"<<num*99<<endl;
//else
cout<<"The program will
terminate here";
return 0;
}
2
5
2
6
Q13 Write C++ program
that calls a function
called Smallest which
finds the smallest
number among three
inputted numbers.
#include<iostream>
using namespace std;
int smallest(int a,int b,int c);
int main()
{
int num1,num2,num3;
cout<<"Enter first number:";
cin>>num1;
cout<<"Enter second number:";
cin>>num2;
cout<<"Enter third number:";
cin>>num3;
int result;
result = smallest(num1,num2,num3);
return 0;
}
int smallest(int a,int b,int c)
{
if (a<=b && a<=c)
cout<< a <<" is smallest among "<< b
<<" and "<< c ;
else if (b<=a && b<=c)
cout<< b <<" is smallest among "<< a
<<" and "<< c ;
else
2
7
cout<< c <<" is smallest among "<< b
<<" and "<< a ;
return 0;
}
2
8
Q14 Write C++ program
that print the sum of n
integers entered by the
user.
#include<iostream>
using namespace std;
int main()
{
int n,a;
int sum=0;
cout<<"How many numbers
you want to add:";
cin>>n;
2
9
for (int i=0;i<n;i++)
{
cout<<"Enter
number"<<i+1<<":";
cin>>a;
sum=sum+a;
}
cout<<"Sum of the "<< n <<"
numbers entered by the user is:
"<<sum<<endl;
return 0;
}
3
0
Q15 Write C++ program
to print the sum of first n
natural numbers using
loop
#include<iostream>
using namespace std;
int natural_numbers(int n);
int main()
{
int num,sum;
3
1
cout<<"Enter any
number :";
cin>>num;
sum=natural_numbers(num);
cout<<"The sum of first
natural numbers is :"<<sum;
return 0;
}
int natural_numbers(int n)
{
return (n*(n+1)/2);
}
3
2
Q16 Write C++ program to
print the sum of
series:1+1/2+1/3……+ 1/n up to
n terms inputted by the user.
#include<iostream>
using namespace std;
int main()
{
int n;
float i ,sum=0;
3
3
cout<<"Enter the number
till which you want to
calculate the sum :";
cin>>n;
for(i=1;i<=n;i++)
{
sum=sum+1.0/i;
}
cout<<"The sum of the
series is :"<<sum;
return 0;
}
3
4
3
5
Q17 Write C++ program
to print the sum of
series:1+1/22+1/32……+
1/n2 up to n terms
inputted by the user.
#include<iostream>
using namespace std;
int main()
{
int n;
float i ,a,sum=0;
cout<<"Enter the number till
which you want to calculate
the sum :";
cin>>n;
for(i=1;i<=n;i++)
{
a=i*i;
sum=sum+1.0/a;
}
cout<<"The sum of the
square of series is :"<<sum;
return 0;
}
3
6
3
7
Q18 Write C++ program to
print 1-20 numbers
entered by the user
arranged in specific rows
and columns
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int count=1;
for(int i=1;i<=5;i++)
{
cout<<"Row"<<i<<":";
for(int j=1;j<=4;j++)
{
cout<<setw(3)<<count++;
}
cout<<endl;
}
return 0;
}
3
8
3
9
Q19 Write C++ program to
numbers in rows and
columns.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i;
for(i=1;i<=4;i++)
{
cout<<"Row"<<i<<":";
for(int j=1; j<=4;j++)
{
cout<<setw(3)<<j;
}
cout<<endl;
}
return 0;
}
4
0
4
1
Q20 Write C++ program to
print number pattern
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a;
cout<<"Enter any number
for the number ladder
pattern:";
cin>>a;
for(int i=1; i<=a ;i++)
{
for(int j=1; j<=i;j++)
{ cout<<j;
}
cout<<endl;
}
return 0;
}
4
2
4
3
Q21 Write C++ program to
print star pattern.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a;
cout<<"Enter any number
till which you want the
pattern:";
cin>>a;
for(int i=1; i<=a ; i++)
{
for(int j=1; j<=i;j++)
{ cout<<"*";
}
cout<<endl;
}
return 0;
}
4
4
4
5
Q22 Write C++ program to
print the square of
elements of an array.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"This program squares the
elements of an array "<<endl;
int arr[10];
int size;
cout<<"Enter the size of the array:";
cin>>size;
for(int i=0;i<size;i++)
{
cout<<"Enter the array elements:";
cin>>arr[i];
}
for(int i=0;i<size;i++)
{
arr[i]=arr[i]*arr[i];
cout<<"The square of elements
are:"<<setw(3)<<arr[i];
cout<<endl;
}
return 0;
}
4
6
4
7
Q23 Write C++ program to
print and calculate the
square of index no and
store it in the array.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"This program squares the
index no of an array "<<endl;
int arr[10];
int size;
cout<<"Enter the size of the array:";
cin>>size;
for(int i=0;i<size;i++)
{
cout<<"Enter the array
elements"<<i+1<<":";
cin>>arr[i];
}
for(int i=0;i<size;i++)
{
arr[i]=i*i;
cout<<"The square of index nos
are:"<<setw(3)<<arr[i];
cout<<endl;
4
8
}
return 0;
}
4
9
Q24 Write C++ program to
print and calculate the
multiples of 3.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i;
cout<<"This program
prints the multiple of 3 in
an array "<<endl;
int arr[10];
for(i=1;i<=10;i++)
{
arr[i]=i*3;
cout<<setw(3)<<arr[i];
}
return 0;
5
0
}
5
1
Q25 Write C++ program to
print the digit in reversed
order.
#include<iostream>
using namespace std;
int main()
{
int num;
int reverse=0;
5
2
cout<<"Input a number to
be reversed:";
cin>>num;
for(int i=1 ; i<=num ; i++)
{
reverse=reverse*10;
reverse=reverse+num
%10;
num=num/10;
}
cout<<"the reverse no
is:"<<reverse;
return 0;
}
5
3
5
4
Q26 Write C++ program to
print array elements in
reversed order.
#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"This program prints the
array in reverse order."<<endl;
cout<<"How many elements do you
want in your array limit is 20:";
int num,i;
cin>>num;
if (num>20)
{num=20;}
int arr[num];
cout<<"Enter "<<num<<" values
"<<endl;
for(i=0;i<num;i++)
{cin>>arr[i];
}
cout<<"Your reversed numbers
are:"<<endl;
for(int j=(num-1);j>=0;j--)
{
cout<<setw(3)<<arr[j];
}
return 0;
}
5
5
5
6
Q27 Write C++ program to
double the value of every
array elements
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"This program doubles the
values of an array"<<endl;
int arr[10];
int size;
int i;
cout<<"Enter the size of the array:";
cin>>size;
for(i=0;i<size;i++)
{
cout<<"Enter the array element
"<<i+1<<":";
cin>>arr[i];
}
for(i=0;i<size;i++)
{ arr[i]=arr[i]*2;
cout<<"The double values of
thearray
element"<<i+1<<":"<<setw(3)<<arr[i]l
;
}
cout<<endl;
5
7
return 0;
}
5
8
Q28 Write C++ program to
sum the array elements.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"This program performs the
sum of an array"<<endl;
int arr[10];
int size;
int i;
int sum=0;
cout<<"Enter the size of the array:";
cin>>size;
for(i=0;i<size;i++)
{
cout<<"Enter the element
"<<i+1<<":";
cin>>arr[i];
}
for(i=0;i<size;i++)
{
sum=sum+arr[i];
}
cout<<"The sum of the array
elements :"<<setw(2)<<sum;
return 0;
5
9
}
6
0
Q29 Write C++ program to
check whether the
elements of an array is
even or odd.
#include <iostream>
#include<iomanip>
using namespace std;
void func1(int x[],int size);
void func2(int x[],int size);
int main()
{
cout<<"This program prints
even and odd array
elements."<<endl;
int arr[10];
int size,i;
cout<<"Enter the size of
array(limit=10):";
cin>>size;
cout<<"Enter "<<size<<"
values"<<endl;
for(i=0;i<size;i++)
{
cin>>arr[i];
}
6
1
cout<<"\n The even numbers in
array are:"<<endl;
func1(arr,size);
cout<<"\n The odd numbers in
array are:"<<endl;
func2(arr,size);
return 0;
}
void func1(int x[],int size)
{
for(int i=0;i<size;i++)
if(x[i]%2==0)
{cout<<setw(3)<<x[i];}
return;
}
void func2(int x[],int size)
{
for(int i=0;i<size;i++)
if(x[i]%2!=0)
{
cout<<setw(3)<<x[i];
}
return;
}
6
2
6
3
Q30 Write C++ program to
find min and max value in
an array inputted by the
user. Also print its index
number.
#include<iostream>
using namespace std;
void maxVal(int x[],int size);
void minVal(int x[],int size);
int main()
{
cout<<"This program prints
maximum and minimum value in an
array along with their index
locations."<<endl;
int arr[10];
int size,i;
cout<<"Enter the size of your
array(limit=10):";
cin>>size;
for(i=0;i<size;i++)
{
cout<<"Enter the
element"<<i+1<<":";
cin>>arr[i];
}
maxVal(arr,size);
cout<<endl;
minVal(arr,size);
return 0;
6
4
}
void maxVal(int x[],int size)
{
int max=x[0];
int loc=0;
for(int i=1;i<size;i++)
{if(max<x[i])
{max=x[i];
loc=i; }
}
cout<<"The maximum value stored
in array is "<<max<<" and its index
no. is "<<loc;
}
void minVal(int x[],int size)
{
int min=x[0];
int loc=0;
for(int i=1;i<size;i++)
{
if(min>x[i])
{
min=x[i];
loc=i;
}
}
cout<<"The minimum value stored in
array is "<<min<<" and its index no.
is "<<loc;
}
6
5
6
6
Q31 Write C++ program to
remove duplicates from
an array.
#include<iostream>
int main()
{
int i,j,k,n,a[30];
cout<<"How many elements?";
cin>>n;
cout<<"\nEnter elements of array\n";
for(i=0;i<n;++i)
cin>>a[i];
for(i=0;i<n;++i)
for(j=i+1;j<n;)
{
if(a[i]==a[j])
{
for(k=j;k<n-1;++k)
a[k]=a[k+1];
--n;
}
else
++j;
}
cout<<"\n";
for(i=0;i<n;++i)
6
7
cout<<a[i]<<" ";
return 0;
}
6
8
Q32 Write C++ program
to search for any element
in the array and also find
the index no of the
element.
#include<iostream>
using namespace std;
int main()
{
cout<<"This program finds an
element in the array and also
print its index no"<<endl;
int size;
cout<<"Enter the size of an
array(limit = 10):";
cin>>size;
for(int i=0;i<size;i++)
{
int arr[10];
cout<<"Enter the
element"<<i+1<<":";
cin>>arr[i];
cout<<endl;
6
9
}
int element;
int arr[10];
cout<<"Enter the element you
want to find in the array:";
cin>>element;
int flag;
for(int i=0;i<size;i++)
{
if(arr[i]==element)
{
arr[i]=i;
cout<<"The element is found at
index no:"<<arr[i]<<endl;
flag=1;
break;
}
else
{
flag=0;
}
}
if (flag==0)
cout<<"The element not found";
return 0;
}
7
0
7
1
Q33 Write C++ program
using linear search find
out the indices for all the
duplicate values.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int arr[10];
int size;
int element;
int arrTemp[10];
int j=0, flag=0;
cout<<"Enter the Size for Array
Size: ";
cin>>size;
for(int i=0; i<size; i++)
{
cout<<"Enter element
"<<i+1<<":";
cin>>arr[i];
}
7
2
cout<<"Enter the element to
Search: ";
cin>>element;
for(int i=0; i<size; i++)
{
if(arr[i]==element)
{
arrTemp[j] = i;
j++;
flag++;
}
}
if(flag>0)
{
cout<<"\n Number Found at
Index No :";
size = flag;
for(int i=0; i<size; i++)
cout<<setw(3)<<arrTemp[i];
}
else
cout<<"\nNumber doesn't
Found!";
cout<<endl;
return 0;
}
7
3
7
4
Q34 Write C++ program
Using Binary search to
sort all the numbers in an
array
#include<iostream>
using namespace std;
int main()
{
cout<<"This program finds
element in array."<<endl;
int arr[10];
int size,i,j;
cout<<"Enter the size of
array(limit=10):";
cin>>size;
for(i=0;i<size;i++)
{
cout<<"Enter the element
"<<i+1<<" of an array:";
cin>>arr[i];
}
cout<<"Enter the element
you want to find:";
7
5
int num;
cin>>num;
int low=0,high=(size-1);
int mid;
int flag=0;
while(low<=high)
{
mid=(low+high)/2;
if(arr[mid]==num)
{cout<<"Element found at
index no. "<<mid;
flag=1;
break;}
else if(arr[mid]>num)
{high=mid-1;}
else
{low=mid+1;}
}
if(flag==0)
cout<<"The element was
not found.";
return 0;
}
7
6
7
7
Q35 Write C++ program
to calculate the average
of elements of an array.
#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"This program sums up all the
values stored in an array."<<endl;
cout<<"How many values do you want to
store in an array(limit=10):";
int num;
cin>>num;
if(num>10)
{
num=10;
}
int arr[num];
for(int i=0;i<num;i++)
{
cout<<"Enter the elements"<<i+1<<":";
cin>>arr[i];
}
int sum=0;
for(int j=0;j<num;j++)
{
sum=sum+arr[j];
}
float
average=static_cast<float>(sum)/num;
cout<<"The average of all the values in
the array is "<<average;
return 0;
7
8
}
7
9
Q36 Write C++ program
to convert table into One
dimensional Array.
#include<iomanip>
using namespace std;
void func1(int x[20][20],int rows, int columns);
void func2(int x[20][20],int rows,int
columns,int
y[40]);
int main()
{
cout<<"This program converts a 2d array to a
1d array."<<endl;
int rows,columns;
cout<<"How many number of rows you want
to enter in 2d array:";
cin>>rows;
cout<<"How many number of columns you
want to enter in 2d array:";
cin>>columns;
int arr[20][20];
cout<<"Enter "<<(rows*columns)<<" values to
be stored."<<endl;
for( int i=0;i<rows;i++)
{
cout<<"Enter row "<<i+1<<"
elements:"<<endl;
for(int j=0;j<columns;j++)
{
cout<<"Enter element "<<j+1<<":";
cin>>arr[i][j];
}
}
cout<<" The 2d array looks like:"<<endl;
8
0
func1(arr,rows,columns);
int resultant[40];
func2(arr,rows,columns,resultant);
return 0;
}
void func1(int x[20][20],int rows, int columns)
{
for(int i=0;i<rows;i++)
{for(int j=0;j<columns;j++)
{cout<<setw(4)<<x[i][j];}
cout<<endl;}
return;
}
void func2(int x[20][20],int rows,int
columns,int y[40])
{
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
y[i*columns+j]=x[i][j];
}
}
cout<<"The new array looks like:"<<endl;
for(int k=0;k<(rows*columns);k++)
{
cout<<setw(4)<<y[k];
}
return;
}
8
1
8
2
Q37 Write C++ program to fill
the matrix in such a way that
the value of diagonal columns
will be zero and the value of
upper region above the
diagonal will be 1 and the value
of lower region below the
diagonal will be -1s
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int table[6][6];
for(int row=0;row<6;row++)
for(int column=0;column<6;column+
+)
if (row==column)
table[row][column]=0;
else if(row>column)
table[row][column]=-1;
else
table[row][column]=1;
for(int row=0;row<6;row++)
{
for(int column=0;column<6;column+
+)
cout<<setw(4)<<table[row]
[column];
cout<<endl;
}
8
3
return 0;
}
8
4
Q38 Write C++ program
to print the square of
every element in a 2-D
Array will be passed row
wise (User-Input
program).
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"THIS PROGRAM PERFORMS
THE SQUARE OF THE ARRAY
ELEMENTS "<<endl;
int i,j;
int result;
int elements[i][j];
int rows;
int cols;
cout<<"Enter the number of rows:";
cin>>rows;
cout<<"Enter the number of
columns:";
cin>>cols;
for( i=0; i<rows; i++)
8
5
{
cout << "Enter row "<< i+1 << endl;
for( j=0; j<cols; j++)
{
cout << "columns "<<j+1<<":";
cin >> elements[i][j];
}
}
cout<<"The values entered by the
user are as follows"<<endl;
for( i=0; i<rows; i++)
{
cout << "rows " << i+1 <<":"<< endl;
for( j=0; j<cols; j++)
{
cout << "columns "<< j+1<< " : " <<
elements[i][j]<<endl;
}
}
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
result=elements[i][j]*elements[i][j];
cout<<"The square of the row
"<<i+1 <<" element
"<<j+1<<":"<<setw(3)<<result<<endl;
}
}
return 0;
}
8
6
8
7
Q39 Write C++ program
to calculate the factorial
of a number using
iteration.
#include<iostream>
using namespace std;
int main()
{
8
8
int n;
cout<<"Enter any
number for which you
want to find the
factorial:";
cin>>n;
int fact=1;
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
cout<<"Factorial of
"<<n<<" is:"<<fact;
return 0;
}
8
9
9
0
Q40 Write C++ program
to calculate the factorial
of a number using
recursion.
#include<iostream>
using namespace std;
int factorial (int n);
int main()
{
cout<<"THIS PROGRAM PRINTS
THE FACTORIAL OF A NUMBER
USING
RECURSION"<<endl;
int num;
cout<<"Enter any number for which
you want to find the factorial :";
cin>>num;
int result;
result=factorial(num);
cout<<"The factorial of "<<num<<"
is:"<<result;
return 0;
}
int factorial(int n)
{
if (n>1)
return (n*factorial(n-1));
else
return 1;
9
1
}
9
2
Q41 Write C++ program
to calculate the sum of
natural number using
recursion.
#include<iostream>
using namespace std;
int sum(int num);
int main()
{
int n;
9
3
cout<<"Enter any number:";
cin>>n;
cout<<"Sum of "<< n
<<"natural numbers
are:"<<sum(n);
return 0;
}
int sum(int num)
{
if (num!=0)
return(num+sum(num-1));
return 0;
}
9
4
Q42 Write C++ program
named Smallest EOF
function which takes the
input of positive numbers
from the user and return
the smallest number.
#include<iostream>
using namespace std;
int smallestEOF(void)
{
9
5
int smallest=INT_MAX;
int numIN;
cout<<"\nPlease enter an integer:";
while (cin>>numIN)
{
if (numIN < smallest)
{
smallest=numIN;
}
cout<<"Enter next integer <EOF> to
stop:";
}
return smallest;
}
int main()
{
cout<<"This program finds the smallest
number among all the numbers"<<endl;
int result;
result=smallestEOF();
cout<<"The smallest number among all
the numbers is :"<<result;
return 0;
}
9
6
9
7
Q43 Write C++ program
named anyPositiveEOF
function which reads
number series &
determine if any are
positive. Also it returns
true if numbers>0 and
returns false if all
numbers<=0.
#include<iostream>
using namespace std;
bool anyPositiveEOF (void)
{
bool anyPositive=false;
int numIn;
cout<<"\nEnter the integer: ";
while (cin>>numIn)
{
if (numIn>0)
anyPositive=(numIn>0);
cout<<"Enter next integer <EOF> to stop: ";
}
return anyPositive;
}
int main()
{
cout<<"The program finds positive number among
all the numbers "<<endl;
int result;
result=anyPositiveEOF();
cout<<"The positive number among all the number
is: "<<result;
return 0;
}
9
8
9
9
Q44 Write C++ program
named all Positive EOF
function which reads a
number series &
determine if all are
positive.
#include<iostream>
using namespace std;
bool allPositiveEOF (void)
{
bool allPositive = true;
int numIn;
int num;
cout<<"How many numbers you want to check:";
cin>>numIn;
for(int i=0;i<numIn;i++)
{
cout<<"Enter number "<<i+1<<":";
cin>>num;
}
if (num<0)
return 0;
//break;
else
return 1;
return allPositive;
}
int main()
{
cout<<"This program read the number series and
determine if all are positive"<<endl;
int result;
result=allPositiveEOF();
cout<<"The output of this program is :"<<result;
return 0;
}
1
0
0
1
0
1
Q45 Write C++ program
to calculate the sum of
two numbers using pass
by value in function.
#include<iostream>
using namespace std;
int add(int x,int y);
int main()
{
int a,b;
cout<<"Enter first value:";
cin>>a;
cout<<"Enter second value:";
cin>>b;
int result;
result=add(a,b);
cout<<"The sum of two
numbers is:"<<result<<endl;
return 0;
}
int add(int x , int y)
{
int sum;
sum=x+y;
1
0
2
return sum;
}
1
0
3
Q46 Write C++ program
to merge two sorted
arrays.
#include<iostream>
#include<iomanip>
using namespace std;
void func1(int x[10],int y[10], int z[20],int size1, int size2);
int main()
{
cout<<"This program merges two sorted arrays."<<endl;
int size1,size2;
cout<<"Enter size of first array.(limit=10):";
cin>>size1;
int arr1[10];
cout<<"The elements of first array are:"<<endl;
for(int i=0;i<size1;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>arr1[i];
1
0
4
}
int arr2[10];
cout<<"Enter size of second array.(limit=10):";
cin>>size2;
cout<<"The elements of second array are:"<<endl;
for(int i=0;i<size2;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>arr2[i];
}
int resultant[20];
func1(arr1, arr2, resultant, size1, size2);
int size3=size1+size2;
cout<<"The new array looks like:"<<endl;
for (int l=0;l<size3;l++)
{
cout<<setw(4) <<resultant[l];
}
return 0;
}
void func1(int x[10],int y[10], int z[20],int size1,int size2)
{
int i=0,j=0,k=0;
int size3=size1+size2;
while(i<size1 && j<size2 )
{
if (x[i]<=y[j])
{
z[k++]=x[i++];
}
else
{
z[k++]=y[j++];
}
}
while(i<size1 )
{z[k++]=x[i++];}
while(j<size2 )
{z[k++]=y[j++];}
return;
}
1
0
5
Q47 Write C++ program
to find whether a number
1
0
6
is prime no or composite
no using iteration.
#include<iostream>
using namespace std;
int main()
{
int n,i,c=0;
cout<<"THIS PROGRAM FINDS
WHETHER A NUMBER IS PRIME OR
COMPOSITE"<<endl;
cout<<"Enter any number:";
cin>>n;
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(n==1)
cout<<"none";
else if(c==2)
cout<<"The Inputted number is a
prime number";
else
cout<<"The Inputted number is a
composite number";
return 0;
}
1
0
7
1
0
8
Q48 Write C++ program
to swap two numbers
inputted by the user
using Pass by Reference
in function.
#include<iostream>
#include<iomanip>
using namespace std;
void swap(int &x ,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
}
int main()
{
int a,b;
cout<<"THIS PROGRAM PERFORMS THE
SWAPPING OF TWO
NUMBERS"<<endl;
cout<<"Enter first number:";
cin>>a;
cout<<"Enter second number:";
cin>>b;
cout<<"Before Swapping , The value of
variables are :"<<"a=" <<a<< setw(3)
<<"b="<<b<<endl;
swap(a,b);
cout<<"After Swapping , The value of
variables are:"<<"a=" <<a<<setw(3)
<<"b="<<b<<endl;
1
0
9
return 0;
}
1
1
0
Q49 Write C++ program
to find the GCD (Greatest
Common Divisor) of a
given number.
#include <iostream>
using namespace std;
int main()
{
int n1, n2, gcd;
cout << "Enter first number: ";
cin >> n1 ;
cout<<"Enter second number:";
1
1
1
cin>>n2;
if ( n2 > n1)
{
int temp = n2;
n2 = n1;
n1 = temp;
}
for (int i = 1; i <= n2; i++)
{
if (n1 % i == 0 && n2 % i ==0)
{
gcd = i;
}
}
cout << "GCD = " << gcd;
return 0;
}
1
1
2
1
1
3
Q50 Write C++ program to create a
class Fraction using Constructor.
#include<iostream>
using namespace std;
class Fraction
{
private:
int numerator;
int denominator;
public:
Fraction();
Fraction(int numer);
Fraction(int numer,int denom);
void store (int numer,int denom);
void print() const;
};
Fraction :: Fraction()
{
numerator = 0;
denominator = 1;
}
Fraction :: Fraction(int numer)
{
numerator = numer;
denominator = 1;
}
Fraction ::Fraction(int numer , int denom)
{
numerator = numer;
denominator = denom;
}
void Fraction::store(int n ,int d)
{
numerator = n;
denominator = d;
}
void Fraction::print() const
{
cout<<numerator<<"/"<<denominator;
}
int main()
{
int num ,den;
cout<<"Enter numerator:";
cin>>num;
cout<<"Enter denominator:";
cin>>den;
Fraction fr1;
cout<<"fr1 contains:";
fr1.print();
cout<<endl;
Fraction fr2(4);
1
1
4
cout<<"fr2 contains:";
fr2.print();
cout<<endl;
Fraction fr3(5,8);
cout<<"fr3 contains:";
fr3.print();
cout<<endl;
Fraction fr4;
cout<<"fr4 contains:";
fr4.store(4,-8);
fr4.print();
cout<<endl;
return 0;
}
1
1
5
Q51 Write C++ program
to perform Linear Search
using recursion.
#include<iostream>
using namespace std;
1
1
6
array:";
cin>>element;
int result;
result=recursive_linear_search(element ,arr , size);
if (result>=0)
cout<<"The element is found at "<<result;
else
cout<<"The element is not found";
return 0;
}
int recursive_linear_search(int ele ,int arr[], int size)
76
{
size = size-1;
if(size<0)
{
return 0;
}
else if (arr[size]==ele)
{
return size;
}
else
{
return recursive_linear_search(ele , arr , size);
}
}
1
1
7
Q52 Write C++ program
to find the GCD of a given
numbers using recursion.
#include <iostream>
using namespace std;
int recursive_gcd(int n1 ,int n2);
int main()
{
cout<<"THIS PROGRAM
CALCULATES THE GCD OF TWO
1
1
8
NUMBERS USING
RECURSION"<<endl;
int num1, num2;
cout << "Enter first number: ";
cin >> num1 ;
cout<<"Enter second number:";
cin>>num2;
int result;
result=recursive_gcd(num1,num2);
cout<<"The gcd of two numbers
"<<num1 <<" and "<<num2 <<" is:
"<<result;
return 0;
}
int recursive_gcd(int n1 ,int n2)
{
if (n2==0)
return n1;
return recursive_gcd(n2,n1%n2);
}
1
1
9
1
2
0
Q53 Write C++ program
to perform Binary Search
using recursion.
#include<iostream>
using namespace std;
int main()
{
cout<<"This program finds element in
array."<<endl;
int arr[10];
int size,i,j;
cout<<"Enter the size of array(limit=10):";
cin>>size;
for(i=0;i<size;i++)
{
cout<<"Enter the element "<<i+1<<" of an
array:";
cin>>arr[i];
}
cout<<"Enter the element you want to find:";
int num;
cin>>num;
int low=0,high=(size-1);
int mid;
int flag=0;
while(low<=high)
{
mid=(low+high)/2;
if(arr[mid]==num)
{cout<<"Element found at index no. "<<mid;
flag=1;
break;}
else if(arr[mid]>num)
{high=mid-1;}
else
{low=mid+1;}
}
if(flag==0)
cout<<"The element was not found.";
return 0;
1
2
1
}
1
2
2
Q54 Write program to find the
fibonacci of a number series using
iteration.
#include <iostream>
using namespace std;
long fib(long num);
int main()
{
int num;
cout<<"enter the number upto
which you want to print the
series :";
cin>>num;
return 0;
}
1
2
3
1
2
4
Q55 Write program to find the
fibonacci of a number series
using recursion .
#include <iostream>
using namespace std;
int main()
{
int nterms;
cout<<"enter number of
terms ";
cin>>nterms;
cout<<"the fibonacci series
is ";
cout<<endl;
int i=1;
int a=0, b=1, c;
while(i<=nterms)
{ c=a+b;
cout<<c<<endl;
a=b;
b=c;
i++;
}
return 0;
1
2
5
}
1
2
6
Q56 Write a program to create class
fraction using copy construction.
#include<iostream>
using namespace std;
class Fraction
{
private:
int numerator;
int denominator;
public:
//void store(int n, int d);
Fraction(int num,int den);
Fraction(const Fraction& fr);
void printIn();
};
Fraction::Fraction(int num,int den)
{
numerator=num;
denominator=den;
}
Fraction::Fraction(const Fraction& fr)
{
numerator=fr.numerator;
denominator=fr.denominator;
};
void Fraction::printIn()
{
cout<<numerator<<"/"<<denominator;
return;
}
int main()
{
cout<<"This program prints fractions."<<endl;
Fraction fr1(5,9);
cout<<"The first fraction looks like:";
fr1.printIn();
cout<<endl;
Fraction fr2(fr1); //copying fr1 to fr2
cout<<"The second fraction looks like:";
fr2.printIn();
return 0;
1
2
7
1
2
8
Q57 Write a program to create a
class name fraction to increment
and decrement the value of a
fraction input by the user using
constructor.
#include<iostream>
using namespace std;
class Fraction
{
private:
int numerator;
int denominator;
int temp;
public:
Fraction(int num,int den);
void print();
void func1();
void func2();
~Fraction();
};
Fraction::Fraction(int num,int den) //constructor
{
numerator=num;
denominator=den;
temp=num;
}
void Fraction::print()
{
cout<<"Fraction looks
like:"<<numerator<<"/"<<denominator<<endl;;
}
void Fraction::func1() //function to increment by1
{
numerator=numerator+denominator;
cout<<"Incremented fraction looks
like:"<<numerator<<"/"<<denominator<<endl;
}
void Fraction::func2() //function to decrement by1
{
numerator=temp-denominator;
cout<<"Decremented fraction looks
like:"<<numerator<<"/"<<denominator<<endl;
}
Fraction::~Fraction()//destructor
{
cout<<"End of program."<<endl;
}
int main()
1
2
9
{
cout<<"Enter numerator"<<endl;
int n,d;
cin>>n;
cout<<"Enter denominator "<<endl;
cin>>d;
Fraction fr1(n,d);
fr1.print(); //we only called print,func1,func2,but constructor
and destructor got
fr1.func1(); //automatically implemented when object was
instantiated
fr1.func2();
return 0;
}
1
3
0
Q58 Write a program to
differentiate between pass by value
and pass by reference using function
calling.
#include<iostream>
using namespace std;
void byval(int num1,int num2);
void byref(int &num1,int &num2);
int main()
{
int a,b;
cout<<"a :";
cin>>a;
cout<<"b :";
cin>>b;
byval(a,b);
cout<<"after passing the function by value,a &
b are :";
cout<<"\na :"<<a;
cout<<"\nb :"<<b;
byref(a,b);
1
3
1
cout<<"\nafter passing the function by ref,a &
b are :";
cout<<"\na :"<<a;
cout<<"\nb :"<<b;
return 0;
}
void byval(int num1,int num2)
{
num1=num1+10;
num2=num2+num1;
}
void byref(int &num1,int &num2)
{
num1=num1+10;
num2=num2+num1;
1
3
2
Q59 Write a program to create a
class fraction to perform the
addition of two fraction input by the
user.
#include<iostream>
using namespace std;
class Fraction
{
private:
int deno;
int num;
public:
Fraction(int n,int d);
Fraction(){}
void print();
void addTo(const Fraction& fr);
void add(const Fraction& fr1, const Fraction& fr2);
};
void Fraction::print()
1
3
3
{
cout<<"\nfraction is :"<<num<<"/"<<deno;
}
void Fraction::addTo(const Fraction& fr)
{
num=(num*fr.deno)+(fr.num*deno);
deno=deno*fr.deno;
}
void Fraction::add(const Fraction& fr1, const Fraction& fr2)
{
num=(fr1.num*fr2.deno)+(fr1.deno*fr2.num);
deno=fr1.deno*fr2.deno;
}
Fraction::Fraction(int n,int d)
{
num=n;
deno=d;
}
int main()
{
int n,den1,num2,den2;
cout<<"enter num1 :";
cin>>n;
cout<<"enter den1 :";
cin>>den1;
cout<<"enter num2 :";
cin>>num2;
cout<<"enter den2 :";
cin>>den2;
Fraction fr1(n,den1);
fr1.print();
Fraction fr2(num2,den2);
fr2.print();
Fraction fr3;
fr3.add(fr1,fr2);
cout<<"\n'sum' by storing it in another object fr3:";
fr3.print();
fr1.addTo(fr2);
cout<<"\nafter simple addition :";
fr1.print();
return 0;
1
3
4
}
class account
{
float bal;
public:
account( float balance)
{
bal=balance;
}
void deposit();
void withdraw();
void display();
};
void account::deposit() //depositing an amount
{
int damt1;
cout<<"\n Enter Deposit Amount = ";
cin>>damt1;
bal+=damt1;
}
void account::withdraw() //withdrawing an amount
{
int wamt1;
cout<<"\n Enter Withdraw Amount = ";
cin>>wamt1;
if(wamt1>bal)
cout<<"\n Cannot Withdraw Amount";
bal-=wamt1;
}
void account::display() //displaying the details
{
cout<<"\n Balance : "<<bal;
}
int main()
{
float balance;
cout<<"\n Enter Details: \n";
cout<<"-----------------------";
cout<<"\n Balance : ";
cin>>balance;
account b1(balance);
b1.deposit();
b1.withdraw();
b1.display();
return 0;
}
1
3
6
1
3
7
Q61 Write a program to use a
macro.
#include<iostream>
using namespace std;
#define ONE 1;
#define LONG_STR "this is a
very\
long string"
#define ABS(a) (a)<0?-a:a
int main()
{
cout<<ONE;
int absolute=ABS(-2);
cout<<"\nabsolute value :
"<<absolute;
cout<<endl;
cout<<LONG_STR;
return 0;
}
1
3
8
1
3
9
Q62 Write C++ program to implement the concept of
destructors.
#include<iostream>
using namespace std;
class Fraction
{
private:
int numerator;
int denominator;
public:
Fraction();
~Fraction();
void store (int numer,int denom);
void print() const;
};
Fraction :: Fraction()
{
cout<<"The object is being
created"<<endl;
numerator = 0;
denominator = 1;
}
Fraction::~Fraction()
{
cout<<"The object is being
deleted"<<endl;
}
void Fraction::store(int n ,int d)
{
1
4
0
numerator = n;
denominator = d;
}
void Fraction::print() const
{
cout<<numerator<<"/"<<denominator;
}
int main()
{
int num ,den;
cout<<"Enter numerator:";
cin>>num;
cout<<"Enter denominator:";
cin>>den;
Fraction fr1;
cout<<"fr1 contains:";
fr1.store(num,den);
fr1.print();
cout<<endl;
return 0;
}
1
4
1
1
4
2
Q63 Write C++ program to
perform addition of two
matrices.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"-----------------------THIS
PROGRAMS CALCUALTES THE SUM
OF TWO MATRICES-----------------------
"<<endl;
int m1[5][5],m2[5][5],result[5][5];
int row1;
int col1;
int row2;
int col2;
cout<<"Enter the rows of matrix 1:";
cin>>row1;
cout<<"Enter the columns of matrix
1:";
cin>>col1;
cout<<"Enter the rows of matrix 2:";
cin>>row2;
cout<<"Enter the columns of matrix
2 :";
cin>>col2;
1
4
3
if(row1!=row2 || col1!=col2)
{
cout<<"Dimension mismatched
for addition"<<endl;
}
else
{
//Entering the elements of rows and
columns of matrix
cout<<"________________________
________________________"<<endl;
cout<<"ELEMENTS OF MATRIX
1"<<endl;
for(int i=0 ; i<row1; i++)
{
cout<<"ROW "<<i+1<<"
ELEMENTS"<<endl;
for(int j=0 ; j<col1 ; j++)
{
cout<<"Enter column"<<j+1<<":";
cin>>m1[i][j];
}
}
cout<<"________________________
___________________________"<<e
ndl;
cout<<"ELEMENTS OF MATRIX
2"<<endl;
1
4
4
for(int i=0 ; i<row2 ; i++)
{
cout<<"ROW "<<i+1<<"
ELEMENTS"<<endl;
for(int j=0;j<col2;j++)
{
cout<<"Enter
column"<<j+1<<":";
cin>>m2[i][j];
}
}
cout<<"________________________
______________________________"
<<endl;
cout<<"THE TWO MATRICES LOOKS
LIKE "<<endl;
cout<<"MATRIX 1 WILL BE :"<<endl;
for(int i=0;i<row1;i++)
{
for(int j=0;j<col1;j++)
{
cout<<setw(5)<<m1[i][j];
}
cout<<endl;
}
cout<<"MATRIX 2 WILL BE :"<<endl;
for(int i=0;i<row2;i++)
{
1
4
5
for(int j=0;j<col2;j++)
{
cout<<setw(5)<<m2[i][j];
}
cout<<endl;
}
cout<<"THE RESULTANT MATRIX
LOOKS LIKE:"<<endl;
for(int i=0;i<row1;i++)
{
for(int j=0;j<col1;j++)
{
result[i][j]=m1[i][j]+m2[i][j];
cout<<setw(5)<<result[i][j];
}
cout<<endl;
}
}
return 0;
}
1
4
6
1
4
7
Q64 Write C++ program to perform subtraction of two
matrices.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"-----------------------THIS PROGRAMS CALCUALTES
THE DIFFERENCE OF TWO MATRICES-----------------------
"<<endl;
int m1[5][5],m2[5][5],result[5][5];
int row1;
int col1;
int row2;
int col2;
cout<<"Enter the rows of matrix 1:";
cin>>row1;
cout<<"Enter the columns of matrix 1:";
cin>>col1;
cout<<"Enter the rows of matrix 2:";
cin>>row2;
cout<<"Enter the columns of matrix 2:";
cin>>col2;
if(row1!=row2 || col1!=col2)
{
cout<<"Dimensions mismatched for
subtraction"<<endl;
}
else
{
//Entering the elements of rows and columns of matrix
cout<<"________________________________________
________"<<endl;
cout<<"ELEMENTS OF MATRIX 1"<<endl;
for(int i=0 ; i<row1 ; i++)
{
cout<<"ROW "<<i+1<<" ELEMENTS"<<endl;
for(int j=0 ; j<col1 ; j++)
{
cout<<"Enter column"<<j+1<<":";
1
4
8
cin>>m1[i][j];
}
}
cout<<"________________________________________
___________"<<endl;
cout<<"ELEMENTS OF MATRIX 2"<<endl;
for(int i=0 ; i<row2 ; i++)
{
cout<<"ROW "<<i+1<<" ELEMENTS"<<endl;
for(int j=0;j<col2;j++)
{
cout<<"Enter column"<<j+1<<":";
cin>>m2[i][j];
}
}
cout<<"________________________________________
______________"<<endl;
cout<<"THE TWO MATRICES LOOKS LIKE "<<endl;
cout<<"MATRIX 1 WILL BE :"<<endl;
for(int i=0;i<row1;i++)
{
for(int j=0;j<col1;j++)
{
cout<<setw(5)<<m1[i][j];
}
cout<<endl;
}
cout<<"MATRIX 2 WILL BE :"<<endl;
for(int i=0;i<row2;i++)
{
for(int j=0;j<col2;j++)
{
cout<<setw(5)<<m2[i][j];
}
cout<<endl;
}
cout<<"THE RESULTANT MATRIX LOOKS LIKE:"<<endl;
for(int i=0;i<row1;i++)
{
for(int j=0;j<col1;j++)
{
1
4
9
result[i][j]=m1[i][j]-m2[i][j];
cout<<setw(5)<<result[i][j];
}
cout<<endl;
}
}
return 0;
}
1
5
0
Q65 Write C++ program to
perform multiplication of two
matrices.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int m1[2][3] , m2[3][2];
1
5
1
cout<<"THIS PROGRAM FINDS
THE PRODUCT OF TWO
MATRICES "<<endl;
int row1 , row2, col1 , col2;
cout<<"MATRIX 1"<<endl;
cout<<"Enter the row of matrix
1:";
cin>>row1;
cout<<"Enter the column of
matrix 1:";
cin>>col1;
cout<<"MATRIX 2"<<endl;
cout<<"Enter the row of matrix
2 :";
cin>>row2;
cout<<"Enter the column of
matrix 2 :";
cin>>col2;
if(col1!=row2)
{
cout<<"Dimension mismatch
for multiplication"<<endl;
}
else
{ int result[5][5];
1
5
2
cout<<"ELEMENTS OF
MATRIX 1"<<endl;
for(int i=0 ; i<row1 ; i++)
{
cout<<"ROW "<<i+1<<"
ELEMENTS"<<endl;
for(int j=0 ; j<col1 ; j++)
{
cout<<"Enter
column"<<j+1<<":";
cin>>m1[i][j];
}
}
cout<<"_____________________
____________________________
__"<<endl;
cout<<"ELEMENTS OF MATRIX
2"<<endl;
for(int i=0 ; i<row2 ; i++)
{
cout<<"ROW "<<i+1<<"
ELEMENTS"<<endl;
for(int j=0;j<col2;j++)
{
1
5
3
cout<<"Enter
column"<<j+1<<":";
cin>>m2[i][j];
}
}
cout<<"_____________________
____________________________
_____"<<endl;
cout<<"THE TWO MATRICES
LOOKS LIKE "<<endl;
cout<<"MATRIX 1 WILL
BE :"<<endl;
for(int i=0;i<row1;i++)
{
for(int j=0;j<col1;j++)
{
cout<<setw(5)<<m1[i][j];
}
cout<<endl;
}
cout<<"MATRIX 2 WILL
BE :"<<endl;
for(int i=0;i<row2;i++)
{
for(int j=0;j<col2;j++)
1
5
4
{
cout<<setw(5)<<m2[i][j];
}
cout<<endl;
}
cout<<"THE RESULTANT MATRIX
LOOKS LIKE:"<<endl;
for(int i=0; i<row1; i++)
{
for(int j=0;j<col2;j++)
{
result[i][j]=0;
for(int k=0;k<col1;k+
+)
{
result[i][j]
+=m1[i][k]*m2[k][j];
cout<<result[i]
[j]<<"\t";
}
}
cout<<endl;
}
}
}
1
5
5
1
5
6
Q66 Write C++ program to find the transpose of a
matrix.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int m[5][5];
int rows;
int cols;
cout<<"------------------------------------THIS PROGRAM
PRINTS THE TRANSPOSE OF A
MATRIX---------------------------------- "<<endl;
cout<<"Enter the rows of matrix:";
cin>>rows;
cout<<"Enter the columns of matrix:";
cin>>cols;
//Entering the elements of rows and columns of matrix
cout<<endl;
cout<<"ELEMENTS OF MATRIX "<<endl;
for(int i=0 ; i<rows ; i++)
{
cout<<"ROW "<<i+1<<" ELEMENTS"<<endl;
for(int j=0 ; j<cols ; j++)
{
cout<<"Enter column"<<j+1<<":";
cin>>m[i][j];
}
}
cout<<endl;
cout<<"THE INPUTTED MATRIX LOOKS LIKE:"<<endl;
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
cout<<setw(5)<<m[i][j];
}
cout<<endl;
1
5
7
}
int result[cols][5];
int r1 , c1;
r1=cols;
c1=rows;
cout<<endl;
cout<<"THE RESULTANT TRANSPOSE MATRIX LOOKS
LIKE:"<<endl;
for(int i=0 ; i<r1 ; i++)
{
for(int j=0; j<c1 ; j++)
{
result[i][j]=m[j][i];
cout<<setw(5)<<result[i][j];
}
cout<<endl;
}
}
1
5
8
Q67 Write C++ program to find the roots of a quadratic
equation.
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
cout<<"This program prints the roots of a
quadratic equation"<<endl;
float a , b , c;
int D;
float root1;
float root2;
cout<<"Example of quadratic equation: ax^2 +
bx + c = 0"<<endl;
1
5
9
cout<<"Enter the value of a:";
cin>>a;
cout<<"Enter the value of b:";
cin>>b;
cout<<"Enter the value of c:";
cin>>c;
D=b*b-4*a*c;
cout<<"The value of D is :"<<D<<endl;
if (D>0 || D==0)
{
if (a==0)
{
root1=-c/b;
cout<<"The equation will have only
one root"<<root1;
}
else if (a==0 && b==0)
{
cout<<"NO Solution"<<endl;
}
else
{
root1=(-b+sqrt(D))/(2*a);
root2=(-b-sqrt(D))/(2*a);
1
6
0
1
6
1
Q68 Write C++ program to perform addition of two
matrices using Matrix class.
#include<iostream>
#include<iomanip>
using namespace std;
class Matrix
{
private:
int row;
int col;
int arr[5][5];
public:
Matrix();
Matrix(int r,int c);
Matrix add(const Matrix &m);
void input();
void print();
};
Matrix::Matrix()
{
row=1;
col=1;
arr[5][5]=0;
}
Matrix::Matrix(int r,int c)
{
row=r;
col=c;
}
Matrix Matrix::add(const Matrix &m)
{
Matrix temp(m.row ,m.col);
int i,j;
for(i=0; i<m.row; i++)
{
for(j=0; j<m.col; j++)
1
6
2
{
temp.arr[i][j]=arr[i][j]+m.arr[i][j];
}
}
return temp;
}
int main()
{
int row1,col1,row2,col2;
cout<<"---------------------WELCOME-THIS
PROGRAM PERFORMS ADDITION OF TWO
MATRICES-------------------------------"<<endl;
cout<<"Enter no of rows of Matrix 1:";
cin>>row1;
cout<<"Enter no of columns of Matrix 1:";
cin>>col1;
1
6
3
cout<<"Enter no of rows of Matrix 2:";
cin>>row2;
cout<<"Enter no of cols of Matrix 2:";
cin>>col2;
if (row1!=row2 || col1!=col2)
{
cout<<"Dimensions mismatched"<<endl;
}
else
{
Matrix m1(row1,col1);
Matrix m2(row2,col2);
cout<<"-----------------------------------------------------------
---------------------------------"<<endl;
cout<<"Matrix 1"<<endl;
m1.input();
cout<<"-----------------------------------------------------------
----------------------------------"<<endl;
cout<<"Matrix 2"<<endl;
m2.input();
Matrix m3(row1,col1);
m3=m1.add(m2);
cout<<"-----------------------------------------------------------
-----------------------------------"<<endl;
cout<<"Matrix 1 looks like:"<<endl;
m1.print();
cout<<endl;
cout<<"-----------------------------------------------------------
-------------------------------------"<<endl;
cout<<"Matrix 2 looks like:"<<endl;
m2.print();
cout<<endl;
cout<<"-----------------------------------------------------------
-------------------------------------"<<endl;
1
6
4
cout<<"Result of addition of two matrices
is:"<<endl;
m3.print();
}
return 0;
}
1
6
5
Q69 Write C++ program to perform subtraction of two matrices
using Matrix class.
#include<iostream>
#include<iomanip>
using namespace std;
class Matrix
{
private:
int row;
int col;
int arr[5][5];
public:
1
6
6
Matrix();
Matrix(int r,int c);
Matrix sub(const Matrix &m);
void input();
void print();
};
Matrix::Matrix()
{
row=1;
col=1;
arr[5][5]=0;
}
Matrix::Matrix(int r,int c)
{
row=r;
col=c;
}
Matrix Matrix::sub(const Matrix &m)
{
Matrix temp(m.row ,m.col);
int i,j;
for(i=0; i<m.row; i++)
{
for(j=0; j<m.col; j++)
{
temp.arr[i][j]=arr[i][j]-m.arr[i][j];
}
}
return temp;
}
void Matrix ::input()
{
int i,j;
for(i=0; i<row; i++)
{
cout<<"Row"<<i+1<<endl;
1
6
7
for(j=0; j<col; j++)
{
cout<<"Enter
element"<<j+1<<":";
cin>>arr[i][j];
}
}
}
void Matrix ::print()
{
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
cout<<setw(5)<<arr[i][j];
}
cout<<endl;
}
}
int main()
{
int row1,col1,row2,col2;
cout<<"---------------------WELCOME-THIS
PROGRAM PERFORMS SUBTRACTION OF TWO
MATRICES-------------------------------"<<endl;
cout<<"Enter no of rows of Matrix 1:";
cin>>row1;
cout<<"Enter no of columns of Matrix 1:";
cin>>col1;
cout<<"Enter no of rows of Matrix 2:";
cin>>row2;
cout<<"Enter no of cols of Matrix 2:";
cin>>col2;
if (row1!=row2 || col1!=col2)
{
cout<<"Dimensions mismatched"<<endl;
1
6
8
}
else
{
Matrix m1(row1,col1);
Matrix m2(row2,col2);
cout<<"-------------------------------------------------------
-------------------------------------"<<endl;
cout<<"Matrix 1"<<endl;
m1.input();
cout<<"-------------------------------------------------------
--------------------------------------"<<endl;
cout<<"Matrix 2"<<endl;
m2.input();
Matrix m3(row1,col1);
m3=m1.sub(m2);
cout<<"-------------------------------------------------------
---------------------------------------"<<endl;
cout<<"Matrix 1 looks like:"<<endl;
m1.print();
cout<<endl;
cout<<"-------------------------------------------------------
-----------------------------------------"<<endl;
cout<<"Matrix 2 looks like:"<<endl;
m2.print();
cout<<endl;
cout<<"-------------------------------------------------------
-----------------------------------------"<<endl;
1
6
9
cout<<"Result of subtraction of two
matrices is:"<<endl;
m3.print();
}
return 0;
}
1
7
0
Q70 Write C++ program to perform multiplication of
two matrices using Matrix Class.
#include<iostream>
#include<iomanip>
using namespace std;
class Matrix
{
private:
int row;
int col;
int result[5][5];
public:
Matrix();
Matrix(int r,int c);
Matrix mul(const Matrix& m);
void input();
void print();
1
7
1
};
Matrix::Matrix()
{
row=1;
col=1;
result[5][5]=0;
}
Matrix::Matrix(int r,int c)
{
row=r;
col=c;
}
Matrix Matrix::mul(const Matrix& m)
{
Matrix temp(row, m.col);
1
7
2
}
void Matrix::print()
{
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
cout<<setw(5)<<result[i][j];
}
cout<<endl;
}
}
int main()
{
int row1,col1,row2,col2;
cout<<"---------------------WELCOME-THIS
PROGRAM PERFORMS MULTIPLICATION OF
TWO MATRICES-------------------------------"<<endl;
cout<<"Enter no of rows of Matrix 1:";
cin>>row1;
cout<<"Enter no of columns of Matrix 1:";
cin>>col1;
cout<<"Enter no of rows of Matrix 2:";
cin>>row2;
cout<<"Enter no of cols of Matrix 2:";
cin>>col2;
if(col1==row2)
{
Matrix m1(row1,col1);
Matrix m2(row2,col2);
cout<<"-----------------------------------------------------------
---------------------------------"<<endl;
cout<<"Matrix 1"<<endl;
m1.input();
cout<<"-----------------------------------------------------------
----------------------------------"<<endl;
cout<<"Matrix 2"<<endl;
1
7
3
m2.input();
cout<<"-----------------------------------------------------------
-----------------------------------"<<endl;
cout<<"Matrix 1 looks like:"<<endl;
m1.print();
cout<<"-----------------------------------------------------------
-------------------------------------"<<endl;
cout<<"Matrix 2 looks like:"<<endl;
m2.print();
cout<<"-----------------------------------------------------------
-------------------------------------"<<endl;
cout<<"Result of multiplication of two matrices
is:"<<endl;
Matrix m3;
m3=m1.mul(m2);
m3.print();
}
else
{
cout<<"Dimensions mismatched"<<endl;
}
return 0;
}
1
7
4
1
7
5
Q71 . Write C++ program to find the transpose of a
matrix using Matrix class.
#include<iostream>
#include<iomanip>
using namespace std;
class Matrix
{
private:
int row;
int col;
int arr[5][5];
public:
Matrix();
Matrix(int r,int c);
void transpose();
void input();
void print();
};
Matrix::Matrix()
{
row=1;
col=1;
arr[5][5]=0;
}
Matrix::Matrix(int r,int c)
{
row=r;
col=c;
}
void Matrix::transpose()
{
Matrix temp(col,row);
for(int i=0; i<col; i++ )
{
for(int j=0; j<row; j++)
{
temp.arr[i][j]=arr[j][i];
cout<<setw(5)<<temp.arr[i][j];
1
7
6
}
cout<<endl;
}
}
int main()
{
int rows,cols;
cout<<"------------------WELCOME-THIS
PROGRAM FINDS THE TRANSPOSE OF A
MATRIX----------------------"<<endl;
1
7
7
cout<<"-----------------------------------------------------------
---------------------------------"<<endl;
cout<<"Input the Matrix elements"<<endl;
Matrix m1(rows,cols);
m1.input();
cout<<"-----------------------------------------------------------
-----------------------------------"<<endl;
cout<<"Inputted Matrix looks like:"<<endl;
m1.print();
cout<<"-----------------------------------------------------------
-----------------------------------"<<endl;
cout<<"The Transpose of a matrix looks
like:"<<endl;
m1.transpose();
return 0;
}
1
7
8
Q73 Write C++ program to create a Matrix Calculator
using Matrix class.
#include<iostream>
#include<iomanip>
using namespace std;
class Matrix
{
private:
int row;
int col;
int mat[5][5];
public:
Matrix();
Matrix(int r,int c);
1
7
9
Matrix add(const Matrix &m);
Matrix sub(const Matrix &m);
Matrix mul(const Matrix &m);
void transpose();
void input();
void print();
};
Matrix::Matrix()
{
row=1;
col=1;
mat[5][5]=0;
}
Matrix::Matrix(int r,int c)
{
row=r;
col=c;
}
Matrix Matrix::add(const Matrix &m)
{
Matrix temp(m.row ,m.col);
int i,j;
for(i=0; i<m.row; i++)
{
for(j=0; j<m.col; j++)
{
temp.mat[i][j]=mat[i][j]+m.mat[i][j];
}
}
return temp;
}
1
8
0
return temp;
}
void Matrix::transpose()
{
Matrix temp(col,row);
for(int i=0; i<col; i++ )
{
for(int j=0; j<row; j++)
{
temp.mat[i][j]=mat[j][i];
cout<<setw(5)<<temp.mat[i][j];
}
cout<<endl;
}
}
1
8
1
cout<<"Enter
element"<<j+1<<":";
cin>>mat[i][j];
}
}
}
int main()
{
cout<<"-------------------WELCOME TO MATRIX
CALCULATOR------------------------------"<<endl;
cout<<"SELECT THE OPERATION THAT YOU
WANT TO PERFORM ON TWO
MATRICES"<<endl;
cout<<"1.ADDITION OF TWO
MATRICES"<<endl;
cout<<"2.SUBTRACTION OF TWO MATRICES
"<<endl;
cout<<"3.MULTIPLICATION OF TWO
MATRICES"<<endl;
cout<<"4.TRANSPOSE OF A MATRIX "<<endl;
cout<<"-----------------------------------------------------------
-------------------"<<endl;
int choice;
cout<<"Enter your choice :";
cin>>choice;
if(choice==1)
{
int row1,row2,col1,col2;
cout<<"YOU HAVE SELECTED ADDITION OF
TWO MATRICES"<<endl;
1
8
2
cout<<"Enter no of rows of Matrix 1:";
cin>>row1;
cout<<"Enter no of columns of Matrix 1:";
cin>>col1;
cout<<"Enter no of rows of Matrix 2:";
cin>>row2;
cout<<"Enter no of cols of Matrix 2:";
cin>>col2;
if (row1!=row2 || col1!=col2)
{
cout<<"Dimensions mismatched"<<endl;
}
else
{
Matrix m1(row1,col1);
Matrix m2(row2,col2);
cout<<"-----------------------------------------------------------
---------------------------------"<<endl;
cout<<"Matrix 1"<<endl;
m1.input();
cout<<"-----------------------------------------------------------
----------------------------------"<<endl;
cout<<"Matrix 2"<<endl;
m2.input();
Matrix m3(row1,col1);
m3=m1.add(m2);
cout<<"-----------------------------------------------------------
-----------------------------------"<<endl;
cout<<"Matrix 1 looks like:"<<endl;
m1.print();
cout<<endl;
cout<<"-----------------------------------------------------------
-------------------------------------"<<endl;
cout<<"Matrix 2 looks like:"<<endl;
m2.print();
cout<<endl;
1
8
3
cout<<"-----------------------------------------------------------
-------------------------------------"<<endl;
cout<<"Result of addition of two matrices
is:"<<endl;
m3.print();
}
}
else if(choice==2)
{
int row1,col1,row2,col2;
cout<<"---------------------YOU HAVE
SELECTED SUBTRACTION OF TWO
MATRICES-------------------------------"<<endl;
cout<<"Enter no of rows of Matrix 1:";
cin>>row1;
cout<<"Enter no of columns of Matrix 1:";
cin>>col1;
cout<<"Enter no of rows of Matrix 2:";
cin>>row2;
cout<<"Enter no of cols of Matrix 2:";
cin>>col2;
if (row1!=row2 || col1!=col2)
{
cout<<"Dimensions mismatched"<<endl;
}
else
{
Matrix m1(row1,col1);
Matrix m2(row2,col2);
cout<<"-----------------------------------------------------------
---------------------------------"<<endl;
cout<<"Matrix 1"<<endl;
m1.input();
cout<<"-----------------------------------------------------------
----------------------------------"<<endl;
cout<<"Matrix 2"<<endl;
m2.input();
Matrix m3(row1,col1);
m3=m1.sub(m2);
1
8
4
cout<<"-----------------------------------------------------------
-----------------------------------"<<endl;
cout<<"Matrix 1 looks like:"<<endl;
m1.print();
cout<<endl;
cout<<"-----------------------------------------------------------
-------------------------------------"<<endl;
cout<<"Matrix 2 looks like:"<<endl;
m2.print();
cout<<endl;
cout<<"-----------------------------------------------------------
-------------------------------------"<<endl;
cout<<"Result of subtraction of two matrices
is:"<<endl;
m3.print();
}
}
else if(choice==3)
{
int row1,col1,row2,col2;
cout<<"---------------------YOU HAVE
SELECTED MULTIPLICATION OF TWO
MATRICES-------------------------------"<<endl;
cout<<"Enter no of rows of Matrix 1:";
cin>>row1;
if(col1==row2)
{
Matrix m1(row1,col1);
Matrix m2(row2,col2);
1
8
5
cout<<"-----------------------------------------------------------
---------------------------------"<<endl;
cout<<"Matrix 1"<<endl;
m1.input();
cout<<"-----------------------------------------------------------
----------------------------------"<<endl;
cout<<"Matrix 2"<<endl;
m2.input();
cout<<"-----------------------------------------------------------
-----------------------------------"<<endl;
cout<<"Matrix 1 looks like:"<<endl;
m1.print();
cout<<"-----------------------------------------------------------
-------------------------------------"<<endl;
cout<<"Matrix 2 looks like:"<<endl;
m2.print();
cout<<"-----------------------------------------------------------
-------------------------------------"<<endl;
cout<<"Result of multiplication of two matrices
is:"<<endl;
Matrix m3;
m3=m1.mul(m2);
m3.print();
}
else
{
cout<<"Dimensions mismatched"<<endl;
}
}
else if(choice==4)
{
int rows,cols;
1
8
6
cout<<"------------------YOU HAVE SELECTED
THE TRANSPOSE OF A
MATRIX----------------------"<<endl;
cout<<"Enter no of rows of Matrix :";
cin>>rows;
cout<<"Enter no of columns of Matrix :";
cin>>cols;
cout<<"-----------------------------------------------------------
---------------------------------"<<endl;
cout<<"Input the Matrix elements"<<endl;
Matrix m1(rows,cols);
m1.input();
cout<<"-----------------------------------------------------------
-----------------------------------"<<endl;
cout<<"Inputted Matrix looks like:"<<endl;
m1.print();
cout<<"-----------------------------------------------------------
-----------------------------------"<<endl;
cout<<"The Transpose of a matrix looks
like:"<<endl;
m1.transpose();
return 0;
}
}
1
8
7
1
8
8
Q73 Write C++ program to create a Matrix Calculator
without using Matrix Class.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout<<"--------------------------------WELCOME TO MATRIX
CALCULATOR-----------------------------------------"<<endl;
cout<<"Kindly Select what operation you need to
perform :"<<endl;
cout<<"1.ADDITION OF TWO MATRICES"<<endl;
cout<<"2.SUBTRACTION OF TWO MATRICES "<<endl;
cout<<"3.MULTIPLICATION OF TWO MATRICES
"<<endl;
cout<<"4.TRANSPOSE OF A MATRIX "<<endl;
int choice;
cout<<"Enter your choice:";
cin>>choice;
cout<<"-----------------------------------------------------------------
--------------------------------------"<<endl;
if(choice==1)
{
cout<<"You have selected Addition of two
Matrices"<<endl;
int m1[5][5],m2[5][5],result[5][5];
int row1;
int col1;
int row2;
int col2;
cout<<"Enter the rows of matrix 1:";
cin>>row1;
cout<<"Enter the columns of matrix 1:";
cin>>col1;
cout<<"Enter the rows of matrix 2:";
cin>>row2;
cout<<"Enter the columns of matrix 2 :";
1
8
9
cin>>col2;
if(row1!=row2 || col1!=col2)
{
cout<<"Dimension mismatched for
addition"<<endl;
}
else
{
//Entering the elements of rows and columns of matrix
cout<<"________________________________________
________"<<endl;
cout<<"ELEMENTS OF MATRIX 1"<<endl;
for(int i=0 ; i<row1; i++)
{
cout<<"ROW "<<i+1<<" ELEMENTS"<<endl;
for(int j=0 ; j<col1 ; j++)
{
cout<<"Enter column"<<j+1<<":";
cin>>m1[i][j];
}
}
cout<<"________________________________________
___________"<<endl;
cout<<"ELEMENTS OF MATRIX 2"<<endl;
for(int i=0 ; i<row2 ; i++)
{
cout<<"ROW "<<i+1<<" ELEMENTS"<<endl;
for(int j=0;j<col2;j++)
{
cout<<"Enter column"<<j+1<<":";
cin>>m2[i][j];
}
}
cout<<"________________________________________
______________"<<endl;
cout<<"THE TWO MATRICES LOOKS LIKE "<<endl;
cout<<"MATRIX 1 WILL BE :"<<endl;
for(int i=0;i<row1;i++)
{
for(int j=0;j<col1;j++)
1
9
0
{
cout<<setw(5)<<m1[i][j];
}
cout<<endl;
}
cout<<"MATRIX 2 WILL BE :"<<endl;
for(int i=0;i<row2;i++)
{
for(int j=0;j<col2;j++)
{
cout<<setw(5)<<m2[i][j];
}
cout<<endl;
}
cout<<"THE RESULTANT MATRIX LOOKS LIKE:"<<endl;
for(int i=0;i<row1;i++)
{
for(int j=0;j<col1;j++)
{
result[i][j]=m1[i][j]+m2[i][j];
cout<<setw(5)<<result[i][j];
}
cout<<endl;
}
}
}
else if (choice==2)
{
cout<<"You have selected Subtraction of two
matrices"<<endl;
int m1[5][5],m2[5][5],result[5][5];
int row1;
int col1;
int row2;
int col2;
cout<<"Enter the rows of matrix 1:";
cin>>row1;
cout<<"Enter the columns of matrix 1:";
cin>>col1;
cout<<"Enter the rows of matrix 2:";
cin>>row2;
cout<<"Enter the columns of matrix 2:";
1
9
1
cin>>col2;
if(row1!=row2 || col1!=col2)
{
cout<<"Dimensions mismatched for
subtraction"<<endl;
}
else
{
//Entering the elements of rows and columns of matrix
cout<<"________________________________________
________"<<endl;
cout<<"ELEMENTS OF MATRIX 1"<<endl;
for(int i=0 ; i<row1 ; i++)
{
cout<<"ROW "<<i+1<<" ELEMENTS"<<endl;
for(int j=0 ; j<col1 ; j++)
{
cout<<"Enter column"<<j+1<<":";
cin>>m1[i][j];
}
}
cout<<"________________________________________
___________"<<endl;
cout<<"ELEMENTS OF MATRIX 2"<<endl;
for(int i=0 ; i<row2 ; i++)
{
cout<<"ROW "<<i+1<<" ELEMENTS"<<endl;
for(int j=0;j<col2;j++)
{
cout<<"Enter column"<<j+1<<":";
cin>>m2[i][j];
}
}
cout<<"________________________________________
______________"<<endl;
cout<<"THE TWO MATRICES LOOKS LIKE "<<endl;
cout<<"MATRIX 1 WILL BE :"<<endl;
for(int i=0;i<row1;i++)
{
for(int j=0;j<col1;j++)
{
1
9
2
cout<<setw(5)<<m1[i][j];
}
cout<<endl;
}
cout<<"MATRIX 2 WILL BE :"<<endl;
for(int i=0;i<row2;i++)
{
for(int j=0;j<col2;j++)
{
cout<<setw(5)<<m2[i][j];
}
cout<<endl;
}
cout<<"THE RESULTANT MATRIX LOOKS LIKE:"<<endl;
for(int i=0;i<row1;i++)
{
for(int j=0;j<col1;j++)
{
result[i][j]=m1[i][j]-m2[i][j];
cout<<setw(5)<<result[i][j];
}
cout<<endl;
}
}
}
else if(choice==3)
{
cout<<"You have selected Multiplication of two
Matrices"<<endl;
int m1[2][3] , m2[3][2];
int row1 , row2, col1 , col2;
cout<<"MATRIX 1"<<endl;
cout<<"Enter the row of matrix 1:";
cin>>row1;
cout<<"Enter the column of matrix 1:";
cin>>col1;
cout<<"MATRIX 2"<<endl;
cout<<"Enter the row of matrix 2 :";
cin>>row2;
cout<<"Enter the column of matrix 2 :";
cin>>col2;
if(col1!=row2)
1
9
3
{
cout<<"Dimension mismatch for
multiplication"<<endl;
}
else
{
int result[5][5];
cout<<"ELEMENTS OF MATRIX 1"<<endl;
for(int i=0 ; i<row1 ; i++)
{
cout<<"ROW "<<i+1<<" ELEMENTS"<<endl;
for(int j=0 ; j<col1 ; j++)
{
cout<<"Enter column"<<j+1<<":";
cin>>m1[i][j];
}
}
cout<<"________________________________________
___________"<<endl;
cout<<"ELEMENTS OF MATRIX 2"<<endl;
for(int i=0 ; i<row2 ; i++)
{
cout<<"ROW "<<i+1<<" ELEMENTS"<<endl;
for(int j=0;j<col2;j++)
{
cout<<"Enter column"<<j+1<<":";
cin>>m2[i][j];
}
}
cout<<"________________________________________
______________"<<endl;
cout<<"THE TWO MATRICES LOOKS LIKE "<<endl;
cout<<"MATRIX 1 WILL BE :"<<endl;
for(int i=0;i<row1;i++)
{
for(int j=0;j<col1;j++)
{
cout<<setw(5)<<m1[i][j];
}
cout<<endl;
}
1
9
4
cout<<"MATRIX 2 WILL BE :"<<endl;
for(int i=0;i<row2;i++)
{
for(int j=0;j<col2;j++)
{
cout<<setw(5)<<m2[i][j];
}
cout<<endl;
}
cout<<"THE RESULTANT MATRIX LOOKS LIKE:"<<endl;
for(int i=0; i<row1; i++)
{
for(int j=0;j<col2;j++)
{
result[i][j]=0;
for(int k=0;k<col1;k++)
{
result[i][j]+=m1[i][k]*m2[k]
[j];
cout<<result[i][j]<<"\t";
}
}
cout<<endl;
}
}
}
else if(choice==4)
{
cout<<"You have selected a transpose of a
Matrix"<<endl;
int m[5][5];
int rows;
int cols;
cout<<"Enter the rows of matrix:";
cin>>rows;
cout<<"Enter the columns of matrix:";
cin>>cols;
cout<<endl;
cout<<"ELEMENTS OF MATRIX "<<endl;
for(int i=0 ; i<rows ; i++)
{
cout<<"ROW "<<i+1<<" ELEMENTS"<<endl;
1
9
5
for(int j=0 ; j<cols ; j++)
{
cout<<"Enter column"<<j+1<<":";
cin>>m[i][j];
}
}
cout<<endl;
cout<<"THE INPUTTED MATRIX LOOKS LIKE:"<<endl;
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
cout<<setw(5)<<m[i][j];
}
cout<<endl;
}
int result[cols][5];
int r1 , c1;
r1=cols;
c1=rows;
cout<<endl;
cout<<"THE RESULTANT TRANSPOSE MATRIX LOOKS
LIKE:"<<endl;
for(int i=0 ; i<r1 ; i++)
{
for(int j=0; j<c1 ; j++)
{
result[i][j]=m[j][i];
cout<<setw(5)<<result[i][j];
}
cout<<endl;
}
}
else
{
cout<<"Wrong Input";
}
return 0;
}
1
9
6
1
9
7
Q74 Write C++ program to implement
Matrix Calculator using switch.
#include <iostream>
#include <iomanip>
using namespace std;
class Matrix
{
private:
int row;
int col;
int arr[6][6];
public:
Matrix()
{
row=1;
col=1;
}
1
9
8
row = r;
col = c;
}
void input()
{
for(int i=0; i<row; i++)
{
cout<<"For row "<<i + 1<<endl;
{
for(int j=0; j<col; j++)
cin>>arr[i][j];
void print()
{
cout<<"________\n\n";
}
cout<<"________\n\n";
};
1
9
9
Matrix Matrix :: add(const Matrix& m)
{
Matrix temp(m.row, m.col);
for(int i=0; i<row; i++){
for(int j=0; j<col; j++)
{
temp.arr[i][j]=arr[i][j]+m.arr[i][j];
}
}
return temp;
}
}
}
return temp;
}
}
cout<<endl;
}
cout<<"________\n\n";
}
int main()
{
cout<<"\tThis programs performs matrix functions\n\
n";
string res="y";
while(1)
{
int r, c, r1, c1, choice;
cout<<"\nFunctions:\n"<<
"1. Add two matrices\n"<<
"2. Subtract two matrices\n"<<
"3. Multiply two matrices\n"<<
"4. Transpose of a matrix\n"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice){
case 1:{
give(r,c,r1,c1);
if(r!=r1 || c!=c1)
{
cout<<"Error : Dimensions mismatch..."<<endl;
}
else{
Matrix m1(r,c);
cout<<"For the first matrix:\n";
2
0
1
m1.input();
m1.print();
Matrix m2(r1,c1);
cout<<"For the second matrix:\n";
m2.input();
m2.print();
Matrix m3(r,c);
m3=m1.add(m2);
cout<<" Added matrix\n";
m3.print();
}
}
break;
case 2:{
give(r,c,r1,c1);
if(r != r1 || c != c1 )
{
cout<<"Error : Dimensions mismatch..."<<endl;
}
else{
Matrix m1(r,c);
cout<<"For the first matrix:\n";
m1.input();
m1.print();
Matrix m2(r1,c1);
cout<<"For the second matrix:\n";
m2.input();
m2.print();
Matrix m3(r,c);
m3=m1.sub(m2);
cout<<" Subtracted matrix\n";
m3.print();
}
}
case 3:{
give(r,c,r1,c1);
if(c1!=r)
{
cout<<"Error : Dimensions mismatch..."<<endl;
}
else{
Matrix m1(r,c);
2
0
2
cout<<"For the first matrix:\n";
m1.input();
m1.print();
Matrix m2(r1,c1);
cout<<"For the second matrix:\n";
m2.input();
m2.print();
Matrix m3(r,c1);
m3=m1.mul(m2);
cout<<" Multiplied matrix\n";
m3.print();
}
}
case 4:{
cout<<"For matrix: \n";
cout<<"Enter the number of rows in matrix: ";
cin>>r;
cout<<"Enter the number of columns in matrix: ";
cin>>c;
Matrix m1(r,c);
m1.input();
m1.print();
Matrix m3(c,r);
cout<<" \nTranspose of matrix\n";
m1.transpose();
}
default:
{
cout<<"Oops! Wrong input...";
}
cout<<"\nDo you want to continue? (y/n): ";
cin>>res;
if(res=="y")
continue;
else if(res=="n")
cout<<"Thank you!";
return 0;
}
}
}
2
0
3
2
0
4
Q75 Write C++ program to swap two numbers using
pointers.
#include<iostream>
using namespace std;
void exchangebyPointers(int *px,int *py)
{
int temp;
temp=*px;
*px=*py;
*py=temp;
return ;
}
int main()
{
int a,b;
cout<<"Swapping of two numbers"<<endl;
cout<<"Enter first number:";
cin>>a;
cout<<"Enter second number:";
cin>>b;
cout<<"Before Swapping, the numbers
are :"<<endl;
cout<<"a="<<a<<"b="<<b<<endl;
cout<<"After Swapping,the numbers
are:"<<endl;
exchangebyPointers(&a,&b);
cout<<"a="<<a<<"b="<<b;
return 0;
}
2
0
5
2
0
6
Q76 Write C++ program to create a polygon class having three
derived classes: Triangle, Square and Rectangle.
#include <iostream>
#include <cmath>
using namespace std;
//Base Class
class Polygon
{
protected:
double area; //area of polygon
double perimeter; //perimeter of polygon
public:
//printing the data members
void printar()
{
cout<<"The area is: "<<area;
}
void printperi()
{
cout<<"The perimeter is: "<<perimeter;
}
};
//Derived classes
class Triangle : public Polygon
{
private:
float s1,s2,s3;
public:
float calcPeri()
{
//calculating triangle's perimeter
float perimeter=s1+s2+s3;
return perimeter;
}
float calcArea()
{
//calculating triangle's area
2
0
7
float s=(calcPeri())/2;
//using heron's formula
float area;
area=sqrt(s*(s-s1)*(s-s2)*(s-s3));
return area;
}
Triangle(float s1x, float s2x, float s3x)
{
s1=s1x;
s2=s3x;
s3=s3x;
}
};
class Square : public Polygon
{
private:
float s;
public:
float calcPeri()
{
//calculating square's perimeter
int perimeter=4*s;
return perimeter;
}
float calcArea()
{
//calculating square's area
float area=s*s;
return area;
}
Square (float sx)
{
s=sx;
}
};
class Rectangle : public Polygon
{
private:
float l,b;
public:
2
0
8
float calcPeri()
{
//calculating triangle's perimeter
float perimeter=2*(l+b);
return perimeter;
}
float calcArea()
{
//calculating rectangle's area
float area=l*b;
return area;
}
2
0
9
{
float side;
cout<<"\nEnter the side of square:";
cin>>side;
if (side!=0)
{
Square s1(side);
if(ch2==1)
cout<<"\nThe perimeter is:
"<<s1.calcPeri()<<endl;
else if(ch2==2)
cout<<"\nThe area is:
"<<s1.calcArea()<<endl;
}
else
cout<<"Invalid side!\n;
}
if (choice==2)
{
float l,b;
cout<<"\nEnter the length of rectangle:";
cin>>l;
cout<<"Enter the breadth of rectangle:";
cin>>b;
if (l!=0 && b!=0)
{
Rectangle r1(l,b);
if(ch2==1)
cout<<"\nThe perimeter is:
"<<r1.calcPeri()<<endl;
else if(ch2==2)
cout<<"\nThe area is:
"<<r1.calcArea()<<endl;
}
else
cout<<"Invalid sides!\n";
}
if (choice==3)
{
float s1,s2,s3;
2
1
0
cout<<"\nEnter the three sides of
triangle(seperated by space):";
cin>>s1>>s2>>s3;
if (s1+s2<=s3 || s1+s3<=s2 || s2+s3<=s1)
cout<<"Invalid sides!\n";
else
{
Triangle t1(s1,s2,s3);
if(ch2==1)
cout<<"\nThe perimeter is:
"<<t1.calcPeri()<<endl;
else if(ch2==2)
cout<<"\nThe area is:
"<<t1.calcArea()<<endl;
}
}
}
else
cout<<"\nInvalid option selected!";
}
else
{
cout<<"\nInvalid option selected!";
exit(100);
}
cout<<"\nDo you want to continue? (y/n): ";
cin>>o;
if(o=="y")
continue;
else {
cout<<"\nThank you!";
break;}
}
return 0;
}
2
1
1
2
1
2
Q77 Write C++ program to create a matrix class using
pointers.
#include <iostream>
#include <iomanip>
using namespace std;
class Matrix
{
private:
int r;
int c;
int **k;
2
1
3
public:
Matrix()
{
int i,j;
cout<<"Enter no. of rows and
columns:";
cin>>i>>j;
r=i;
c=j;
}
void give()
{
int **ptr;
ptr=new int* [r];
for (int i=0; i<r; i++)
{
ptr[i]=new int[c];
}
k=ptr;
for (int i=0; i<r ; i++)
{
cout<<"Enter integers for row
"<<i+1<<"below: "<<endl;
for (int j=0; j<c;j++)
cin>>ptr[i][j];
cout<<endl;
}
}
void printmat(){
cout<<"The Matrix is: \n";
for (int i=0; i<r ; i++)
2
1
4
{
for (int j=0; j<c;j++)
cout<<setw(5)<<k[i][j];
cout<<endl;
}
}
};
int main()
{
Matrix m;
m.give();
m.printmat();
}
2
1
5
Q78 Write C++ program to create student class having
member functions: Name, Roll no, Year, class, Total Marks.
#include<iostream>
using namespace std;
class student
{
private:
string name;
int rollno;
int Class;
2
1
6
int year;
int TotalMarks;
public:
student();
void input();
void display();
};
student::student()
{
int r , c ,y,tm;
string n;
n=name;
r=rollno;
c=Class;
y=year;
tm=TotalMarks;
}
void student::input()
{
cout<<"Enter the name of the student:";
cin>>name;
cout<<"Enter the rollno of the student:";
cin>>rollno;
cout<<"Enter the class of the student:";
cin>>Class;
cout<<"Enter the year of admission:";
cin>>year;
cout<<"Enter the total marks of
student:";
2
1
7
cin>>TotalMarks;
cout<<"--------------------------------------------------------"<<endl;
}
void student::display()
{
cout<<"Name of the student
is:"<<name<<endl;
cout<<"Rollno of the student
is :"<<rollno<<endl;
cout<<"Class of the student
is:"<<Class<<endl;
cout<<"Academic year is:"<<year<<endl;
cout<<"The total marks of student is :"<<TotalMarks<<endl;
}
int main()
{
cout<<"---------------------------------
RECORDS OF
STUDENTS------------------------------"<<endl;
int num;
cout<<"How many students you want to
enter:";
cin>>num;
cout<<"----------------------------------------------
-----------------------------------"<<endl;
cout<<"Input the details of all the
students"<<endl;
cout<<"----------------------------------------------
-----------------------------------"<<endl;
student s1;
2
1
8
for(int i=0;i<num;i++)
{
cout<<"----------------------------------------------
--------------------------------"<<endl;
cout<<"Input details of
student"<<i+1<<":"<<endl;
s1.input();
//cout<<"-------------------------------------------
------------------------------------"<<endl;
cout<<"The details of
student"<<i+1<<":"<<endl;
s1.display();
}
return 0;
}
2
1
9
Q79 Write C++ program to create a class Complex
having data members real and imaginary to implement
addition, subtraction, multiplication of complex class.
#include<iostream>
using namespace std;
class Complex
{
private:
int real;
int imaginary;
2
2
0
public:
Complex(int r,int i)
{
real=r;
imaginary=i;
}
Complex add(const Complex& a);
Complex sub(const Complex& s);
Complex mul(const Complex& m);
void print();
};
Complex Complex::add(const Complex&
a)
{
Complex temp(a.real,a.imaginary);
temp.real=real+a.real;
temp.imaginary=imaginary+a.imaginary;
return temp; //returning object temp
with real part and complex part
}
Complex Complex::sub(const Complex& s)
{
Complex temp(s.real,s.imaginary);
temp.real=real-s.real;
temp.imaginary=imaginary-s.imaginary;
return temp;
}
Complex Complex::mul(const Complex&
m)
{
Complex temp(m.real,m.imaginary);
2
2
1
temp.real=real*m.real-
imaginary*m.imaginary;
temp.imaginary=imaginary*m.real+m.imaginary*real;
return temp;
}
void Complex::print()
{
cout<<real<<"+"<<imaginary<<"i"<<endl;
}
int main()
{
char option;
cout<<"This is a complex number
calculator."<<endl;
cout<<"To add two complex numbers:
Press A"<<endl;
cout<<"To subtract two complex
numbers: Press S"<<endl;
cout<<"To multiply two complex
numbers:Press M"<<endl;
cin>>option;
int r1,r2,i1,i2;
cout<<"Enter the real part of first
number:";
cin>>r1;
cout<<"Enter the imaginary part of first
number:";
cin>>i1;
cout<<"Enter the real part of second
number:";
cin>>r2;
2
2
2
cout<<"Enter the imaginary part of
second number:";
cin>>i2;
Complex c1(r1,i1);
Complex c2(r2,i2);
cout<<"The first complex number looks
like:";
c1.print();
cout<<"The second complex number
looks like:";
c2.print();
if(option=='A' || option=='a')
{
Complex c3(0,0);
c3=c1.add(c2);
cout<<"The sum looks like:";
c3.print();
}
else if(option=='S' || option=='s')
{
Complex c3(0,0);
c3=c1.sub(c2);
cout<<"The difference looks like:";
c3.print();
}
else if(option=='M' || option=='m')
{
Complex c3(0,0);
c3=c1.mul(c2);
cout<<"The product looks like:";
c3.print();
2
2
3
}
else
cout<<"The entered option is invalid";
return 0;
}
2
2
4
Q80 Write C++ program to define a class Person
having name as a data member. Inherit two classes
Student and Employee from Person. Student has
additional attributes as course, marks and year and
Employee has department and Salary. Write display
() method in all the three classes to display the
corresponding attributes. Provide the necessary
methods to show runtime polymorphism.
#include<iostream>
#include<cstdio>
#include<cstring>
2
2
5
#include<string>
using namespace std;
class person
{
public:
string name;
virtual void get();
virtual void display();
};
void person::get()
{
cout<<"Enter name :";
getline(cin,name);
return;
}
void person::display()
{
cout<<"Name:";
cout<< name;
return;
}
2
2
8
2
2
9
-ll COMMAND LINE ll-
Q81. Write C++ program to count the number of
integers entered by the user.
#include<iostream>
using namespace std;
2
3
0
Q 82. Write C++ program to print the command line
arguments entered by the user.
#include<iostream>
using namespace std;
2
3
1
2
3
2
Q 83. Write C++ program to perform the addition of two
numbers entered by the user.
#include<iostream>
#include<cstdlib>
using namespace std;
int main(int argc , char *argv[])
{
int sum=0,var;
for(int i=1 ; i<argc;i++)
{
var=atoi(argv[i]);
sum=sum+var;
}
cout<<"The sum of numbers
is :"<<sum<<endl;
return 0;
}
2
3
3
Q 84. Write C++ program that
prints a table indicating the number
of occurrences of each alphabet in
2
3
4
the text entered as command line
arguments
#include<iostream>
using namespace std;
#include<iomanip>
#include<cstring>
int main(int argc,char *argv[])
{
int i,j=0;
int count[26]={0};
string st="";
char c,m;
cout<<"\n\tThe text is:";
for(i=0;i<argc;i++)
cout<<argv[i];
for(i=1;i<argc;i++)
st+=argv[i];
cout<<"\n\tnow string is:"<<st;
cout<<"\n\tLenght is:"<<st.length();
int len;
len=st.length();
cout<<"\n\
tcharacters"<<setw(15)<<"frequency
";
for(i=0;i<len;i++)
{
if(st[i]>'a'&& st[i]<='z')
2
3
5
{
c=st[i];
c=c-32;
st[i]=c;
}
m=st[i];
if(m>='A'&&m<='Z')
{
count[m-65]++;
}
}
for(i=0;i<26;i++)
{
if(count[i]!=0)
{
m=static_cast<char>(i+65);
cout<<"\n\
t"<<m<<setw(15)<<count[i];
}
}
return 0;
}
2
3
6
Q. 85
Create a class Box containing
length, breadth and height.
Include following methods in it:
a)Calculate its volume
2
3
7
b)Overload predecrement
operator
c)check if it is a cube or cuboid
#include <iostream>
using namespace std;
class Box {
private:
float length;
float breath;
float height;
public:
//Constructor
Box(): length(0),breath(0),height(0) { }
//Constructor
Box(float length,float breath,float height){
this->length=length;
this->breath=breath;
this->height=height;
}
//Calculate Volume
float calculateVolume(){
2
3
8
return 0;
}
}
Box& operator= (const Box &box){
this->length=box.length;
this->breath=box.breath;
this->height=box.height;
return *this;
}
bool isCube(){
return (this->length==this->breath)
&& (this->breath==this->height);
}
//void display info about box
2
3
9
void display(){
cout<<"The length: "<<this-
>length<<"\n";
cout<<"The breath: "<<this-
>breath<<"\n";
cout<<"The height: "<<this-
>height<<"\n";
}
};
2
4
0
box.display();
cout<<"\n--box:\n";
--box;
box.display();
cout<<"\n";
if(userBox.isCube()){
cout<<"The box is cube.\n";
}else{
cout<<"The box is cuboid.\n";
}
return 0;
}
2
4
1
2
4
2