0% found this document useful (0 votes)
42 views

Ques 1 Write A Program To Find The Area of Square, Rectangle, Triangle and Circle

The document contains 7 questions and their C++ program solutions. The questions involve writing programs to: 1) Calculate the area of different shapes (square, rectangle, triangle, circle) 2) Perform mathematical operations (addition, subtraction, multiplication, division, modulus) on two integers based on a switch case selection 3) Find the greatest and smallest number among three numbers using if-else statements 4) Find the greatest number among three numbers using a function 5) Concatenate two strings 6) Find the roots of a quadratic equation 7) Define a Matrix class with member functions to create matrices and perform operations like addition, subtraction, multiplication etc. on matrices.

Uploaded by

Tanu chaudhary
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Ques 1 Write A Program To Find The Area of Square, Rectangle, Triangle and Circle

The document contains 7 questions and their C++ program solutions. The questions involve writing programs to: 1) Calculate the area of different shapes (square, rectangle, triangle, circle) 2) Perform mathematical operations (addition, subtraction, multiplication, division, modulus) on two integers based on a switch case selection 3) Find the greatest and smallest number among three numbers using if-else statements 4) Find the greatest number among three numbers using a function 5) Concatenate two strings 6) Find the roots of a quadratic equation 7) Define a Matrix class with member functions to create matrices and perform operations like addition, subtraction, multiplication etc. on matrices.

Uploaded by

Tanu chaudhary
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

sonali

M.Sc. Electronics
1st Semester
Ques 1 Write a program to find the area of square ,rectangle , triangle
and circle.
Program:-

#include <iostream>

#include<math.h>

using namespace std;

