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

TNB CPP-JAVA Programs DS

Practical for 2 sem for bca

Uploaded by

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

TNB CPP-JAVA Programs DS

Practical for 2 sem for bca

Uploaded by

rarya2256
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

INDEX

C++ Programs with Output


Sl.
Program Title Date Signature Remarks
No.
Overloading unary Plus (+) operator using
1.
member function
Overloading unary minus (-) operator using
2.
member function
Overloading unary ++(increment prefix)
3.
operator using member function
Overloading unary -- (Decrement Postfix)
4.
operator using member function
Overloading Binary Plus (+) operator using
5.
member function.
Overloading Binary Minus (-) operator using
6.
member function
Overloading Binary plus (+) operator using
7.
Friend function
A C++ Program to Access private data using
8.
non-member function, use friend function
A C++ Program to declare friend class and
9.
access the private data
A C++ Program to Add Two numbers using
10.
Friend Function
A c++ program to demonstrate the virtual
11.
function
A C++ Program Of Complex Number, Using All
12. Type of Constructor
(Default/Parameterized/Copy)

13. Single Inheritance

14. Multilevel Inheritance

A c++ program to demonstrate Exception


15.
handling
A c++ program to demonstrate Exception
16.
Handling multiple catch block

17 Re-throw in Exception handling

18 Exception handling in class

A c++ program to demonstrate the Function


19.
template
A c++ program to demonstrate Overloading a
20.
function template

21. A c++ program to demonstrate class template

A c++ program to demonstrate template outside


22.
of class

1
A c++ program to demonstrate Writing txt into
23.
file using constructor
A c++ program to demonstrate Reading entire
24.
text from file using constructor
A c++ program to demonstrate Write text on
25. the file using open() function for creating or
opening a file.
A c++ program to demonstrate Read the
26.
contents of a entire file.
A c++ program to demonstrate getline()
27.
function

28. A c++ program to demonstrate write() function

A c++ program to Copy the content of one file


29.
to another file

JAVA Programs with output


A java program to add two numbers by taking
1.
input using Console class
A java program to check whether the given
2. number is arm strong number or not thrugh
console class
A java program to perform the multiplication
3. using repetitive addition through command line
argument
A java program to find the largest number in
between Two Numbers
4.
* Input - Using InputStreamReader and
BufferedReader
A java program to find the Compound Interest
5. and Simple Interest* Input - Using
InputStreamReader and BufferedReader
A java program to find the cubes of a given
6.
number.* input - DataInputStream Class
A java program to count the factor of a given
7. number * Input - DataInputStream class

. A java program to count the largest factor of a


8.
given number* Input - DataInputStream class
A java program to find the sum of all odd
9.
numbers (up to n)
A Java Program to Find the sum of n(last digit)
10.
digit
A java program to find the smallest factor of a
11.
number n (except 1)

12. A java program to find the all factor of number

A java program to find the sum of all Even


13.
numbers(up to n)
Java program to calculate x^n using for loop.
14.
(Without using pow() function).

2
15. Print the multiplication table of given number

16. Java program to reverse a given Integer.

Java program to find the factorial of a given


17.
number.

18. A java program to find the length of string

19. A java program to Compare two strings

A java program to remove the duplicate


20.
elements in array
A java program to find the area of a circle,
21. * Using class, object, constructor, destructor
and methods
A java program to find the CI and SI,* using
22.
class, object, constructor and method
A java program to demonstrate the Hybrid
23.
Inheritance
A java program to demonstrate threads in java
24.
using extending thread class
Thread in java by implementing Runnable
25.
Interface
A java Program to demonstrate Exception
26.
Divide by 0.
A java program to demonstrate the nested try
27.
block in exception handling

3
4
1. Overloading unary Plus (+) operator using member function
#include<iostream>
#include<conio.h>
using namespace std;
class UnaryPlus
{
private :
int a,b;
public :
//Inside function declaration
void input();
void output();
UnaryPlus operator+(UnaryPlus &obj2);
/*
//Inside operator function definition
UnaryPlus operator+(UnaryPlus &Obj2)
{
UnaryPlus Sum;
Sum.a = a + Obj2.a;
Sum.b = b + Obj2.b;
return Sum;
}
*/
};
//Outside function definition
void UnaryPlus :: input()
{
cout<<"A = ";
cin>>a;
cout<<"B = ";
cin>>b;
}
void UnaryPlus :: output()
{
cout<<"A = "<<a<<", B = "<<b<<endl;
}
UnaryPlus UnaryPlus :: operator+(UnaryPlus &obj2)
{
UnaryPlus sum;
sum.a = a + obj2.a;
sum.b = b + obj2.b;
return sum;
}
Int main()
{
UnaryPlus Obj1,Obj2,Sum;
//clrscr();
cout<<"Enter values for first object :"<<endl;
Obj1.input();

5
cout<<"Enter values for second object : "<<endl;
Obj2.input();
cout<<"--------------------------------------------"<<endl;
cout<<"Value of first object :"<<endl;
Obj1.output();
cout<<"Value of second object :"<<endl;
Obj2.output();
cout<<"---------------------------------------------"<<endl;
Sum = Obj1 + Obj2;
cout<<"Addition of both object values are : "<<endl;
Sum.output();
getch();
return 0;
}

