1. a) Write a C++ program to multiply six numbers using default

WWW.VIDYARTHIPLUS.COM
1. a) Write a C++ program to multiply six numbers using default arguments.
#include<iostream.h>
int mul(int a=4,int b=8,int c=3,int d=5, int e=2, int f=3);
void main()
{
cout<<" mul ()="<<mul()<<endl;
cout<<" mul (2)="<< mul (2)<<endl;
cout<<" mul (2,5)="<< mul (2,5)<<endl;
}
int mul (int a, int b, int c, int d, int e, int f)
{
return(a*b*c*d*e*f);
}
b) Write a JAVA program with three threads named as “one”,” two”, “three”
and initiate these threads from the main thread and show the result
class one extends Thread
{
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.println("\t From Thread 1: i= "+i);
}
System.out.println("Exit from 1");
} }
class two extends Thread
{
public void run()
{
for(int j=1;j<=10;j++)
{
System.out.println("\t From Thread 2: j= "+j);
}
System.out.println("Exit from 2");
} }
class three extends Thread
{
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
public void run()
{
for(int k=1;k<=10;k++)
{
System.out.println("\t From Thread 3: k= "+k);
}
System.out.println("Exit from 3");
}
}
class ThreadTest
{
public static void main(String args[])
{
one a=new one();
two b=new two();
three c= new three();
a.start();
b.start();
c.start();
}
}
2. a) Write a C++ program to perform addition of four numbers function
overloading.
#include<iostream.h>
#include<conio.h>
int add(int);
int add(int,int);
float add(float,int,int);
float add(float,float,int,int);
void main()
{
clrscr();
cout<<"\n addition1:"<<add(10);
cout<<"\n addition2:"<<add(5,8);
cout<<"\n addition3:"<<add(6.3,5,5);
cout<<"\n addition3:"<<add(6.3,5.5,5,10);
}
int add (int s)
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
{
return(s+s);
}
int add (int r, int h)
{
return(r+h);
}
float add(float f,int b,int h)
{
return(f+b+h);
}
float add(float a,float b,int c,int d)
{
return(a+b+c+d);
}
b) Write a JAVA program to illustrate multiple inheritances for
student details.
import java.io.*;
interface a1
{
int s1=50,s2=60,s3=80,s4=70,s5=90;
public void total();
}
class b1 implements a1
{
int tot;
public void total()
{
tot=s1+s2+s3+s4+s5;
System.out.println("The total marks is"+tot);
}
}
class main
{
public static void main(String ar[])
{
b1 b=new b1();
b.total();
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
}
}
3. a) Write a C++ program to find the sum of 5 numbers using Default
arguments.
#include<iostream.h>
int sum(int a=14,int b=28,int c=30,int d=5, int e=2);
void main()
{
cout<<" sum ()="<<sum ()<<endl;
cout<<" sum (18)="<< sum (18)<<endl;
cout<<" sum (18,25)="<< sum (18,25)<<endl;
}
int sum (int a, int b, int c, int d, int e)
{
return(a+b+c+d+e);
}
b) Write a JAVA program with three threads named as “A”,” B”, “C” and
initiate these threads from main thread and show the result.
class a extends Thread
{
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.println("\t From Thread a: i= "+i);
}
System.out.println("Exit from a");
}
}
class b extends Thread
{
public void run()
{
for(int j=1;j<=10;j++)
{
System.out.println("\t From Thread b: j= "+j);
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
}
System.out.println("Exit from b");
}
}
class c extends Thread
{
public void run()
{
for(int k=1;k<=10;k++)
{
System.out.println("\t From Thread c: k= "+k);
}
System.out.println("Exit from c");
}
}
class ThreadTest
{
public static void main(String args[])
{
a a1=new a();
b b1=new b();
c c1= new c();
a1.start();
b1.start();
c1.start();
}
}
4. a) Write a C++ program to get and display the details of a student name and
register number in a class and age, branch and hobby in another class using
single inheritance.
#include<iostream.h>
#include<conio.h>
class stu1
{
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
protected:
int regno;
char name[20];
public:
void getdata()
{
cout<<"Enter the name and reg no:";
cin>>name;
cin>>regno;
}
};
class stu:public stu1
{
private:
char h1[30],h2[40],dept[5];
int age;
public:
void get()
{
cout<<"Enter the hobies : ";
cin>>h1>>h2;
cout<<"Enter the age : ";
cin>>age;
cout<<"Enter the dept : ";
cin>>dept;
}
void display()
{
cout<<"Name of student : "<<name<<"\n";
cout<<"RegNo of student : "<<regno<<"\n";
cout<<"Age of student : "<<age<<"\n";
cout<<"Dept of student : "<<dept<<"\n";
cout<<"Hobbies of student : "<<h1<<"\t
,"<<h2<<"\n";
}
};
void main()
{
clrscr();
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
stu s;
s.getdata();
s.get();
s.display();
getch();
}
b)
Write a JAVA program to get and display the details of staff name
and designation in a class, department, salary in another class and awards in the
third class using interfaces and Inheritance.
import java.util.*;
import java.util.*;
interface e
{
public void disp();
}
class emp implements e
{
String desig,name;
Scanner get = new Scanner(System.in);
emp()
{
System.out.println("Enter Name of the staff:");
name = get.nextLine();
System.out.println("Enter staff designation:");
desig = get.nextLine();
}
public void disp()
{
System.out.println("staff Name: "+name);
System.out.println("Designation: "+desig);
}
}
class emp2
{
String dept;
double sal;
Scanner get1 = new Scanner(System.in);
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
emp2()
{
System.out.println("Enter Dept of the staff:");
dept = get1.nextLine();
System.out.println("Enter salary of staff:");
sal = get1.nextInt();
}
public void disp1()
{
System.out.println("staff Dept Name: "+dept);
System.out.println("Staff salary :Rs "+sal);
}
}
class emp3 extends emp2
{
String awards;
Scanner get2 = new Scanner(System.in);
emp3()
{
System.out.println("Enter awards of the staff:");
awards = get2.nextLine();
}
void disp2()
{
System.out.println("staff Award: "+awards);
}
}
class main2
{
public static void main(String args[])
{
emp em=new emp();
emp3 em3=new emp3();
em.disp();
em3.disp1();
em3.disp2();
}
}
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
5. a) Write a C++ program to generate Fibonacci series using constructor and
destructor.
#include<iostream.h>
#include<conio.h>
class fib
{
int n,c,i,a,b;
public:
fib();
~fib()
{
cout<<"destructor is called";
}
};
fib::fib()
{
a=0;b=1;
cout<<"enter any no";
cin>>n;
cout<<"fibnocci series:\n";
for(i=0;i<n;i++)
{
if(i<=1)
{
c=i;
}
else
{
c=a+b;
a=b;
b=c;
}
cout<<c<<"\n";
}
}
void main()
{
clrscr();
fib ff;
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
getch();
}
b)
Write a JAVA program with two packages namely decrement and
arithmetic operators and import these packages in a main class and show the
result.
Package arithmetic;
public class addsub
{
int a=15,b=10,c,d;
public void print1()
{
c=a+b;
d=a-b;
System.out.println("sum="+c);
System.out.println("sum="+d);
}
}
package decrement;
public class dec
{
int a=15,c;
public void print2()
{
c=--a;
System.out.println("decrement="+c);
}
}
main program:
import arithmetic.addsub;
import decrement.dec;
class sample1
{
public static void main (String args[])
{
addsub x=new addsub();
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
dec y=new dec();
x.print1();
y.print2();
}
}
6. a) Write a C++ program to perform complex number subtraction by
overloading an operator using friend function.
#include<iostream.h>
#include<conio.h>
class over
{
float x,y;
public:
void setdata( float a, float b)
{
x= a;
y= b;
}
friend over operator -(over o1, over o2);
void display( )
{
cout<<x<<y<<"i";
}
};
over operator -(over o1,over o2)
{
over temp;
temp.x= o1.x - o2.x;
temp.y= o1.y - o2.y;
return(temp);
}
void main( )
{
over o1,o2 , o3;
o1. setdata( 11.5, 2.5);
o2. setdata( 6.5, 4.5);
o3= o1-o2;
cout<<"\nthe result is:";
o3. display ( );
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
}
b) Write a JAVA program with three threads named as “P”,” Q”, “R” and
initiate these threads from main thread and show the result.
class p extends Thread
{
public void run()
{
for(int i=1;i<=10;i++)
{
System.out.println("\t From Thread p: i= "+i);
}
System.out.println("Exit from p");
}
}
class q extends Thread
{
public void run()
{
for(int j=1;j<=10;j++)
{
System.out.println("\t From Thread q j= "+j);
}
System.out.println("Exit from q");
}
}
class r extends Thread
{
public void run()
{
for(int k=1;k<=10;k++)
{
System.out.println("\t From Thread r: k= "+k);
}
System.out.println("Exit from r");
}
}
class ThreadTest
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
{
public static void main(String args[])
{
p a1=new p();
q b1=new q();
r c1= new r();
a1.start();
b1.start();
c1.start();
}
}
7. a) Write a C++ program to handle division by zero exception.
#include<iostream.h>
void main( )
{
int x, y, z, d, c;
cout<<"enter the values of x, y, z";
cin>>x>>y>>z;
try
{
if(x==y)
throw y;
else
c=x-y;
d=z/c;
cout<<"the result of z/ (x-y )is:"<<d;
}
catch( int y)
{
cout<<"exception caught x-y is "<<(x-y );
}
}
b)Write a JAVA program with a class named as “circle” that implements an
interface named as “circleinterface” and define the methods named as “area” and
“circum” in the class to find the area and circumference of the circle.
import java.io.*;
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
interface circleinterface
{
int r=5;
double pi=3.14;
public void area();
public void circum();
}
class circle implements circleinterface
{
double ar,circum;
public void area()
{
ar=pi*r*r;
System.out.println("The area marks is = "+ar);
}
public void circum()
{
circum=2*pi*r;
System.out.println("The area marks is = "+circum);
}
}
class main1
{
public static void main(String ar[])
{
circle b=new circle();
b.area();
b.circum();
}
}
8. a)Write a C++ program to calculate student percentage by inheriting the
personal details from the base class to the derived class
#include<iostream.h>
#include<conio.h>
class stu1
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
{
protected:
int regno,m1,m2,m3;
char name[20];
public:
void getdata()
{
cout<<"Enter the name and reg no:";
cin>>name;
cin>>regno;
cout<<"Enter the three sub marks : ";
cin>>m1>>m2>>m3;
}
};
class stu:public stu1
{
private:
int avg, total;
public:
void get()
{
total=m1+m2+m3;
avg=total/3;
}
void display()
{
cout<<"Name of student : "<<name<<"\n";
cout<<"RegNo of student : "<<regno<<"\n";
cout<<"Total of student : "<<total<<"\n";
cout<<"Avg of student : "<<avg<<"\n";
}
};
void main()
{
clrscr();
stu s;
s.getdata();
s.get();
s.display();
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
getch();
}
b) Write a JAVA program for handling Arithmetic exception.
class ex
{
public static void main(String ar[])
{
int a = 10;
int b = 0;
int c;
try
{
c=a/b;
}
catch(ArithmeticException e)
{
System.out.println("\n"+"----ARITHMETIC EXCEPTION------ ");
System.out.println("\n"+"dividend is :: "+a+"\t"+"divisor is :: "+b);
System.out.println("Dvide by zero Exception");
}
}
}
9. a)Write a C++ program with two classes named as “add”, “sub” to perform
addition and subtraction of two given numbers and inherit these values to
another class named as “remainder” to find the remainder of these values.
#include<iostream.h>
#include<conio.h>
class add
{
protected:
int a,b,c;
public:
void get()
{
cout<<"Enter the values of a and b:";
cin>>a;
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
cin>>b;
c=a+b;
}
};
class sub
{
protected:
int d,e,f;
public:
void get1()
{
cout<<"Enter the values of d and e:";
cin>>d;
cin>>e;
f=d-e;
}
};
class reminder:public add, public sub
{
int r;
public:
void get2()
{
r=f%c;
}
void display()
{
cout<<"The sum is : "<<c;
cout<<"\n The sub value is : "<<f;
cout<<"\n The rem value is : "<<r;
}
};
void main()
{
clrscr();
reminder s;
s.get();
s.get1();
s.get2();
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
s.display();
getch();
}
b) Write a JAVA program to find the square and cube of a number using class
and object.
class sqr1
{
public void square()
{
for(int i=1;i<=10;i++)
System.out.println("\n"+i+" square is:: "+i*i);
}
}
class cub1
{
public void cube()
{
for(int i=1;i<=10;i++)
System.out.println("\n"+i+" cube is:: "+i*i*i);
}
}
class ex
{
public static void main(String ar[])
{
sqr1 s=new sqr1();
cub1 c= new cub1();
s.square();
c.cube();
}
}
10.a) Write a C++ program to perform subtraction of two numbers using
function overloading.
#include<iostream.h>
#include<conio.h>
int sub(int);
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
float sub(float,int);
void main()
{
clrscr();
cout<<"\n Subtraction 1:"<<sub(10);
cout<<"\n Subtraction 2:"<<sub(15.2,8);
}
int sub (int s)
{
return(s-5);
}
float sub (float r, int h)
{
return(r-h);
}
b)Write a JAVA program with a class named as “rectangle” to find the area
of rectangle and inherit this area into another class named as “cuboid” to
find the area of the cuboid.
import java .io.*;
class rectangle
{
int l=5, b=10;
int arearect=l*b;
void printrect()
{
System.out.println("The area of Rectangle ="+arearect);
}
}
class cuboid extends rectangle
{
int h=7;
int areacube=arearect*h;
void printcube()
{
System.out.println("The area of Cuboid ="+areacube);
}
}
class main2
{
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
public static void main(String args[])
{
cuboid c=new cuboid();
c.printcube();
c.printrect();
}
}
11.a) Write a C++ program to perform complex number addition by
overloading an operator using friend function.
#include<iostream.h>
#include<conio.h>
class over
{
float x,y;
public:
void setdata( float a, float b)
{
x= a;
y= b;
}
friend over operator +(over o1, over o2);
void display( )
{
cout<<x<<"+"<<y<<"i";
}
};
over operator +(over o1,over o2)
{
over temp;
temp.x= o1.x + o2.x;
temp.y= o1.y + o2.y;
return(temp);
}
void main( )
{
over o1,o2 , o3;
o1. setdata( 11.5, 2.5);
o2. setdata( 6.5, 4.5);
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
o3= o1+o2;
cout<<"\nthe result is:";
o3. display ( );
}
b)
Write a JAVA program to find the area of rectangle and circle using
class and object.
import java .io.*;
class Rectangle
{
void arearect()
{
double l=5.5,b=6.0,area;
area=l*b;
System.out.println("Area of Rectangle ="+area);
}
}
class Circle
{
void areacir()
{
double r=6.5,pi=3.14,ar;
ar=pi*r*r;
System.out.println("Area of circle ="+ar);
}
}
class main2
{
public static void main(String args[])
{
Rectangle rect=new Rectangle();
Circle cir=new Circle();
rect.arearect();
cir.areacir();
}
}
12. a) Write a C++ program with the class name “one” and “two” to get the
personal and mark details of a student respectively and create a class “three” that
inherits the properties of “one” and “two” to calculate the average mark obtained
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
by that student.
#include<iostream.h>
#include<conio.h>
class one
{
protected:
int rollno;
char name[30];
public:
void getdata()
{
cout<<"Enter name";
cin>>name;
cout<<"Enter Roll No: ";
cin>>rollno;
}
};
class two
{
protected:
int sub1,sub2;
public:
void getmark()
{
cout<<"Enter marks of two subjects: ";
cin>>sub1>>sub2;
}
};
class three:public one,public two
{
int total;
float avg;
public:
void display()
{
total=sub1+sub2;
avg=total/2;
cout<<"Name :"<<name;
cout<<"\n rollno:"<<rollno;
cout<<"\n sub1:"<<sub1;
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
cout<<"\n sub2:"<<sub2;
cout<<"\n total:"<<total;
cout<<"\n Average : "<<avg;
}
};
void main()
{
clrscr();
three s;
s.getdata();
s.getmark();
s.display();
getch();
}
b) Write a JAVA program to find the factorial of a given number using class
and object.
import java .io.*;
class factorial
{
int n=5, i, f= 1;
void fact()
{
for ( i= 1 ; i <= n ; i++ )
f= f*i;
System.out.println("Factorial of "+n+" is = "+f);
}
}
class main2
{
public static void main(String args[])
{
factorial f=new factorial();
f.fact();
}
}
13.a) Write a C++ program with two classes named as “one”, “two” to find the
area of square and rectangle respectively, and inherit these values to another
class named as “cuboid” with its own property “height” to find the area of
the cuboid.
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
#include<iostream.h>
#include<conio.h>
class one
{
protected:
int s,ar;
public:
void area()
{
cout<<"Enter the length and bredth of square";
cin>>s;
ar=s*s;
}
};
class two
{
protected:
int l,b,ar1;
public:
void area1()
{
cout<<"Enter the length and bredth of rectangle";
cin>>l>>b;
ar1=l*b;
}
};
class cuboid:public one, public two
{
private:
int h,ar2;
public:
void area2()
{
cout<<"Enter the height of cuboid";
cin>>h;
ar2=l*b*h;
}
void display()
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
{
cout<<"Area of rectangle:"<<ar1<<"\n";
cout<<"Area of square:"<<ar<<"\n";
cout<<"Area of cuboid:"<<ar2<<"\n";
}
};
void main()
{
clrscr();
cuboid s;
s.area();
s.area1();
s.area2();
s.display();
getch();
}
b) Write a JAVA program to find the prime numbers of a given number
using class and object.
import java .io.*;
class primeno
{
int i,c,j;
void prime()
{
System.out.println(" prime numbers are:");
for(i=2;i<=100;i++)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
{
c++;
}
}
if(c==2)
{
System.out.print(" " + i);
}
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
}
}
}
class main2
{
public static void main(String args[])
{
primeno f=new primeno();
f.prime();
}
}
15. a. Develop C++ program using function template to sort an array of ‘N’
numbers.
#include<iostream.h>
#include<conio.h>
template<class t>
void bubble(t a[], int n)
{
int i, j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
void main()
{
int a[6]={1,3,2,4,8,5};
clrscr();
bubble(a,6);
cout<<"\nSorted Order Integers: ";
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
for(int i=0;i<6;i++)
cout<<a[i]<<"\t";
getch();
}
b. Develop a Java program for generating three threads to perform the following
Operations:
Accept ‘N’ numbers
Print the greatest number
Print the smallest number
16.a. Develop a C++ program using classes and member functions to perform the
following:
• Accept a string.
• Reverse the string.
• Check whether the string is a palindrome or not and print the result
b. Develop a Java application to illustrate how user-defined exceptions are
handled
class ex1 extends Exception
{
public ex1(String problem)
{
super(problem);
}
}
public class main2
{
public static void main( String args[ ] ) throws ex1
{
int balance = 100,
withdraw = 1000;
if (balance < withdraw)
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
{
ex1 e = new ex1("No balance please");
throw e;
}
else
{
System.out.println("Draw the money & enjoy");
}
}
}
17 .a. Write a C++ program to generate Fibonacci series using
constructor and destructor.
#include<iostream.h>
#include<conio.h>
class fib
{
int n,c,i,a,b;
public:
fib();
~fib()
{
cout<<"destructor is called";
}
};
fib::fib()
{
a=0;b=1;
cout<<"enter any no";
cin>>n;
cout<<"fibnocci series:\n";
for(i=0;i<n;i++)
{
if(i<=1)
{
c=i;
}
else
{
c=a+b;
a=b;
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
b=c;
}
cout<<c<<"\n";
}
}
void main()
{
clrscr();
fib ff;
getch();
}
b) Write a JAVA program with two packages namely decrement and
arithmetic operators and import these packages in a main class and show
the result.
NOTE:
1.inside bin create one folder: “arithmetic”
2. save the package arithmetic as “ addsub.java”.
3.similarly inside bin create another one folder: “decrement”
4. save the package decrement as “dec.java”.
5. In bin save the “main program” as sample1.java.
6. compile the program as: javac sample1.java
7. run the program as: java sample1
Package arithmetic;
public class addsub
{
int a=15,b=10,c,d;
public void print1()
{
c=a+b;
d=a-b;
System.out.println("sum="+c);
WWW.VIDYARTHIPLUS.COM
V+ TEAM
WWW.VIDYARTHIPLUS.COM
System.out.println("sum="+d);
}
}
package decrement;
public class dec
{
int a=15,c;
public void print2()
{
c=--a;
System.out.println("decrement="+c);
}
}
main program:
import arithmetic.addsub;
import decrement.dec;
class sample1
{
public static void main (String args[])
{
addsub x=new addsub();
dec y=new dec();
x.print1();
y.print2();
}
}
WWW.VIDYARTHIPLUS.COM
V+ TEAM