You are on page 1of 22

C++

PROGRAMMS

MADE BY:-

Ashmeet Singh
th
12 Science
1.
Q. Write a program which input value in 2 matrices A and B and
compare them for equality.
#include<iostream.h>
void main()
{
int A[4][4],V[4][4];
int i , j;
for( i=0 ; i<4 ; i++)
{
for( j=0 ; j<4 ; j++)
{
cout<< " \n Enter value in A[ " << i << " ][ " << j << " ] :";
cin>> A[i][j];
cout<< " \n Enter value in B [ " << i << " ][ "<< j << " ]:";
cin>> B[i][j];
}
}
int flag=1;
for( i=0 ; i<4 ; i++)
{
for( j=0 ; j<4 ; j++)
{
if( A[i][j]==B[i][j] )
flag=0;
break;
}
}
if ( flag==0 )
cout<<" \n Matrices are Equal ";
else
cout<<" \n Matrices are not Equal ";
}

Output:-
2.
Q.WAP which reads a matrix of 4 rows and 4 columns and
calculate the sum of both the diagonals
#include<iostream.h>
void main()
{
int A[4][4],B[4][4];
for ( int i=0 ; i<4 ; i++)
{
For ( int j=0 ; j<4 ; j++)
{
cout<<" \n Enter the value in A["<< i << " ][ " << j << " ]: ";
cin>> A[i][j];
}
}
for( i=0 ; i<4 ; i++)
{
for( j=0; j<4 ; j++)
{
if ( i==j )
{
int s+=A[i][j];
}
}
}
cout<<"\nThe sum of the Diagonal is:"<<s;
}
Output:-
3.
Q.WAP which reads a matrix of 4 rows and 4 columns and
display the upper triangular matrix and lower triangular matrix

#include<iostream.h>
void lower ( int A[4][4] )
{
int i , j ;
for ( i=0 ; i<4 ; i++ )
{
for ( j=0 ; j<4 ; j++ )
{
if ( i<j )
{
cout<<" 0 "<<" ";
}
else
cout<< A[i][j] <<" ";
}
cout<< endl ;
}
}

void upper ( int A[4][4] )


{
int i , j ;
for( i=0 ; i<4 ; i++)
{
for( j=0 ; j<4 ; j++ )
{
if( i > j && i=j)
{
cout<<" 0 "<<" ";
}
else
cout<< A[i][j] <<" ";
}
cout<< endl ;
}
}

void main()
{
int A[4][4];
for ( int i=0 ; i<4 ; i++ )
{
for ( int j=0 ; j<4 ; j++ )
{
cout<<" \n Enter the value in A [" << i << " ][ " << j <<" ] :";
cin>>A[i][j];
}
}
cout<<" \n Lower triangular matrix "<<endl;
lower(A);
cout<<" \n Upper Triangular matrix:"<<endl;
upper(A);
}
OUTPUT :-
4.
Q.WAP which read a matrix of 4 rows and 4 columns and
calculate the sum of each row and column from the matrix
#include<iostream.h>
void main()
{
int a[4][4],b[4][4],sr=0,sc=0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
cout<<"\nEnter the value in a["<<i<<"]["<<j<<"]:";
cin>>a[i][j];
}
}
for(i=0;i<4;i++)
{
sr=sr+a[i][0]+a[i][1]+a[i][2]+a[i][3];
}
for(int j=0;j<4;j++)
{
sc=sc+a[0][j]+a[1][j]+a[2][j]+a[3][j];
}
cout<<"\nSum of Rows:"<<sr;
cout<<"\nSum of Columns:"<<sc;
}
OUTPUT:-

5.
Q.WAP which read a matrix of 4 rows and 4 columns and
calculate the sum of alternate elements from the matrix

#include<iostream.h>
void main()
{
int a[4][4],k=0,sum=0,i,j;
cout<<"\nEnter the elements of the matrix:";
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<"\nEnter the element in a ["<<i<<"]["<<j<<"]:";
cin>>a[i][j];
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(k%2==0)
sum+=a[i][j];
k++;
}
}
cout<<"\nSum of alternate elements:";
cout<<sum;
}
OUTPUT:-
6.
Q.WAP which uses overloaded prototypes of function volume
that calculates the volume of cube,cuboid and cylinder
#include<iostream.h>
void volume(float l)
{
cout<<"The volume of the cube is:"<<l*l*l<<endl;
}

