You are on page 1of 50

Q1 – WAP in C++ to find area of a circle.

#include<iostream.h>
#include<conio.h>
void main()
{
float pi, area,r;
clrscr();
pi=3.14;
cout<<"Enter radius of the circle - "<<endl;
cin>>r;
area = pi*r*r;
cout<<endl<<"Area of the circle - "<<area;
getch();
}
Q2 – WAP in C++ to print sum of two numbers.
#include<iostream.h>
#include<conio.h>
void main()
{
float a,b,sum;
clrscr();
cout<<"Enter two numbers - "<<endl;
cin>>a>>b;
sum=a+b;
cout<<endl<<"Sum of two numbers is - "<<sum;
getch();
}
Q3 – WAP in C++ to take input of two numbers and print their sum, product, difference and
average.
#include<iostream.h>
#include<conio.h>
void main()
{
float a,b,sum,product,diff,average;
clrscr();
cout<<"Enter two numbers - "<<endl;
cin>>a>>b;
sum=a+b;
product=a*b;
diff=b-a;
average=(a+b)/2;
cout<<endl<<"Sum of two numbers is - "<<sum<<endl;
cout<<"Product is - "<<product<<endl;
cout<<"Difference of 2nd number from 1st is - "<<diff<<endl;
cout<<"Average is - "<<average;
getch();
}
Q4 – WAP to take input for three numbers and print largest among them (Using if-else
statement).
#include<iostream.h>
#include<conio.h>
void main()
{
float a,b,c;
clrscr();
cout<<"Enter three numbers - "<<endl;
cin>>a>>b>>c;
if(a>b&&a>c)
cout<<"The greatest number is - "<<a<<endl;
else
{
if(b>c)
cout<<"The greatest number is - "<<b<<endl;
else
cout<<"The greatest number is - "<<c<<endl;
}
getch();
}
Q5 – WAP to check whether an integer is positive or negative (Consider 0 as positive, using if-
else).
#include<iostream.h>
#include<conio.h>
void main()
{
float a;
clrscr();
cout<<"Enter a number - "<<endl;
cin>>a;
if(a>=0)
cout<<"Number is positive."<<endl;
else
cout<<"Number is negative."<<endl;
getch();
}
Q6 – WAP to find sum of odd numbers between 1 to n (Using for loop).
#include<iostream.h>
#include<conio.h>
void main()
{
int i,n,sum;
clrscr();
cout<<"Enter the end range - "<<endl;
cin>>n;
sum=0;
for(i=1;i<=n;i+=2)
{
sum=sum+i;
}
cout<<"Sum of odd numbers in the range from 1 to "<<n<<" is - "<<sum<<endl;
getch();
}
Q7 – WAP in C++ to find sum of digits entered by user (Using a loop).
#include<iostream.h>
#include<conio.h>
void main()
{
int i,n,sum;
clrscr();
cout<<"Enter a number - "<<endl;
cin>>i;
n=i;
sum=0;
while(n!=0)
{
sum=sum+n%10;
n=n/10;
}
cout<<"Sum of digits is - "<<sum<<endl;
getch();
}
Q8 – WAP in C++ to find first and last of any number (Using while loop).
#include<iostream.h>
#include<conio.h>
void main()
{
int n,f,l;
clrscr();
cout<<"Enter a number - "<<endl;
cin>>n;
l=n%10;
while(n>=10)
{
n=n/10;
}
f=n;
cout<<"First digit is - "<<f<<endl<<"Last digit is - "<<l<<endl;
getch();
}
Q9 – WAP in C++ to reverse a number using while loop.
#include<iostream.h>
#include<conio.h>
void main()
{
int n,rev,rem;
clrscr();
cout<<"Enter a number - "<<endl;
cin>>n;
rev=0;
while(n!=0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
cout<<"Reversed number is - "<<rev<<endl;
getch();
}
Q10 – WAP in C++ to print first 10 natural numbers (Using do while loop).
#include<iostream.h>
#include<conio.h>
void main()
{
int n;
clrscr();
n=1;
cout<<"First 10 natural numbers are as follow - "<<endl;
do
{
cout<<""<<n<<endl;
n++;
}
while(n<=10);
getch();
}
Q11 – WAP to build a simple calculator using switch case.
#include<iostream.h>
#include<conio.h>
void main()
{
char op;
float a,b;
clrscr();
cout<<"Enter an operand (+,-,*,/) - "<<endl;
cin>>op;
cout<<endl<<"Enter two numbers - "<<endl;
cin>>a>>b;
switch(op)
{
case '+':
cout<<endl<<a+b;
break;
case '-':
cout<<endl<<a-b;
break;
case '*':
cout<<endl<<a*b;
break;
case '/':
cout<<endl<<a/b;
break;
default:
cout<<endl<<"The operand entered is not a valid one!";
break;
}
getch();
}
Q12 – WAP to insert details of a student using structure in C++.
#include<iostream.h>
#include<conio.h>
class student
{
public:
int n,i,j;
struct stud
{
char name[20];
int rollno;
float marks[3];
}s[10];
void get();
void disp();
};
void student::get()
{
cout<<"Enter the number of students whose data needs to be stored - "<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<endl<<"Enter name of the student - ";
cin>>s[i].name;
cout<<endl<<"Enter roll number - ";
cin>>s[i].rollno;
cout<<endl<<"Enter marks in SQL, C++, Python - "<<endl;
for(j=0;j<3;j++)
{
cin>>s[i].marks[j];
}
}
}
void student::disp()
{
for(i=0;i<n;i++)
{
cout<<endl<<"Name of the student is - "<<s[i].name<<endl;
cout<<endl<<"Roll number is - "<<s[i].rollno<<endl;
cout<<endl<<"Marks in SQL, C++, Python are as follows - "<<endl;
for(j=0;j<3;j++)
{
cout<<endl<<s[i].marks[j];
}
}
}
void main()
{
clrscr();
student st;
st.i=0;
st.j=0;
st.get();
st.disp();
getch();
}
Q13 – WAP to insert details of a student using union in C++.
#include<iostream.h>
#include<conio.h>
class student
{
public:
int n,j;
union stud
{
char name[20];
int rollno;
float marks[3];
}s;
void get();
void disp();
};
void student::get()
{
cout<<endl<<"Enter name of the student -"<<endl;
cin>>s.name;
cout<<endl<<"Name of the student is - "<<s.name<<endl;
cout<<endl<<"Enter roll number - ";
cin>>s.rollno;
cout<<endl<<"Roll Number is - "<<s.rollno<<endl;
cout<<endl<<"Enter marks in SQL, C++, Python - "<<endl;
for(j=0;j<3;j++)
{
cin>>s.marks[j];
}
cout<<endl<<"Marks in SQL, C++, Python are as follows -"<<endl;
for(j=0;j<3;j++)
{
cout<<""<<s.marks[j]<<endl;
}
}
void main()
{
clrscr();
student st;
st.j=0;
st.get();
getch();
}
Q14 – WAP to demonstrate pointers using pointer arithmetic.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],n,i;
int *ptr;
cout<<"Enter total numbers to be entered - "<<endl;
cin>>n;
cout<<"Enter "<<n<<" numbers - "<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
ptr=a;
for(i=0;i<n;i++)
{
cout<<endl<<"Address = "<<ptr<<endl;
cout<<"Value at the address = "<<*ptr<<endl;
ptr++;
}
getch();
}
Q15 – WAP to add two matrices using functions..
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[10][10],b[10][10],p,q,i,j,m,n,sum[10][10];
clrscr();
cout<<"Enter rows and columns of the first matrix -"<<endl;
cin>>i>>j;
cout<<endl<<"Enter rows and columns of the second matrix -"<<endl;
cin>>m>>n;
if(i==m&&j==n)
{
cout<<endl<<"Addition is possible!"<<endl;
}
else
{
cout<<endl<<"Addition is not possible!"<<endl;
exit(0);
};
cout<<endl<<"Enter elements of the first matrix -"<<endl;
for(p=0;p<i;p++)
{
for(q=0;q<j;q++)
{
cin>>a[p][q];
}
}
cout<<endl<<"Enter the elements of second matrix -"<<endl;
for(p=0;p<m;p++)
{
for(q=0;q<n;q++)
{
cin>>b[p][q];
}
}
for(p=0;p<i;p++)
{
for(q=0;q<j;q++)
{
sum[p][q]=a[p][q]+b[p][q];
}
}
cout<<endl<<"Sum of the two matrices is -"<<endl;
for(p=0;p<i;p++)
{
for(q=0;q<n;q++)
{
cout<<""<<sum[p][q]<<" ";
}
cout<<endl;
}
getch();
}
Q16 – WAP to find factorial of a number using recursion.