int main(){

int a,are,a1,a2,b1,b2,c1,c2,are1,s,r;

float are2,are3;

cout<<"\n enter the side of square :=";

cin>>a;

are=a*a;

cout<<"\n area of square is :="<<are;

cout<<"\n enter the length and breath of rectangle =";

cin>>a1>>b1;

are1=a1*b1;

cout<<"\n area of rectangle is:="<<are1;

cout<<"\n enter sides of triangle:=";

cin>>a2>>b2>>c2;

s=(a2+b2+c2)/2;

are2=sqrt(s*(s-a2)*(s-b2)*(s-c2));

cout<<"\n area of triangle: = "<<are2;


sonali
M.Sc. Electronics
1st Semester
cout<<"\n enter the value of radius :=";

cin>>r;

are3=3.14*r*r;

cout<<"\n area of circle:="<<are3;

return 0;

Ques 2 write a program that accept two integer values and then manipulate
these values depending upon the operator(+,*,/,-,%)using switch statement.

Program:-

#include <iostream>

#include<math.h>

using namespace std;

int main(){

int ch,a,b,c,c1,c2,c3,c4;

cout<<"1.addtion\n2.subtraction\n3.multiplication\n4.quotient\n5.remainder"
;

cout<<"\n enter your choice :-";


sonali
M.Sc. Electronics
1st Semester
cin>>ch;

cout<<"\n enter the value of a and b";

cin>>a>>b;

switch(ch) {

case 1:

c=a+b;

cout<<"\n sum of a and b is=\t "<<c;

break;;

case 2:

c1=a-b;

cout<<"\n difference of a and b is=\t "<<c1;

break;

case 3:

c2= a*b;

cout<<"\n product of a and b is=\t "<<c2;

break;

case 4 :

c3=a/b;

cout<<"\n quotient of a and b is=\t "<<c3;

break;

case 5:

c4=a%b;
sonali
M.Sc. Electronics
1st Semester
cout<<"\n remainder of a and b is =\t "<<c4;

break;

return 0;

Ques 3 write a program to find the greatest and smallest number among the
three numbers (using switch statement)

Program:-

#include <iostream>

#include<math.h>

using namespace std;

int main(){

int a,b,c,d,e,d1,e1;

cout<<"\n enter the three number:-";

cin>>a>>b>>c;

if (a>b){

d=a;}
sonali
M.Sc. Electronics
1st Semester
else{

d=b;}

if (d>c){

e=d;}

else{

e=c;

cout<<"\n the largest number is "<<e;

if (a>b){

d1=b;}

else{

d1=a;

if (d1>c){

e1=c;}

else{

e1=d1;

cout<<"\n the smallest number is "<<e1;


sonali
M.Sc. Electronics
1st Semester

return 0;

Ques 4 write a program to find the greatest number among the three
numbers(using function)

Program:-

#include <iostream>

#include<math.h>

using namespace std;

int largest(int a , int b , int c);

int main(){

int a,b,c,ans;

cout<<"\n enter the value of a,b and c=\t";

cin>>a>>b>>c;

ans=largest(a,b,c);

cout<<"\n largest number is=\t"<<ans;

return 0;

}
sonali
M.Sc. Electronics
1st Semester

int largest(int a,int b,int c){

int d,e;

if (a>b){

d=a;}

else{

d=b;

if (d>c){

e=d;}

else{

e=c;

return e;

Ques 5 write a program to implement string concatenation.

Program:-

#include <iostream>

using namespace std;

int main(){

string s1,s2,result;
sonali
M.Sc. Electronics
1st Semester
cout<<"\n enter string s1=\t";

cin>>s1;

cout<<"\n enter string s2=\t";

cin>>s2;

result=s1+s2;

cout<<"\n resulting string=\t"<<result;

return 0;

Ques 6 write a program to find roots of quadratic equation ax^2 +bx+c=0.

Program:-

#include <iostream>

#include<math.h>

using namespace std;

int main(){

float a,b,c,d,x1,x2,realPart,imaginaryPart;

cout<<"\nenter the coefficient of a,b and c";


sonali
M.Sc. Electronics
1st Semester
cin>>a>>b>>c;

d=b*b-4*a*c;

if (d == 0){

cout<<"\nroots are equal and real ";

x1= -b/(2*a);

cout<<"x1=x2="<<x1;

if (d > 0){

cout<<"\nroots are different and real";

x1=(-b-sqrt(d))/(2*a);

x2=(-b+sqrt(d))/(2*a);

cout<<"\nx1="<<x1;

cout<<"\nx2="<<x2;

if (d < 0){

cout<<"\nroots and different and complex:";

realPart = -b/(2*a);

imaginaryPart =sqrt(-d)/(2*a);

cout << "\nx1 = " << realPart << "+" << imaginaryPart << "i" ;

cout << "\nx2 = " << realPart << "-" << imaginaryPart << "i" ;

return 0;}
sonali
M.Sc. Electronics
1st Semester

Ques 7 write a class to represent a matrix include member function to perform


following task

1) To create a matrix
2) To add a matrix to another
3) To subtract a matrix to another
4) To multiply a matrix by scalar
5) To divide a matrix by a scalar
6) To transpose a matrix
Program:-
#include <iostream>
using namespace std;
int i1=3,j1=3,i,j,k;
class matrix{
public :
int
a[3][3],b[3][3],c[3][3],mul[3][3],scal[3][3],scall[3][3],transposematrix[3][3
];
void creata();
void creatb();
void add();
void subtract();
void multiplication();
void scalarmul();
void divmul();
sonali
M.Sc. Electronics
1st Semester
void transpose();
};
void matrix::creata(){
cout<<"\n enter the a matrix\t";
for(i=0;i<i1;i++){
for(j=0;j<j1;j++){
cout<<"\n a["<<i<<"]["<<j<<"]";
cin>>a[i][j];
}
}
}
void matrix::creatb(){
cout<<" \n enter the b matrix\t";
for(i=0;i<i1;i++){
for(j=0;j<j1;j++){
cout<<"\n b["<<i<<"]["<<j<<"]";
cin>>b[i][j];
}
}
}
void matrix :: add(){
for(i=0;i<i1;i++){
for(j=0;j<j1;j++){
c[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<i1;i++){
for(j=0;j<j1;j++){
cout<<"\n addtion matix is
\t"<<"["<<i<<"]["<<j<<"]"<<c[i][j];
}
}
}
sonali
M.Sc. Electronics
1st Semester
void matrix :: subtract(){
for(i=0;i<i1;i++){
for(j=0;j<j1;j++){
c[i][j]=a[i][j]- b[i][j];
}
}
for(i=0;i<i1;i++){
for(j=0;j<j1;j++){
cout<<"\n subtract matix is
\t"<<"["<<i<<"]["<<j<<"]"<<c[i][j];
}
}
}
void matrix :: multiplication(){
int sum;
for(i=0;i<i1;i++){
for(j=0;j<j1;j++)
{ sum=0;
for(k=0;k<j1;k++){
sum=sum+a[i][k]*b[k][j];
mul[i][j]=sum;
}
}
}
for(i=0;i<i1;i++){
for(j=0;j<j1;j++){
cout<<"\n multiplication matrix is
\t"<<"["<<i<<"]["<<j<<"]"<<mul[i][j];
}
}
}
void matrix :: scalarmul()
{
sonali
M.Sc. Electronics
1st Semester
for(i=0;i<i1;i++){
for(j=0;j<j1;j++){
scal[i][j]=3*a[i][j];
}
}
for(i=0;i<i1;i++){
for(j=0;j<j1;j++){
cout<<"\n scalar multiplication a[3][3]
matix\t"<<"["<<i<<"]["<<j<<"]"<<scal[i][j];
}
}
}
void matrix :: divmul()
{
for(i=0;i<i1;i++){
for(j=0;j<j1;j++){
scall[i][j]=3/a[i][j];
}
}
for(i=0;i<i1;i++){
for(j=0;j<j1;j++){
cout<<"\n scalar multiplication matix
\t"<<"["<<i<<"]["<<j<<"]"<<scall[i][j];
}
}
}
void matrix :: transpose(){
for(i=0;i<i1;i++){
for(j=0;j<j1;j++){
transposematrix[i][j]=a[j][i];
}
}
for(i=0;i<i1;i++){
sonali
M.Sc. Electronics
1st Semester
for(j=0;j<j1;j++){
cout<<"\n \n transpose matrix is "<<"\t
["<<i<<"][["<<j<<"]"<<transposematrix[i][j];
}
}
}

int main()
{
matrix m;
m.creata();
m.creatb();
m.add();
m.subtract();
m.multiplication();
m.scalarmul();
m.divmul();
m.transpose();
return 0;

}
sonali
M.Sc. Electronics
1st Semester

Ques 8 Create two classes GRADE and STUDENT .the class GRADE has data members
GRADE while STUDENT has data members such as roll no.,name,marks of the students.
Making use of data of both the classes print the roll no.,name,and grade of each student
whose grade is set by the grade class.

Program :

#include<iostream>

#include<string.h>

using namespace std;

int n;

class grade

public:

int m[10][10];

void marks();
sonali
M.Sc. Electronics
1st Semester
}g;

class student

public:

string name[10];

int rollno[10];

void getdata();

}s;

int main()

cout<<"enter the number of students : \n";

cin>>n;

s.getdata();

g.marks();

return (0);

void grade::marks()

for(int i=0;i<n;i++)

int j=0;

cout<<"Enter the marks of "<<s.name[i]<<"\n";

cout<<"Enter the marks of subject1 :";

cin>>m[i][j];
sonali
M.Sc. Electronics
1st Semester
j++;

cout<<"enter the marks of subject2 :";

cin>>m[i][j];

cout<<"Rollno.\t Name\t Total Marks\n";

for(int i=0;i<n;i++)

int sum=0;

for(int j=0;j<2;j++)

sum=sum+m[i][j];

cout<<s.rollno[i]<<" \t";

cout<<s.name[i]<<"\t\t";

cout<<sum<<"\n";

void student:: getdata()

for(int i=0;i<n;i++)

{
sonali
M.Sc. Electronics
1st Semester
cout<<"enter the roll no\n";

cin>>s.rollno[i];

cout<<"enter the name of student\n";

cin>>s.name[i];

Ques 9 program to implement the concept of inheritance

Program

#include <iostream>

using namespace std;


sonali
M.Sc. Electronics
1st Semester
// Base class

class Shape {

protected:

int width;

int height;

public:

void setWidth(int w) {

width = w;

}void setHeight(int h) {

height = h;

} };

// Derived class

class Rectangle: public Shape {

public:

int getArea() {

return (width * height);

};

int main(void) {

Rectangle Rect;

Rect.setWidth(5);

Rect.setHeight(7);
sonali
M.Sc. Electronics
1st Semester
// Print the area of the object.

cout << "Total area: " << Rect.getArea();

return 0;}

Ques 10 program to implement the concept of function overloading .

Program:-

#include <iostream>

using namespace std;

void display(int);

void display(float);

void display(int, float);

int main() {

int a = 5;

float b = 5.5;

display(a);

display(b);

display(a, b);
sonali
M.Sc. Electronics
1st Semester
return 0;

void display(int var) {

cout << "Integer number: " << var << endl;

void display(float var) {

cout << "Float number: " << var << endl;

void display(int var1, float var2) {

cout << "Integer number: " << var1;

cout << " and float number:" << var2;

You might also like