Professional Documents
Culture Documents
File PDF
File PDF
#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();
cin>>value;
fact=factorial(value);
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()
cin>>a>>b;
void integer::add()
return (float(a)/b);
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
#include<iostream.h>
#include<conio.h>
void swap(int,int);
int main()
int a,b;
clrscr();
cin>>a>>b;
cout<<endl<<"Before swapping the values are (1) = "<<a<<" and (2) = "<<b<<endl;
swap(a,b);
getch();
int c;
c=a;
a=b;
b=c;
}
b) Call by reference function
#include<iostream.h>
#include<conio.h>
int c;
c=a;
a=b;
b=c;
int main()
int a,b;
clrscr();
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()
cin>>radius;
void calculate()
pi=3.14;
area=pi*radius*radius;
void result()
};
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();
cin>>n;
array=new int[n];
for(i=0;i<n;i++)
cin>>array[i];
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:
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 deposit an amount
To withdraw an amount
#include<iostream.h>
#include<conio.h>
#include<string.h>
class bank
int acno;
char nm[10],acctype[10];
float bal;
public:
accno=acno;
strcpy(nm,name);
strcpy(acctype,acc_type);
bal=bal;
}
void deposit()
int depamt;
cin>>depamt;
bal=bal+depamt;
void withdraw()
int wamt;
cin>>wamt;
if(wamt>bal)
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;
cin>>accno;
cin>>name;
cin>>acc_type;
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()
cin>>roll;
cin>>name;
void display()
};
int m1,m2;
public:
void get()
{
getdata();
cin>>m1;
cin>>m2;
void show()
display();
};
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()
cin>>roll;
cin>>name;
void display()
};
int m1,m2;
public:
void get()
{
getdata();
cin>>m1;
cin>>m2;
void show()
display();
};
{int a,w;
public:
void in()
get();
cin>>a;
cin>>w;
void out()
{
show();
};
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;
};
void getdata()
person::getdata();
cin>>institute;
cout<<"Year: ";
cin>>level;
void display()
person::display();
cout<<"Year: "<<level<<endl;
};
char company[100];
float salary;
public:
void getdata()
person::getdata();
cin>>company;
cout<<"Salary: Rs.";
cin>>salary;
void display()
person::display();
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...";
};
public:
void eat()
cout<<"Drinking milk...";
};
int main()
clrscr();
cat c1;
c1.eat();
getch();
return 0;
}
Amity Institute of Information Technology