2. Overloading unary minus (-) operator using member function


#include<iostream>
#include<conio.h>
using namespace std;
class UnaryMinus
{
private :
int a,b;
public :
//Inside function declaration
void input();
void output();
UnaryMinus operator-(UnaryMinus &Obj2);
/*
//Inside operator function definition
UnaryMinus operator-(UnaryMinus &Obj2)
{
UnaryMinus Sub;
Sub.a = a - Obj2.a;
Sub.b = b - Obj2.b;
return Sub;
}
*/

6
};
//Outside function definition
void UnaryMinus :: input()
{
cout<<"A = ";
cin>>a;
cout<<"B = ";
cin>>b;
}
void UnaryMinus :: output()
{
cout<<"A = "<<a<<", B = "<<b<<endl;
}
UnaryMinus UnaryMinus :: operator-(UnaryMinus &Obj2)
{
UnaryMinus Sub;
Sub.a = a - Obj2.a;
Sub.b = b - Obj2.b;
return Sub;
}
//Main function
int main()
{
UnaryMinus Obj1,Obj2,Sub;
//clrscr();
cout<<"Enter values for first object:"<<endl;
Obj1.input();
cout<<"Enter values for secoand object: "<<endl;
Obj2.input();
cout<<"--------------------------------------------"<<endl;
cout<<"Value of first object:"<<endl;
Obj1.output();
cout<<"Value of second object:"<<endl;
Obj2.output();
cout<<"---------------------------------------------"<<endl;
Sub = Obj1 - Obj2;
cout<<"Subtraction of both object values are : "<<endl;
Sub.output();
getch();
return 0;
}

7
3. Overloading unary ++(increment prefix) operator using member function
#include<iostream>
using namespace std;
class IncrementPrefix
{
private :
int a,b;
public :

void input();
void output();
//Operator function definition
void operator ++()
{
a++;
b++;
}
};
/* //Outside function definition
void IncrementPrefix :: operator++()
{
++a;
++b;
}
*/
void IncrementPrefix :: input()
{
cout<<"A = ";
cin>>a;
cout<<"B = ";
cin>>b;
}
void IncrementPrefix :: output()
{
cout<<"A = "<<a<<", B = "<<b<<endl;
}
//Main function
int main()
{
IncrementPrefix obj1;
obj1.input();
obj1.output();
++obj1;
cout<<"After first increment values are : "<<endl;
obj1.output();
++obj1;
cout<<"After secoand increment values are : "<<endl;
obj1.output();
return 0;
}

8
4. Overloading unary -- (Decrement Postfix) operator using member function
#include<iostream>
using namespace std;
class DecrementPosrfix
{
private :
int a,b;
public :
void input();
void output();
void operator --(int);
};
//Outside function definition
void DecrementPosrfix :: input(){
cout<<"A = ";
cin>>a;
cout<<"B = ";
cin>>b;
}
void DecrementPosrfix :: output()
{
cout<<"A = "<<a<<", B = "<<b<<endl;
}
//Decrement operator function defenation
void DecrementPosrfix :: operator --(int)
{
a--;
b--;
}
int main(){
DecrementPosrfix obj;
obj.input();
obj.output();
obj--;
cout<<"After fisrt decrement values are :"<<endl;
obj.output();
obj--;
cout<<"After secoand decrement values are :"<<endl;
obj.output();
return 0;
}

9
5. Overloading Binary Plus (+) operator using member function.
#include<iostream>
using namespace std;
class BinaryPlus
{
private :
int a,b;
public :
BinaryPlus()
{
a = 0;
b = 0;
}
BinaryPlus(int x, int y)
{
a = x;
b = y;
}
void input();
void output();
BinaryPlus operator + (BinaryPlus obj);
};
void BinaryPlus :: input()
{
cout<<"A = ";cin>>a;
cout<<"B = "; cin>>b;
}
void BinaryPlus :: output()
{
cout<<"A = "<<a<<"\nB = "<<b<<endl;
}
BinaryPlus BinaryPlus :: operator + (BinaryPlus obj)
{
BinaryPlus bp;
bp.a = a + obj.a;
bp.b = b + obj.b;
return (bp);
}

10
int main()
{
BinaryPlus obj1;
BinaryPlus obj2(5,7);
BinaryPlus obj3(9,3);
BinaryPlus objplus;
cout<<"The members of obj1 :"<<endl;
obj1.output();
cout<<"The members of obj2 : "<<endl;
obj2.output();
cout<<"The members of obj3 : "<<endl;
obj3.output();
cout<<"the members of objplus :"<<endl;
objplus.output();
cout<<"Enter values for obj1 : "<<endl;
obj1.input();
cout<<"Addition of obj1 + obj2 :"<<endl;
objplus = obj1 + obj2;
cout<<"After additon of obj1 +obj2 , values of objplus : "<<endl;
objplus.output();
cout<<"Addition of obj2 + obj3 : "<<endl;
objplus = obj2 + obj3;
cout<<"After addition of obj2 + obj3 , values of objplus : "<<endl;
objplus.output();
cout<<"Enter values for obj2 :"<<endl;
obj2.input();
cout<<"Addition of obj2 + obj1 : "<<endl;
objplus = obj2 + obj1;
cout<<"After addition of obj2 + obj3 , values of objplus : "<<endl;
objplus.output();
return 0 ;
}

