Jaycpp
Jaycpp
Jaycpp
Sol:
#include <iostream>
int main()
OUTPUT
Q2. Write a program to generate electricity bill.
Sol:
#include <iostream>
int main()
int cid;
cout << "\tTotal Reading = " << curr - prev << endl;
else
{
OUTPUT
Q3. Write a program to convert a character from uppercase to lowercase and vice versa using
ASCII value.
Sol:
#include<iostream>
int main()
char ch;
ch = ch+32;
ch = ch-32;
}
OUTPUT
Q4. Write a program to check whether the entered character is a consonant or a vowel.
Sol:
#include<iostream>
int main()
char ch;
else
}
OUTPUT
Q5. Write a program to enter the marks and print grades.
Sol:
#include<iostream>
int main()
int marks;
{
cout << "\nGrade E";
else
OUTPUT
Q6. Write a program to print the days of a week using switch case.
Sol:
#include <iostream>
int main()
int c;
cin >> c;
switch (c)
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
case 5:
break;
case 6:
case 7:
break;
default:
OUTPUT
Q7. Write a program to create an arithmetic calculator.
Sol:
#include <iostream>
int main()
int c;
cin >> c;
switch (c)
case 1:
int add;
int num1,num2;
break;
case 2:
int sub;
cout << "\nEnter the numbers you want to SUBTRACT :";
int num3,num4;
break;
case 3:
int mul;
int num5,num6;
break;
case 4:
float div;
int num7,num8;
break;
case 5:
int mod;
int num9,num10;
break;
default:
OUTPUT
Q8. Write a program to check whether inputted number is palindrome or not.
Sol:
#include<iostream>
int main()
check1 = check;
if (rev == check1)
else
}
OUTPUT
Q9. Write a program to find the integers & sum of all integers greater than 100 & less than 200
that are divisible by 7.
Sol:
#include<iostream>
int main()
int s = 0;
if (i%7 == 0)
s = s+i;
OUTPUT
Q10. Write a program to generate the Fibonacci series upto a given number.
Sol:
#include<iostream>
int main()
int n;
cout << "\nEnter the number of terms you want to print the Fibonacci Series : ";
cin >> n;
int f = 0, s = 1, t = 0;
t = f+s;
f = s;
s = t;
OUTPUT
Q11. Write a program to find the product of the sums of all odd and even digits in a given number.
For example if the number is 2314568 then the program should calculate (2+4+6+8)*(3+1+5)
Sol:
#include<iostream>
int main()
int n;
cin >> n;
while (n > 0)
int d = n % 10;
if (d % 2 == 0)
evsum = evsum + d;
else
odsum = odsum + d;
n = n / 10;
cout << "\n\n\nProduct of sum of odd and even digits of your given number : " << prod;
}
OUTPUT
Q12. Write a program to check whether inputted number is prime or not.
Sol:
#include<iostream>
int main()
int check;
int f = 0;
if (check % i == 0)
f++;
if (f == 2)
else
}
OUTPUT
Q13. Write a program to check whether inputted string is palindrome or not without using string
functions.
Sol:
#include<iostream>
#include<string.h>
int main()
string word;
char ch = word[i];
rev = ch + rev;
if (word == rev)
else
}
OUTPUT
Q14. Write a program to generate the series.
***
*****
*******
*********
Sol:
#include<iostream>
int main()
int sp = 4;
sp--;
}
OUTPUT
Q15. Write a program to generate the series.
***
*****
*******
Sol:
#include<iostream>
int main()
OUTPUT
Q16. Write a program to generate the series.
**
***
****
*****
****
***
**
Sol:
#include<iostream>
int main()
int c = 4;
if (i <= 5)
else
{
c--;
OUTPUT
Q17. Write a program to generate the series.
***
*****
***
Sol:
#include<iostream>
int main()
int sp = 3 , sy = 1;
if (i <= 4)
sp--;
sy = sy + 2;
else
if (i == 5)
sp = 1;
sy = 5;
sp++;
sy = sy - 2;
OUTPUT
Q18. Write a program to generate the series.
212
32123
4321234
543212345
Sol:
#include<iostream>
int main()
int sp = 4 , sy = 2;
cout << k;
cout << l;
sp--;
OUTPUT
Q19. Write a program to generate the series.
ABCDEFGFEDCBA
ABCDEF FEDCBA
ABCDE EDCBA
ABCD DCBA
ABC CBA
AB BA
A A
Sol:
#include<iostream>
int main()
int sp = -1, sy = 0;
if (i == 71 && sy == 0)
{
l = 70;
sy = 1;
sp = sp + 2;
OUTPUT
Q20. Write a Program to Find the Sum OF Digit Using Recursion
Sol:
#include<iostream>
int res = 0;
if (num == 0)
return res;
res = d + add;
return res;
int main()
int n;
cin >> n;
OUTPUT
Q21. Write a program that computesthe value of the following expression. 1+X/1! + X/2! + X/3! +
XN/N!.
Sol:
#include<iostream>
#include<math.h>
int main()
int n , x , u , fact = 1;
cout << "\nEnter the value of n where the series ends : ";
cin >> n;
cin >> x;
float sum = 0;
u = pow(x , i);
fact = fact * j;
fact = 1;
OUTPUT
Q22. Write a program that computes the values of the following. X/1 + X^2/2 +
X^3/3………….X^N/N.
Sol:
#include<iostream>
#include<math.h>
int main()
int n , x , u;
cout << "\nEnter the value of n where the series ends : ";
cin >> n;
cin >> x;
float sum = 0;
u = pow(x , i);
if (i % 2 == 0)
else
}
}
OUTPUT
Q23. Write a program to find the factorial of number using recursion.
Sol:
#include<iostream>
if (x > 1)
else
return true;
int main()
int n;
cout << "Enter the Number you want the Factorial : ";
cin >> n;
cout << "Factorial of "<< n << " : " << factorial (n);
OUTPUT
Q24. Write a program that swaps two integers by reference without using third variable.
Sol:
#include<iostream>
int main()
int a = 5, b = 10;
cout << "\nBefore swap a : " << a << " b : " << b;
a = a * b;
b = a / b;
a = a / b;
cout << "\nAfter swap a : " << a << " b : " << b;
OUTPUT
Q25. Using the concept of function overloading write two versions of power() functions to raise a
number m to a power n. The function takes a float value for m and int type for n in one version
and an int value for both m & n in other version and the return type of the function should be
double. Use a default value of 2 for n to make the function to calculate square when this argument
omitted. Write a main() that calls both the function.
Sol:
#include<iostream>
#include<math.h>
return pow(m,n);
return pow(m,n);
int main()
float num2;
cout << "\n\n" << num1 << " raise to " << num3 << ":" << power (num1,num3);
cout << "\n\n" << num1 << " raise to 2 : " << power (num1);
cout << "\n\n" << num2 << " raise to " << num3 << ":" << power (num2,num3);
cout << "\n\n" << num2 << " raise to 2 : " << power (num2);
OUTPUT
Q26. Define a class employee with the following specifications.
Sol.
Empno integer
Ename 20 characters
Basic,hr,da float
Netpay float
Havedata(): to accept values for empno, ename, basic, hra, da & invoke.
Sol:
#include<iostream>
class Employee
int empno;
char ename[20];
float basic,hra,da,netpay;
float calculate(float,float,float);
public:
void getdata();
void display();
};
return b+h+d;
}
void Employee::getdata()
netpay=calculate(basic,da,hra);
void Employee::display()
cout<<"\nNetpay: "<<netpay;
int main()
Employee one;
one.getdata();
one.display();
}
OUTPUT
Q27. Declare a class student to simulate result preparation system for 10 students with the
following data member rollno, name, marks in 3 subjects, average and grade. After calculating
average marks, grade should be calculated as follows.
Sol.
<50 ”F”
>=90 “A”
Sol:
#include<iostream>
class student
int rollno;
string name;
public:
student()
total=0;average=0;
void getdata();
void display();
};
{
cout<<"\n\nEnter the Rollno:";
cin>>rollno;
for(int i=0;i<3;i++)
cin>>marks[i];
total+=marks[i];
average=total/3;
cout<<"\n\nRollno:"<<rollno;
cout<<"\nName:"<<name;
for(int i=0;i<3;i++)
if(average>=90)
cout<<"\nGrade=A";
cout<<"\nGrade=B";
}
cout<<"\nGrade=C";
cout<<"\nGrade=D";
else
cout<<"\nGrade=F";
int main()
student one[10];
for(int i=0;i<10;i++)
one[i].getdata();
cout<<"\n\n\nStudents details";
for(int j=0;j<10;j++)
one[j].display();
}
}
OUTPUT
Q28. Write a program to explain the concept of static members.
Sol:
#include<iostream>
class static_member
public:
static_member()
nobj++;
cout<<"\nNumber of Object="<<nobj;
};
int main()
static_member obj[50];
obj[1].no_of_object();;
OUTPUT
Q29. Create a class time that has 3 data members int hour, min, sec. Write a program in which one
constructor should initialized these data members to 0 and another should initialized it to some
fixed values. A member function should two objects of type time passed as arguments. A main()
program should create two initialized time objects and one that isn’t initialized. Then it should add
two initialized values together leaving the result in the third variable. Finally it should display the
value of this third variable.
Sol:
#include<iostream>
class Time
int hh,mm,ss;
public:
Time()
hh=mm=ss=0;
hh=a;
mm=b;
ss=c;
void display();
};
if(mm>=60)
{
hh=hh+mm/60;
mm=mm%60;
if(ss>=60)
mm=mm+ss/60;
ss=ss%60;
Time temp;
temp.hh=T.hh+t.hh;
temp.mm=T.mm+t.mm;
temp.ss=T.ss+t.ss;
return temp;
int main()
int h1,h2,m1,m2,s1,s2;
cin>>h1>>m1>>s1;
Time T1(h1,m1,s1);
T1.display();
Time T2(h2,m2,s2);
T2.display();
Time T3=T1.calci(T1,T2);
T3.display();
OUTPUT
Q30. Write a program to swap private data members of two different classes (Friend as a bridge).
Sol:
#include<iostream>
class B;
class A
int num1,num2;
public:
num1=a;
num2=b;
void display(void)
};
class B
int num3,num4;
public:
{
num3=a;
num4=b;
void display(void)
};
x.num1 = y.num3;
y.num3 = temp;
x.num2 = y.num4;
y.num4 = temp1;
int main()
A C1;
B C2;
C1.getdata(100,300);
C2.getdata(200,400);
C1.display();
C2.display();
swap(C1, C2);
C1.display();
C2.display();
OUTPUT
Q31. Define a class distance having private member feet and inch of integer type. Use overloaded
pre increment ++ operator to increment distance and return the result to the main().
Sol:
#include<iostream>
class Distance
int feet,inch;
public:
void getdata();
void display();
Distance operator++()
++inch;
++feet;
return *this;
};
cin>>inch;
cin>>feet;;
if(inch>=12)
{
feet+=inch/12;
inch=inch%12;
int main()
Distance d1;
d1.getdata();
d1.display();
++d1;
d1.display();
OUTPUT
Q32. Define a class string. Use overloaded binary += operator to concatenate two string using
friend function.
Sol:
#include<iostream>
#include<string.h>
class Star
char s[100];
public:
void getdata();
void display();
};
cout<<"\n\nEnter String:";
cin>>s;
cout<<s;
Star s1;
strcpy(s1.s,x.s);
strcat(s1.s,y.s);
return s1;
int main()
str1.getdata();
str2.getdata();
str1.display();
str2.display();
str3=str1+=str2;
str3.display();
OUTPUT
Q33. Write a program to overload << and >> operators used with cout and cin respectively
Sol:
#include <iostream>
class Complex
public:
real = r;
img = i;
};
in>>c.real;
in>>c.img;
return in;
out<<c.real;
out<<" + i"<< c.img;
return out;
int main()
Complex c1;
cin>>c1;
cout<<c1;
OUTPUT
Q34. Write a program to create a base class car containing the following member.
Protected: carname
In the main() function of the program make object of maruti class and ask the user to fill the data
and then display the data with showdata()
Sol: #include<iostream>
class car
protected:
char carname[20];
char customername[30];
public:
void input();
void print_out();
};
int registrationfees;
public:
void readdata();
void showdata();
};
void car::input()
void maruti::readdata()
cin>>customername;
input();
cin>>registrationfees;
void maruti::showdata()
cout<<"\n\nRegistration details";
print_out();
int main()
maruti obj;
obj.readdata();
obj.showdata();
OUTPUT
Q35. An organization wishes to maintain a database of its employees. The database is divided into
a number of Classes whose relationship is shown below. The figure also show the minimum
information required for each class. Specify all the classes and define functions to create the
database and retrieve the individual information as and when required.
Sol:
#include<iostream>
class staff
protected:
float basic;
};
class clerk:staff
char grade;
public:
void getdata();
void showdata();
};
void clerk::getdata()
cin>>empno;
cin>>name;
cin>>basic;
cout<<"\nEnter the grade:";
cin>>grade;
cout<<"\n\n\t\tClerk Details";
cout<<"\nEmployee no:"<<empno;
cout<<"\nEmployee Name:"<<name;
cout<<"\nBasic salary:"<<basic;
class officer:staff
char design[30];
public:
void getdata();
void showdata();
};
void officer::getdata()
cin>>empno;
cin>>name;
cin>>basic;
cout<<"\nEnter the designation of officer:";
cin>>design;
void officer::showdata()
cout<<"\n\n\t\tOfficer Details";
cout<<"\nEmployee no:"<<empno;
cout<<"\nEmployee Name:"<<name;
cout<<"\nBasic salary:"<<basic;
int main()
officer one;
one.getdata();
clerk two;
two.getdata();
one.showdata();
two.showdata();
OUTPUT
Q36. Consider a class network of figure. The class master derives information from both account
and admin classes which in turn derive information from the class person. Define all the four
classes & Write a program to create, update & display the information contained in master objects
(Hybrid inheritance)
Person
Name
basic Account
Pay
Admin
Experience
Master
Name
Code
Pay
Sol:
#include <iostream>
class Person
protected:
float basic;
char name[30];
};
protected:
float pay;
};
protected:
int experience;
};
protected:
int code;
public:
void getdata()
cin>>code;
cin>>name;
cin>>basic;
cin>>pay;
cin>>experience;
void Display();
void Update();
};
void Master::Display()
{
cout<<"\n\t\tDetails as follows";
cout<<"\nCode:"<<code;
cout<<"\nName:"<<name;
cout<<"\nBasic pay:"<<basic;
cout<<"\nSalary:"<<basic+pay;
cout<<"\nYears of experience:"<<experience;
void Master::Update()
int ch;
cout<<"\n\n\tPress 1 for Edit Name\n\tPress 2 for Edit Code\n\tPress 3 for Edit Pay\n\tPress 4 for
Edit Experience";
cin>>ch;
if (ch == 1)
cin>>name;
else if (ch == 2)
cin>>code;
else if (ch == 3)
{
cout<<"\nEnter The Salary:";
cin>>pay;
else if (ch == 4)
cin>>experience;
else
cout <<"\nInvalid";
int main()
Master obj;
obj.getdata();
obj.Display();
obj.Update();
obj.Display();
cout<<endl;
return 0;
OUTPUT
Q37. Consider a class shape. Use this class to store two double type values that could be used to
compute the area of figure. Derive two specific classes triangle and rectangle from the base shape.
Add to the base class a member function display_area() to compute & display the area of figures.
Make a display_area() as a virtual function & redefine this function in the derived classes to suit
their Requirements.
Sol:
#include<iostream>
class shape
protected:
double l1,l2;
public:
};
public:
void getdata();
void display_area();
};
public:
void getdata();
void display_area();
};
void triangle::getdata()
{
cout<<"\nEnter the base of triangle:";
cin>>l1;
cin>>l2;
void triangle::display_area()
double res;
res=(l1*l2)/2;
void rectangle::getdata()
cin>>l1;
cin>>l2;
void rectangle::display_area()
double res;
res=(l1*l2);
int main()
rectangle rec;
rec.getdata();
rec.display_area();
triangle tri;
tri.getdata();
tri.display_area();
OUTPUT
Q38. Write a program to take a rollno from the user and find that rollno in a text file which
contains all students record. If you find that record then delete the particular record from the file.
Sol:
#include<iostream>
#include<fstream>
class student
int roll_no;
char name[40];
float marks;
public:
void getdata();
void display();
int getrno()
return roll_no;
};
cout<<"\tEnter rollno:";
cin>>roll_no;
cout<<"\tEnter name:";
cin>>name;
cin>>marks;
cout<<"\n";
}
cout<<"\tRoll no:"<<roll_no<<"\tName:"<<name<<"\tMarks:"<<marks<<"\n";
int main()
student info[6],s1,stud;
fstream my_file;
my_file.open("student.txt",ios::in|ios::app);
if(!my_file)
my_file.seekg(0);
cout<<"\n\tStudent details\n";
for(int i=0;i<6;i++)
my_file.read((char*)&info[i],sizeof(info[i]));
info[i].display();
my_file.close();
ifstream fio("student.txt",ios::in);
ofstream file("temp.txt",ios::out);
cin>>rno;
while(!fio.eof())
fio.read((char*)&s1,sizeof(s1));
if(s1.getrno()==rno)
s1.display();
found='t';
continue;
else
file.write((char*)&s1,sizeof(s1));
if(found=='f')
fio.close();
file.close();
remove("student.txt");
rename("temp.txt","student.txt");
fio.open("student.txt",ios::in);
while(!fio.eof())
fio.read((char*)&stud,sizeof(stud));
if(fio.eof())
break;
stud.display();
fio.close();
OUTPUT
Q39. Write a program that reads a text file and create another file that is identical except that
every sequence of consecutive blank space is replaced by a single space. Printout both files.
Sol:
#include<iostream>
#include<fstream>
int main()
ofstream of1;
ifstream if1;
of1.open("File.txt",ios::out);
of1<<str;
of1.close();
if1.open("file.txt",ios::in);
while(!if1.eof())
if1>>str;
of1.open("file2.txt",ios::out);
of1<<s;
of1.seekp(0);
of1.close();
if1.close();
ifstream if2;
if2.open("file.txt",ios::in);
string line;
getline(if2,line);
cout<<line;
if2.close();
ifstream if3;
if3.open("file2.txt",ios::in);
string line1;
getline(if3,line1);
cout<<line1;
if3.close();
remove("file.txt");
remove("file2.txt");
OUTPUT
Q40. A file contains a list of telephone numbers in the following form.
John 25551123
Ahmed 26521234
The name contains only one word. Write a program to read the file and output the list in two
columns. Use a class object to store each set of data.
Sol:
#include<iostream>
#include<fstream>
class telephone
char name[50];
long phone;
public:
void getdata();
void display();
};
void telephone::getdata()
cin>>name;
cin>>phone;
void telephone::display()
cout<<"\n"<<name<<"\t"<<phone;
}
int main()
telephone tel[2];
fstream file;
for(int i=0;i<2;i++)
tel[i].getdata();
file.seekg(0);
for(int i=0;i<2;i++)
tel[i].display();
file.close();
OUTPUT
Q41. Write a function template for finding the minimum value contained in an array.
Sol:
#include<iostream>
template<class T>
T min=a[0];
for(int i=0;i<size;i++)
if(a[i]<min)
min=a[i];
return min;
int main()
int a[10],size,i,min1;
cin>>size;
cout<<"\n";
for(i=0;i<size;i++)
cin>>a[i];
}
min1=minimum(a,size);
cout<<min1;
OUTPUT
Q42. Write a program with the following
d) A try block to defect and throw an exception if the condition divide by zero occurs.
Sol:
#include<iostream>
class doubledivison
double a,b;
void div();
};
cin>>a;
cin>>b;
try
if(cin.fail())
{
throw "Bad input!";
if(b==0)
throw 0;
cout<<"\nAns is "<<a/b;
catch(const int n)
cout<<"\n"<<Str;
int main()
doubledivison one;
one.read();
one.div();
OUTPUT