#include<iostream.h>

#include<conio.h>

int main()

int factorial(int);

int fact,value;

clrscr();

cout<<"Enter any number: "<<endl;

cin>>value;

fact=factorial(value);

cout<<endl<<"Factorial of the number is : "<<fact<<endl;

getch();

return 0;

int factorial(int n)

if(n<0)

return(-1);

if(n==0)

return(1);

else

return(n*factorial(n-1));

}
}
Q17 – WAP to design a class operation to add, subtract, multiply and divide two numbers using
inline functions.

#include<iostream.h>

#include<conio.h>

class integer

private:

int a,b;

public:

void getdata();

void add();

int sub()

return(a-b);

void mult();

float div();

void disp();

};

void integer::getdata()

cout<<"Enter two numbers - "<<endl;

cin>>a>>b;

void integer::add()

cout<<endl<<"Numbers are "<<a<<" and "<<b<<endl;


cout<<endl<<"Addition is : "<<a+b<<endl;

inline void integer::mult()

cout<<endl<<"Multiplication is : "<<long (a)*b<<endl;

inline float integer::div()

return (float(a)/b);

inline void integer::disp()

cout<<endl<<"Subtraction is : "<<sub()<<endl;

mult();

void main()

integer x;

clrscr();

x.getdata();

x.add();