11
6. Overloading Binary Minus (-) operator using member function
#include<iostream>
using namespace std;
class BinaryMinus
{
private :
int a,b;
public:
BinaryMinus()
{
a = 0;
b = 0;
}
BinaryMinus(int x, int y)
{
a=x;
b = y;
}
BinaryMinus(BinaryMinus &val)
{
a = val.a;
b = val.b;
}
void input();
void output();
BinaryMinus operator-(BinaryMinus obj);
~BinaryMinus(){}
};
void BinaryMinus :: input()
{
cout<<"A = ";
cin>>a;
cout<<"B = ";
cin>>b;
}
void BinaryMinus :: output()
{
cout<<"A = "<<a<<"\nB = "<<b<<endl;
}
BinaryMinus BinaryMinus :: operator - (BinaryMinus obj)
{
BinaryMinus bm;
bm.a = a - obj.a;
bm.b = b - obj.b;
return (bm);
}
int main(){
BinaryMinus obj1;
BinaryMinus obj2(10,5);
BinaryMinus obj3(1,2);
BinaryMinus obj4(obj2);
BinaryMinus objminus;

12
cout<<"Valuse of Objects : "<<endl;
cout<<"obj1 ::"<<endl;
obj1.output();
cout<<"obj2 ::"<<endl;
obj2.output();
cout<<"obj3 ::"<<endl;
obj3.output();
cout<<"obj4 ::"<<endl;
obj4.output();
cout<<"------------------------"<<endl<<"Enter values for obj1 : "<<endl;
obj1.input();
cout<<"------------------------"<<endl<<"Subtraction of objects:: “<<endl;
cout<<"obj1 - obj4 ::"<<endl;
objminus = obj1 - obj4;
objminus.output();
cout<<"obj2 - obj3 ::"<<endl;
objminus = obj2 - obj3;
objminus.output();
return 0;
}

7. Overloading Binary plus (+) operator using Friend function


#include<iostream>
using namespace std;
class B;
class A{

int a;
public:
void setA(){
a=20;
}
void disp(){
cout<<"A = "<<a;
}
friend void fun(A ob1,B ob2);
};

13
class B{

int b;
public:
void setB(){
b=30;
}
void disp(){
cout<<"\nB = "<<b;
}
friend void fun(A ob1,B ob2);
};

void fun(A ob1,B ob2)


{

int c =ob1.a+ob2.b;
cout<<"\n Result : "<<c;
}
int main(){
A ob1;
B ob2;
//void fun(A,B);
ob1.setA();
ob2.setB();
fun(ob1,ob2);
return 0;
}

8. A C++ Program to Access private data using non-member function, use