void volume(float l,float b,float h)


{
cout<<"The volume of cuboid is:"<<l*b*h<<endl;
}

void volume(float b,float h)


{
cout<<"The volume of cylinder is:"<<3.14*b*h*b<<endl;
}

void main()
{ clrscr();
float l , b , h , ch ;
cout<<” VOLUME CALCULATOR ”<<endl;
cout<<"1. Volume of cube"<<endl;
cout<<"2. Volume of cuboid"<<endl;
cout<<"3. Volume of cylinder"<<endl;
cout<<" Enter your Choice :"<<endl;
cin>>ch;
if ( ch==1 )
{
cout<<"\nEnter the side of the cube:"<<endl;
cin>>l;
volume( l );
}
else if( ch==2 )
{
cout<<"\nEnter the Length of the cuboid:"<<endl;
cin>>l;
cout<<"Enter the Breath of the cuboid:"<<endl;
cin>>b;
cout<<"Enter the Height of the cuboid:"<<endl;
cin>>h;
volume ( l , b , h );
}
else if ( ch==3 )
{
cout<<" Enter the radius of cylinder:"<<endl;
cin>>b;
cout<<" Enter the hieght of the cylinder:"<<endl;
cin>>h;
volume( b , h );
}
else
cout<<” Wrong Choice : ”<<endl;
OUTPUT :-
7.
Q. Define a class Applicant in C++ with the following
description:
PRIVATE MEMBERS :-
1.A data member Ano (Admission Number) of type long
2.A data member Name of type string
3.A data member Agg (Aggregate Marks) of type float
4.A data member Grade of type char
5.A member function GradeMe() to find the Grade as per the
Aggregate Marks obtained by a student. Equivalent Aggregate
Marks range and the respective Grades are shown as follows:
AGGREGATE MARKS GRADE
>=80 A
less than 80 and >= 65 B
less than 65 and >= 50 C
less than 50 D
#include<iostream.h>
#include<stdio.h>
class applicant
{
private:
long admin;
char name[20], grade[20];
float agg ;
grademe()
{
if (agg>=80 )
cout<<"\n Your grade is A";
else if (agg>=65 && agg<85 )
cout<<"\n Your grade is B";
else if(agg>=50 && agg<65 )
cout<<"\n Your grade is C";
else
cout<<"\n Your grade is D";
}
public:
void enter()
{
cout<<"\n Enter the admission number:";
cin>>admin;
cout<<"\n Enter the students name:";
gets(name);
cout<<"\n Enter aggregate marks:";
cin>>agg;
}
void result()
{
cout<<"\n Admission number:"<<admin;
cout<<"\n Name of student:"<<name;
cout<<"\n Aggregate Marks:"<<agg;
grademe();
}
};

void main()
{
applicant a1;
a1.enter();
a1.result();
}
OUTPUT :-
8.
Q. Write a C++ program to perform various operations on a
string class without using language supported built-in string
functions. The operations on a class are:
1.Read a string
2.Display the string
3.Reverse the string
4.Copy the string into an empty string
5.Concatenate two strings

#include<iostream.h>
#include<stdio.h>
class string
{
private:
char s[40], q[40];
public:
void read()
{
cout<<"\n Enter your string:"<<endl;
gets(s);
}
void display()
{
cout<<"\n Your string:"<<endl;
puts(s);
}
void reverse()
{ int m;
for(int i=0;s[i]!=NULL;i++);
m=i ;
cout<<"\n Reverse of the string:"<<endl;
for(i=m-1;i>=0;i--)
cout<<s[i];
}
void copy()
{
for (int i=0;s[i]!=NULL;++i)
{
q[i]=s[i];
}
q[i]=”NULL”;
cout<<"\n String copied:";
puts(q);
}
void concatenate()
{
char r[40];
cout<<"Enter another string:";
gets(r);
for ( int i=0;s[i]!=NULL;++i);
int j=0;
while(r[j]!=NULL)
{
s[i]=r[j];
i++;
j++;
}
s[i]=NULL;
cout<<"Concatenated string:";
puts(s);
}
};

void main()
{
string s1;
s1.read();
s1.display();
s1.reverse();
s1.copy();
s1.concatenate();
}

OUTPUT :-

You might also like