x.disp();

cout<<endl<<"Division is : "<<x.div()<<endl;

getch();
}
Q18 – WAP to swap two numbers using

a) Call by value function

#include<iostream.h>

#include<conio.h>

void swap(int,int);

int main()

int a,b;

clrscr();

cout<<"Enter two numbers - "<<endl;

cin>>a>>b;

cout<<endl<<"Before swapping the values are (1) = "<<a<<" and (2) = "<<b<<endl;

swap(a,b);

getch();

void swap(int a,int b)

int c;

c=a;

a=b;

b=c;

cout<<endl<<"Values after swapping are (1) = "<<a<<" and (2) = "<<b<<endl;

}
b) Call by reference function

#include<iostream.h>

#include<conio.h>

void swap(int &a,int &b)

int c;

c=a;

a=b;

b=c;

int main()

int a,b;

clrscr();

cout<<"Enter two numbers - "<<endl;

cin>>a>>b;

cout<<endl<<"Before swapping the values are (1) = "<<a<<" and (2) = "<<b<<endl;
swap(a,b);

cout<<endl<<"After swapping the values are (1) = "<<a<<" and (2) = "<<b<<endl;

getch();

}
Q19 – WAP to find area of a circle using a class.

#include<iostream.h>

#include<conio.h>

class circle

float radius;

float area;

float pi;

public:

circle()

cout<<"\n enter radius:-";

cin>>radius;

void calculate()

pi=3.14;

area=pi*radius*radius;

void result()

cout<<"\n the area of circle = "<<area;

};

void main()

clrscr();
circle c;

c.calculate();

c.result();

getch();

}
Q20 - WAP to create a dynamic array.

#include<iostream.h>

#include<conio.h>

void main()

int *array;

int i,n;

clrscr();

cout<<"enter the sizem of array:-";

cin>>n;

array=new int[n];

cout<<"\n enter array elments :-";

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

cin>>array[i];

cout<<"\n the elements in array is:-";

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

cout<<"\n "<<array[i];

delete[] array;

getch();

}
Q21 - WAP to count number of objects in a class.

#include<iostream.h>

#include<conio.h>

class hriday

public:

static int count;

int data;

hriday()

{count++;}

void getdata()

{}

void showdata()

{cout<<"\n"<<count;}

};

int hriday::count=0 =;

void main()

hriday h, h1,h2;

h.getdata();

h.showdata();

getch();

}
Q22 - WAP define a class to represent bank account including the following members

DATA MEMBERS:-

a)NAME OF DEPOSITER

b) ACCOUNT NUMBER

c) ACCOUNT TYPE

d) BALANCE AMOUNT

MEMBER FUNCTION

To assign the values in constructor

To deposit an amount

To withdraw an amount

Display balance and name

#include<iostream.h>

#include<conio.h>

#include<string.h>

class bank

int acno;

char nm[10],acctype[10];

float bal;

public:

bank(int accno,char *name,char *acc_type,float bal)

accno=acno;

strcpy(nm,name);

strcpy(acctype,acc_type);

bal=bal;

}
void deposit()

int depamt;

cout<<"\n enter deposit amount :-";

cin>>depamt;

bal=bal+depamt;

void withdraw()

int wamt;

cout<<"\n enter withdwral amount :-";

cin>>wamt;

if(wamt>bal)

cout<<"\n low funds to withdwral .";

else

bal=bal-wamt;

void display()

cout<<"\n NAME:-"<<name;

cout<<"\n balance"<<bal;

};

void main()