friend function
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class AC{
private:
string name;
int ac_no;
float bal;
public:
void read(){
cout<<"\n Name : ";
getline(cin,name);
cout<<"\nA/C No. : ";
cin>>ac_no;
cout<<"Balance : ";
cin>>bal

14
friend void show_bal(AC);
};
void show_bal(AC a){
cout<<"\n Balance of A/C No. : "<<a.ac_no<<" is Rs.: "<<a.bal<<endl;
}
int main(){
system("cls");
AC a;
a.read();
show_bal(a);
getch();
return 0;
}

9. A C++ Program to declare friend class and access the private data
#include<iostream>
using namespace std;
class B;
class A{
private:
int a;
public:
void a_set(){
a=30;
}
void show(B);
};
class B{
private:
int b;
public:
void b_set(){
b=40;
}
friend void A::show(B b);
};
void A::show(B b){
cout<<"\n A = "<<a;
cout<<"\n B = "<<b.b;
}
int main(){
//clrscr();
A a1;
a1.a_set();
B b1;
b1.b_set();
a1.show(b1);
return 0;
}
15
10. A C++ Program to Add Two numbers using Friend Function
#include<iostream>
#include<conio.h>
using namespace std;
class AB{
int a,b;
public:
void set_values(int x,int y){
a=x;
b=y;
}
void disp(){
cout<<"\n A = "<<a<<"\n B = "<<b<<endl;
}
friend int sum(AB);
};
int sum(AB a){
return(a.a+a.b);
}
int main(){
AB x;
//clrscr();
x.set_values(10,20);
x.disp();
cout<<"Sum = "<<sum(x);
getch();
return 0;
}

16
11. A c++ program to demonstrate the virtual function
#include<iostream>
using namespace std;
class Polygon{
protected:
int width,height;
public:
void set_value(int a,int b){
width = a;height = b;
}
virtual int area()//Virtual function
{
return 0;
}
};
class Rectangle : public Polygon{
public:
int area(){
return(width*height);
}
};
class Tringle : public Polygon{
public:
int area(){
return(width*height);
}
};
int main()
{
Rectangle r;
Tringle t;
Polygon p;
Polygon *p1 = &r;
Polygon *p2 = &t;
Polygon *p3 = &p;
p1->set_value(4,5);
p2->set_value(10,15);
p3->set_value(20,34);
cout<<p1->area()<<endl;
cout<<p2->area()<<endl;
cout<<p3->area()<<endl;
return 0;
}

17
12. A C++ Program Of Complex Number, Using All Type of Constructor
(Default/Parameterized/Copy)
#include<iostream>
using namespace std;

class complex{
private:
float rp;
float img_pt;
public:
//Constructor
//Default Constructor
complex(){
rp = 0;
img_pt = 0;
}
//Parameterized Constructor
complex(float rn,float imn){
rp = rn;
img_pt = imn;
}
//Copy Constructor
complex(const complex &ov){
rp = ov.rp;
img_pt = ov.img_pt;
}
//Function Deceleration
void def_cons_arg();
void C_N_Input();
void C_N_Output();
//Deconstructions
~complex(){cout<<"DE Constructor Called ."<<endl;}
};
//Function Definition
void complex::def_cons_arg(){
cout<<"\nEnter Real Part : ";cin>>rp;
cout<<"Enter Imaginary Part : ";cin>>img_pt;
}
void complex::C_N_Input(){
cout<<"Enter The Number in This Form: "<<rp<<"+"<<img_pt<<"i"<<endl;
cout<<"\nEnter Real Part : ";cin>>rp;
cout<<"\nEnter Imaginary Part : ";cin>>img_pt;
}
void complex::C_N_Output(){
cout<<"\n\n The Given Complex Number Is :"<<endl;
cout<<"\n\n\t\t"<<rp<<showpos<<img_pt<<"i"<<noshowpos<<endl;
}

18
int main(){
complex clx,clx1(clx);
cout<<"\n\tThe Default Constructor "<<endl;
clx.C_N_Input();
clx.C_N_Output();
cout<<" \n\n\t The Parameterized Constructor "<<endl;
cout<<"Enter The Default Value For Complex Number Parameter. "<<endl;
clx.def_cons_arg();
clx.C_N_Input();
clx.C_N_Output();
cout<<"\n\n\tThe Copy Constructor."<<endl;
clx1.C_N_Output();
return 0;
}

13. Single Inheritance


#include<iostream>
using namespace std;
class DCA{
public:void display1(){
cout<<"1. M.S office"<<endl;
cout<<"2. M.S windows"<<endl;
cout<<"3. Linux"<<endl;
cout<<"4. M.S. Dos"<<endl;
}
};

19
class ADCA:public DCA{
public:
void display(){
display1();
cout<<"5. D T P"<<endl;
cout<<"6. C F A"<<endl;
}
};
int main(){
DCA D;
ADCA AD;
int ch;
cout<<"\n 1. DCA"<<endl;
cout<<" 2. ADCA"<<endl;
cout<<" 3. Exit"<<endl;
cout<<"Enter yuour choice:"<<endl;
cin>>ch;
switch(ch){
case 1:
D.display1();
break;
case 2:
AD.display();
case 3:
exit(0);
break;
default:
cout<<"Invalid choice"<<endl;
}
return 0;
}

20
14. Multilevel Inheritance
#include<iostream>
using namespace std;
class publisher{
protected:
string pname;
string paddress;
long regno;
};
class book : public publisher {
protected:
long bno;
string bname;
float price;
};
class bookstore : public book{
protected:
long bno;
string sname;
string sadd;
public:
void get_data(){
cout<<"Enter Publisher Name: ";getline(cin,pname);
cout<<"\n Ente Publisher Address";getline(cin,paddress);
fflush(stdin);
cout<<"\n Enter Reg No.:";cin>>regno;
fflush(stdin);
cout<<"\n Enter Book No.:";cin>>bno;
fflush(stdin);
cout<<"\n Enter subject name";getline(cin,sname);
fflush(stdin);
cout<<"\n Enter Price :";cin>>price;
fflush(stdin);
cout<<"\n Enter Book No: ";cin>>bno;
fflush(stdin);
cout<<"\n Enter Stor Name:";getline(cin,sname);
cout<<"\n Enter Store Address:";getline(cin,sadd);
}
void put_data(){
cout<<"\n Publisher name: "<<pname;
cout<<"\n Publisher Address"<<paddress;
cout<<"\nReg No.:"<<regno;
cout<<"\nBook No.:"<<bno;
cout<<"\n subject name"<<sname;
cout<<"\nPrice :"<<price;
cout<<"\nBook No: "<<bno;
cout<<"\nStor Name:"<<sname;
cout<<"\nStore Address:"<<sadd;
}
};

21
int main(){
bookstore b;
b.get_data();
b.put_data();
return 0;
}

15. A c++ program to demonstrate Exception handling


#include<iostream>
using namespace std;
int main()
{
int x,y;
cout<<"X = ";
cin>>x;
cout<<"Y = ";
cin>>y;

try
{
if(y == 0)
throw 1;
cout<<"Division = "<<(x/y)<<endl;
}
catch(int c)
{
cout<<"Divided by zero(0) not possible..."<<endl;
}

cout<<"Addition = "<<(x+y)<<endl;
cout<<"Subtraction = "<<(x-y)<<endl;
cout<<"Multiplication = "<<(x*y)<<endl;
}

22
16. A c++ program to demonstrate Exception Handling multiple catch block
#include<iostream>
using namespace std;
int main()
{
int x,y,z;
cout<<"X = ";
cin>>x;
cout<<"Y = ";
cin>>y;

try
{
if(y == 0)
throw 1;
if( y < 0)
throw 'a';
if(x < y)
throw 1.1;

cout<<"Division = "<<(x/y)<<endl;
}
catch(int c)
{
cout<<"Divided by zero(0) not possible !..."<<endl;
}
catch(char ch)
{
cout<<" Y should must be positive value!..."<<endl;
}
catch(double fc)
{
cout<<"Y should be less then X !..."<<endl;
}

cout<<"Multiplication = "<<(x*y)<<endl;
cout<<"Addition = "<<(x+y)<<endl;
cout<<"Subtraction = "<<(x-y)<<endl;
}

23
17. Re-throw in Exception handling
#include<iostream>
using namespace std;
void sub(int j,int k){
cout<<"In Sub() Function"<<endl;
try{

if(j == 0)
throw j;
else
cout<<"Subtraction = "<<(j-k)<<endl;
}
catch (int j){
cout<<"Catch NULL value"<<endl;
throw;
}
cout<<"End Sub() Function"<<endl;
}
int main(){
cout<<"In main() Function"<<endl;
try{
sub(8,5);
sub(0,8);
}
catch(int){
cout<<"Catch NULL inside main()"<<endl;
}
cout<<"End of main() function"<<endl;
}

24
18. Exception handling in class
#include<iostream>
using namespace std;
class Exception{
public :
void msg(){
cout<<"Exception caught by Exception class."<<endl;
}
};
int main(){
int x,y;
cout<<"X = ";
cin>>x;
cout<<"Y = ";
cin>>y;
try{
if(y == 0){
Exception ob;
throw(ob);
}
else
cout<<"Division = "<<(x/y)<<endl;
}
catch(Exception ob){
ob.msg();
}
return 0;
}

19. A c++ program to demonstrate the Function template


#include<iostream>
using namespace std;
template <class T>
void show(T a)
{
cout<<"A = "<<a<<endl;
cout<<"Size of (A) : "<<sizeof(a)<<endl;
}
int main()
{
show(10);
show('D');
show("Deepak");
show(2.92);
}
25
20. A c++ program to demonstrate Overloading a function template
#include<iostream>
using namespace std;
template <class T1,class T2>
void show(T1 a, T2 b)
{
cout<<"A = "<<a<<",B = "<<b<<endl;
}
template <class T1,class T2,class T3>
void show(T1 a,T2 b,T3 c)
{
cout<<"A = "<<a<<",B = "<<b<<",C = "<<c<<endl;
}
int main()
{
show(20,30);
show(20.5,60.9);
show(2.92,10);
show(11,22,66);
return 0;
}

21. A c++ program to demonstrate class template


#include<iostream>
using namespace std;
template <class T>
class A
{
public:
T num1 = 2;
T num2 = 92;
void add(){
cout<<"Sum = "<<(num1+num2)<<endl;
}
};
int main(){
A<int> obj;
obj.add();
return 0;
} 26
22. A c++ program to demonstrate template outside of class
#include<iostream>
using namespace std;
template <class T1,class T2>
class Abc
{
T1 a;
T2 b;
public :
void input();
void output();
};
template <class T1,class T2>
void Abc<T1,T2>::input()
{
cout<<"A = ";
cin>>a;
cout<<"B = ";
cin>>b;
}
template <class T1,class T2>
void Abc<T1,T2>::output()
{
cout<<"A = "<<a<<",B = "<<b<<endl;
}
int main()
{
Abc <int ,int> obj;
obj.input();
obj.output();
return 0;
}

27
23. A c++ program to demonstrate Writing txt into file using constructor
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char str[100];
ofstream out("Abc.txt");
cout<<"Enter a string : ";
cin.getline(str,100);
out<<str;
cout<<"Successfully written in file"<<endl;
out.close();
return 0;
}

24. A c++ program to demonstrate Reading entire text from file using constructor
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char str[100];
ifstream in("Abc.txt");
cout<<"Content of file : ";
in.getline(str,100);
cout<<str;
in.close();
return 0;
}

28
25. A c++ program to demonstrate Write text on the file using open() function for
creating or opening a file.
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char str[100];
fstream fs;
fs.open("DeepakDetails.txt",ios::out);
if(!fs)
{
cout<<"File creation failed."<<endl;
return 0;
}
else
{
cout<<"Enter a string : ";
cin.getline(str,100);
fs<<str;
cout<<"Contents are written sucessfully."<<endl;
fs.close();
}
return 0;
}

26. A c++ program to demonstrate Read the contents of a entire file.


#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char str[100];
fstream fs;
fs.open("DeepakDetails.txt",ios::in);
if(!fs)
{
cout<<"File openong failed."<<endl;
return 0;
}

29
else
{
cout<<"Contents of file : ";
while(fs)
{
fs.getline(str,100);
cout<<str;
}
}
fs.close();
return 0;
}

27. A c++ program to demonstrate getline() function


#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char str[100];
fstream fs;
fs.open("DeepakDetails.txt",ios::in);
if(!fs)
{
cout<<"File opening error.";
return 0;
}
cout<<"Contents of file : ";
while(fs)
{
fs.getline(str,100);
cout<<str;
}
fs.close();
return 0;
}