{
int accno;

char name[10];

char acc_type[10];

float bal;

cout<<"\n HRIDAY FUNDS BANK ";

cout<<"\n enter accno:-";

cin>>accno;

cout<<"\n enter name :-";

cin>>name;

cout<<"\n enter account type:-";

cin>>acc_type;

cout<<"\n enter balance:-";

cin>>bal;

bank b1(accno,name,acc_type,bal);

b1.deposit();

b1.withdraw();

b1.display();

getch();

}
Q23 - WAP to implement simple inheritance.

#include<iostream.h>

#include<conio.h>

class student

int roll;

char name[10];

public:

void getdata()

cout<<"\n enter roll no of student:";

cin>>roll;

cout<<"\n enter student name :-";

cin>>name;

void display()

cout<<"\n student roll no is:-"<<roll;

cout<<"\n student name is :-"<<name;

};

class mark:public student

int m1,m2;

public:

void get()

{
getdata();

cout<<"\n enter the marks obtained in english :-";

cin>>m1;

cout<<"\n enter the marks obtained in maths:-";

cin>>m2;

void show()

display();

cout<<"\n student marks in english:-"<<m1;

cout<<"\n student marks in math:-"<<m2;

};

void main()

clrscr();

mark n;

n.get();

n.show();

getch();

}
Q24 - WAP to implement multiple inheritance.

#include<iostream.h>

#include<conio.h>

class student

int roll;

char name[10];

public:

void getdata()

cout<<"\n enter roll no of student:";

cin>>roll;

cout<<"\n enter student name :-";

cin>>name;

void display()

cout<<"\n student roll no is:-"<<roll;

cout<<"\n student name is :-"<<name;

};

class mark:public student

int m1,m2;

public:

void get()
{

getdata();

cout<<"\n enter the marks obtained in english :-";

cin>>m1;

cout<<"\n enter the marks obtained in maths:-";

cin>>m2;

void show()

display();

cout<<"\n student marks in english:-"<<m1;

cout<<"\n student marks in math:-"<<m2;

};

class physical:public mark

{int a,w;

public:

void in()

get();

cout<<"\n enter age:-";

cin>>a;

cout<<"\n enter weight:-";

cin>>w;

void out()

{
show();

cout<<"\n age is:-"<<a;

cout<<"\n weight is:-"<<w;

};

void main()

clrscr();

physical n;

n.in();

n.out();

getch();

}
Q25 – WAP to implement hierarchical inheritance.

#include <iostream.h>

#include <conio.h>

class person

char name[100],gender[10];

int age;

public:

void getdata()

cout<<"Name: ";

cin>>name;

cout<<"Age: ";

cin>>age;

cout<<"Gender: ";

cin>>gender;

void display()

cout<<"Name: "<<name<<endl;

cout<<"Age: "<<age<<endl;

cout<<"Gender: "<<gender<<endl;

};

class student: public person

char institute[100], level[20];


public:

void getdata()

person::getdata();

cout<<"Name of College/School: ";

cin>>institute;

cout<<"Year: ";

cin>>level;

void display()

person::display();

cout<<"Name of College/School: "<<institute<<endl;

cout<<"Year: "<<level<<endl;

};

class employee: public person

char company[100];

float salary;

public:

void getdata()

person::getdata();

cout<<"Name of Company: ";

cin>>company;

cout<<"Salary: Rs.";
cin>>salary;

void display()

person::display();

cout<<"Name of Company: "<<company<<endl;

cout<<"Salary: Rs."<<salary<<endl;

};

int main()

clrscr();

student s;

employee e;

cout<<"Student"<<endl;

cout<<"Enter data"<<endl;

s.getdata();

cout<<endl<<"Displaying data"<<endl;

s.display();

cout<<endl<<"Employee"<<endl;

cout<<"Enter data"<<endl;

e.getdata();

cout<<endl<<"Displaying data"<<endl;

e.display();

getch();

return 0;

}
Q26 – WAP to override a function.

#include<iostream.h>

#include<conio.h>

class animal

public:

void eat()

cout<<"Eating...";

};

class cat: public animal

public:

void eat()

cout<<"Drinking milk...";

};

int main()

clrscr();

cat c1;

animal &c = c1;

c1.eat();

getch();

return 0;
}
Amity Institute of Information Technology

Object Oriented Programming Using C++


(ES203)

Submitted To: Submitted By:


Dr. Reshu Aggarwal Yuvraj Singh Chaudhry
Assistant Professor A1004818130
AIIT Course – BCA 3-C
Amity University Batch – 2018-21

You might also like