30
28. A c++ program to demonstrate write() function
#include<iostream>
#include<fstream>
using namespace std;
class Student
{
private :
int roll;
char name[25];
public :
void input()
{
cout<<"Enter name : ";
cin.getline(name,25);
cout<<"Roll no : ";
cin>>roll;
}
};
int main()
{
Student st;
fstream fs;
fs.open("Student.dat",ios::out|ios::binary);
if(!fs)
{
cout<<"File creation error.";
return 0;
}
char ch;
do
{
st.input();
fs.write((char*)&st,sizeof(st));
cout<<"Press Y for new ";
cin>>ch;
}
while(ch == 'y'||ch == 'Y');
return 0;
}

31
29. A c++ program to Copy the content of one file to another file
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
string line;
ifstream in{"DeepakDetails.txt"};
ofstream out{"NewDeepakDetails.txt"};
if(in && out)
{
while(getline(in,line))
{
out<<line<<endl;
}
cout<<"copy finished."<<endl;
}
else
{
cout<<"can not read file."<<endl;
}
in.close();
out.close();
return 0;
}

32
33
1. A java program to add two numbers by taking input using Console
class
//import java.io.Console;
class AddCon
{
public static void main(String[] args)
{
//Console cin = System.console();
int a = Integer.parseInt(System.console().readLine("A = "));
int b = Integer.parseInt(System.console().readLine("B = "));
System.out.print("Sum = "+(a+b));
}
}

2. A java program to check whether the given number is arm strong number
or not thrugh console class
import java.io.*;
public class ArmstrongNo{
public static void main(String[] args) {
Console cin = System.console();
int n = Integer.parseInt(cin.readLine("Enter number : "));
int temp = n,sum = 0;
while(temp > 0){
int rem = n%10;
sum += Math.pow(rem,3);
temp = temp/10;
}
if(sum == n)
System.out.print(n+" is an armstrong number");
else
System.out.print(n+" is not an armstrong number");
}
}

34
3. A java program to perform the multiplication using repetitive addition
through command line argument
import java.io.*;
public class MulRepAdd{
public static void main(String[] args) {
Console cin = System.console();
int n1 = Integer.parseInt(cin.readLine("Enter First Number : "));
int n2 = Integer.parseInt(cin.readLine("Enter Secoand Number : "));
int c= 1,d = 0;
for(;;){
d = d+n1;
if(c == n2)
break;
c++;
}
System.out.print("The multiplication of "+n1+" and "+n2+" is : "+d);
}
}

4. A java program to find the largest number in between Two Numbers


* Input - Using InputStreamReader and BufferedReader
import java.io.*;
class LargeTwo{
public static void main(String[] args) throws IOException {
int a,b;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("A = ");
a = Integer.parseInt(br.readLine());
System.out.print("B = ");
b = Integer.parseInt(br.readLine());
System.out.print("Largest = "+((a>b)?a:b));

}
}

35
5. A java program to find the Compound Interest and Simple Interest
* Input - Using InputStreamReader and BufferedReader
import java.io.*;
class CISI{
public static void main(String[] args) throws IOException {
double pr,rate,t,sim,com;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter the amount : ");
pr = Integer.parseInt(br.readLine());
System.out.print("Entet the no. of years : ");
t = Integer.parseInt(br.readLine());
System.out.print("Enter the rate of interest : ");
rate = Integer.parseInt(br.readLine());
sim = (pr*t*rate)/100;
com = pr*Math.pow((1.0+rate/100.0),t)-pr;
System.out.println("Simple Interest : "+sim);
System.out.println("Compound Interest : "+com);

}
}

6. A java program to find the cubes of a given number.


* input - DataInputStream Class
import java.io.DataInputStream;
import java.io.IOException;
class CubeOfNum
{
public static void main(String[] args) throws IOException
{

DataInputStream din = new DataInputStream(System.in);


System.out.print("Enter a number : ");
double n = Double.parseDouble(din.readLine());
double c = Math.pow(n,3);
System.out.print("The cube of "+(int)n+" is :"+(int)c);

}
}

36
7. A java program to count the factor of a given number
* Input - DataInputStream class
import java.io.*;
class FactorNum{
public static void main(String[] args) throws IOException {
int n;
DataInputStream din = new DataInputStream(System.in);
System.out.print("Enter a number : ");
n = Integer.parseInt(din.readLine());
System.out.print("The Factors are : \n");
for(int i = 1;i <= n;i++){
if(n%i == 0)
System.out.print("\n"+i);
}
}
}

8. A java program to count the largest factor of a given number* Input -


DataInputStream class
import java.io.*;
class LargestFactor{
public static void main(String[] args) throws IOException {
int n;
DataInputStream din = new DataInputStream(System.in);
System.out.print("Enter a number : ");
n = Integer.parseInt(din.readLine());
System.out.print("The Factors are : \n");
for(int i = (n-1);i >= 0;i--){
if(i == 0){
System.out.print("1");
break;
}if(n%i == 0){
System.out.print(i);
break;}
}
}
} 37
9. A java program to find the sum of all odd numbers (up to n)
import java.util.Scanner;
public class SumOddNum{
public static void main(String[] args) {
int n,sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Please enter a number : ");
n = s.nextInt();
for(int i = 1;i <= n;i++){
if(i%2 == 1){
sum += i;
}
}
System.out.print("The sum is : "+sum);
}
}

10. A Java Program to Find the sum of n(last digit) digit


import java.util.Scanner;
public class SumNnum{
public static void main(String[] args) {
float n,sum = 0;
Scanner s =new Scanner(System.in);
System.out.print("Enter last digit : ");
n =s.nextFloat();
for(int i = 1;i <= n;i++){
sum += i;
}
System.out.print("Sum = "+sum);
}
}

38
11. A java program to find the smallest factor of a number n (except 1)
import java.util.Scanner;
public class SmallestFactorNum{
public static void main(String[] args) {
int num;
Scanner s = new Scanner(System.in);
System.out.print("Please Enter A Number : ");
num = s.nextInt();
System.out.println("The Factors aer : ");
for(int i = 2;i <= num;i++){
if(num%i == 0){
System.out.println(i);
}
}
}
}

12. A java program to find the all factor of number


import java.util.Scanner;
public class factorOfNum{
public static void main(String[] args) {
int num;
Scanner s = new Scanner(System.in);
System.out.print("Please Enter a Number : ");
num = s.nextInt();
System.out.print("The factors are : \n");
for(int i = 1;i <= num;i++){
if(num%i == 0){
System.out.print("\n"+i);
}
}
}
}

39
13. A java program to find the sum of all Even numbers(up to n)
import java.util.Scanner;
public class SumEvenNum{
public static void main(String[] args) {
int n,sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Please enter the number : ");
n = s.nextInt();
for(int i = 1;i <= n;i++){
if(i % 2 == 0){
sum += i;
}
}
System.out.print("The sum is : "+sum);
}
}

14. Java program to calculate x^n using for loop. (Without using pow()
function).
import java.util.Scanner;
class XN
{
public static void main(String[] args)
{
int x, n,res = 1;
Scanner sc = new Scanner(System.in);
System.out.print("Enter base : ");
x = sc.nextInt();
System.out.print("Enter power : ");
n = sc.nextInt();
for(int i= 1;i<=n;i++)
res*=x;
System.out.println("Result = "+res);
}
}
40
15. Print the multiplication table of given number
import java.util.Scanner;
class MultiplicationTable
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("N = ");
int n = sc.nextInt();
for(int i= 1;i<=10;i++)
System.out.println("\t"+i+" x "+n+" = "+(i*n));
}
}

16. Java program to reverse a given Integer.


import java.util.Scanner;
class ReverseInteger
{
public static void main(String[] args)
{
int n,ln,rev=0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
n = sc.nextInt();
int tn = n;
for(int i= 1;i<=n;i++)
{
ln = n%10;
rev = (rev*10)+ln;
n/=10;
}
System.out.println("Reverse of "+tn+" is : "+rev);
} 41
}
17. Java program to find the factorial of a given number.
import java.util.Scanner;
class Factorial
{
public static void main(String[] args)
{
int fact = 1,n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
n = sc.nextInt();
int i= 1;
while(i<=n)
{
fact*= i;
i++;
}
System.out.println("Factorial = "+fact);
}
}

18. A java program to find the length of string


class LengthOfString
{
public static void main(String[] args) {
// create a string
String st = "Hello! World";
System.out.println("String: " + st);
// get the length of st
int length = st.length();
System.out.println("Length: " + length);
}
}

42
19. A java program to Compare two strings
class CompareTwoString
{
public static void main(String[] args)
{
// create 3 strings
String first = "java programming";
String second = "java programming";
String third = "c programming";
// compare first and second strings
boolean result1 = first.equals(second);
System.out.println("Strings first and second are equal: " + result1);
// compare first and third strings
boolean result2 = first.equals(third);
System.out.println("Strings first and third are equal: " + result2);
}
}

20. A java program to remove the duplicate elements in array


import java.util.Scanner;
public class RemoveDuplicateEle{
public static void main(String args[]){

Scanner s = new Scanner(System.in);


int i,j=0,k=0;
//Input
System.out.print("Enter the total number of elements : ");
int n = s.nextInt();
int arr[] = new int[n];
System.out.println("Now,\nEnter the "+n+" elements one by one : ");
for(i=0;i<n;i++){
System.out.print("N"+(i+1)+" : ");
arr[i] = s.nextInt();
}
//Output 1
System.out.println("The Array elements are : ");
for(i=0;i<n;i++){
System.out.print(arr[i]+" ");
} 43
//Process
for(i=0;i<n;++i){
for(j=i+1;j<n;){
if(arr[i] == arr[j]){
for(int temp = j; temp<n; ++temp){
arr[temp] = arr[temp+1];
}
n = n-1;
}
else
j++;
}
}
//Output 2
System.out.println("The Array elements after removing duplicate
elements :");
for(i=0;i<n;i++){
System.out.print(arr[i]+" ");
}

}
}

21. A java program to find the area of a circle,


* Using class, object, constructor, destructor and methods
import java.util.Scanner;
class Area
{
double area,radious;
Area()
{
area = 0.0;
radious = 0.0;
}
Area(double radious)

44
{
radious = radious;
}
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the rdious of circle : ");
radious = sc.nextDouble();
}
void output()
{
System.out.println("Area of circle : "+(3.141*radious*radious));
}
}
public class AreaCircle{
public static void main(String[] args) {
Area a = new Area(10);
a.output();
a.input();
a.output();

}
}

22. /** A java program to find the CI and SI,


* using class, object, constructor and method
* @param p Prise for argumented constructor
* @param r Rate for argmented constructor
* @param t Time for argumented constructor
* @return not returns any value
* @author DEEPAK SINGH
*/
import java.util.Scanner;
class Calculate{
double price, rate, time;
double ci,si;
Calculate(){
ci = 0.0;
si = 0.0;
}
Calculate(double p, double r, double t){
price = p;
rate = r;
time = t;
}

45
//Input and Output function defentaion
public void input(){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the principal amount : ");
price = sc.nextDouble();
System.out.print("Enter interest rate : ");
rate = sc.nextDouble();
System.out.print("Enter time(years) : ");
time = sc.nextDouble();
}
//Clalculate function
void calc(){
si =(price * time * rate)/100;
ci = price * Math.pow(1.0+rate/100.0,time) - price;
}
public void output(){
calc();
System.out.println("Simple Interest : "+si);
System.out.println("Compound Interest : "+ci);
}
}
class CISI{
public static void main(String[] args) {
Calculate c = new Calculate(1000,1,1);
c.output();
c.input();
c.output();
}
}

23. A java program to demonstrate the Hybrid Inheritance


class A
{
int a;
A(){ a= 5; }
void disp(){
System.out.print("A = "+a);
}
}

46
interface B
{
int b = 10;
void disp();
}
class C extends A implements B
{
int c;
C(){
super();
c = 15;
}
public void disp()
{
super.disp();
System.out.println("B = "+b);
System.out.println("C = "+c);
}
}
class D extends C
{
int d;
D(){
super();
d = 20;
}
public void disp(){
super.disp();
System.out.println("D = "+d);
}
}
class E extends C{
int e;
E(){
super();
e = 25;
}
public void disp(){
super.disp();
System.out.println("E = "+e);
}

}
class HybridInhEg
{
public static void main(String[] args)
{
E p = new E();
p.disp();
D q = new D();
q.disp();

}
} 47
24. A java program to demonstrate threads in java using extending thread
class
import java.lang.Thread;
class A extends Thread
{
public void run()
{
for(int i = 0;i<=10;i++)
System.out.println("i = "+i);
}
}
class B extends Thread
{
public void run()
{
for(int j = 0 ;j<=20;j++)
System.out.println("j = "+j);
}
}
class C extends Thread
{
public void run(){
for (int k =0 ; k<=30;k++)
System.out.println("k = "+k);
}
}
class ThreadEg
{
public static void main(String[] args) {
A a = new A();
B b = new B();
C c = new C();
a.start();
b.start();
c.start();
}
}

48
25 . Thread in java by implementing Runnable Interface
import java.lang.Thread;
public class ThreadEg2 implements Runnable
{
public static void main(String[] args)
{
ThreadEg2 obj = new ThreadEg2();
Thread thread = new Thread(obj);
thread.start();
System.out.println("This code is outside of the thread");
}
public void run()
{
System.out.println("This code is running in a thread");
}
}

26. A java Program to demonstrate Exception Divide by 0.


public class ExceptionEg3
{
public static void main(String args[])
{

int n1, n2;

49
try
{

n1 = 0;
n2 = 100 / n1;
System.out.println(n2);
}
catch (ArithmeticException e)
{
System.out.println("The divider cannot be zero, try a
different number.");
}
catch (Exception e)
{
System.out.println("You cannot execute this program:
DivideByZeroException");

}
}
}

27. A java program to demonstrate the nested try block in exception handling
class ExceptionEg5
{
public static void main(String args[])
{
try
{
try
{
System.out.println("going to divide");
int b=59/0;
}
catch(ArithmeticException e)
{
System.out.println(e);
}
try
{
int a[]=new int[5];
a[5]=4;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);}

50
System.out.println("other statement");
}
catch(Exception e)
{
System.out.println("Exception handeled");}
System.out.println("casual flow");
}
}

51

You might also like