I.
FIBONACCI SERIES
PROGRAM
import java.util.*;
class fibonacci{
public static void main(String[] args)
{
Scanner x = new Scanner(System.in);
System.out.println("\n Enter the Value of 'n'
:"); int n = x.nextInt();
int a=0,b=1,c,i;
System.out.println("The Fibonacci Series is
= "); System.out.println(a+"\n"+b);
for(i=2;i<=n;i++){
c=a+b;
System.out.println(a+b);
a=b;
b=c;
}
System.out.println("\n");
x.close();
}
}
OUTPUT
II. SUM OF FIRST ‘N’ NATURAL NUMBERS
PROGRAM
import java.util.*;
class
SumOfNatural{
public static void main(String[] args){
Scanner x = new
Scanner(System.in);
System.out.println("\n Enter the Value Of
'n'="); int n = x.nextInt();
int i,s=0;
for(i=1;i<=n;i++)
s = s+i;
System.out.println("\n The Sum of First "+n+" Natural Numbers is =
"+s+"\n"); x.close();
}
}
OUTPUT
III. SUM OF FIRST ‘N’ EVEN NUMBERS
PROGRAM
import java.util.Scanner;
class SumOfEven{
public static void main(String[]
args){ int i,n,s=0;
try (Scanner x = new Scanner(System.in)) {
System.out.println("\n Enter the Value Of
'n'="); n = x.nextInt();
for(i=0;i<=n;i++){
if(i%2==0)
s += i;
}
System.out.println("The Sum of Even Numbers between 0 to "+n+" is = "+s+"\n");
}
}
}
OUTPUT
IV. FINDING ASCII VALUE
PROGRAM
import java.util.Scanner;
class ascii{
public static void main(String[] args) {
Scanner abc = new
Scanner(System.in); char a;
System.out.println("Enter Any
Key="); a = abc.next().charAt(0);
System.out.println("The ASCII Value of "+a+" is "+(int)a);
abc.close();
}
}
OUTPUT
V. REVERSE THE NUMBER
PROGRAM
import java.util.Scanner;
class ReverseNumber
{
public static void main(String[] args)
{
Scanner x = new
Scanner(System.in); int number,
reverse = 0;
System.out.println("Enter the Number= ");
number = x.nextInt();
while(number != 0)
{
int remainder = number % 10;
reverse = reverse * 10 +
remainder; number = number/10;
}
System.out.println("The reverse of the given number is " +
reverse); x.close();
}
}
OUTPUT
VI. ARMSTRONG NUMBER
PROGRAM
import java.util.*;
class Arm {
int power(int x, long y)
{
if (y == 0)
return 1;
if (y % 2 == 0)
return power(x, y / 2) * power(x, y /
2); return x * power(x, y / 2) * power(x, y /
2);
}
int order(int x)
{
int n = 0;
while (x != 0) {
n++;
x = x / 10;
}
return n;
}
void Armstrong(int x)
{
int n = order(x);
int temp = x, sum = 0;
while (temp != 0) {
int r = temp % 10;
sum = sum + power(r, n);
temp = temp / 10;
}
if(sum==x)
System.out.println(x+" is armstrong
number"); else
System.out.println(x+" is not armstrong number");
}
}
class armstrong{
public static void main(String[] args)
{
Arm ob = new Arm();
Scanner n = new
Scanner(System.in);
System.out.println("Enter Your Number =
"); int x = n.nextInt();
ob.Armstrong(x);
n.close();
}
}
OUTPUT
VII. ALTERNATE PRIME NUMBER
PROGRAM
import java.util.Scanner;
class apn{
public static void main(String[] args){
Scanner x = new
Scanner(System.in); int n,i,j,a=0,e;
System.out.println("Enter the
number:"); n = x.nextInt();
e=2;
System.out.println("Alternate Prime Numbers Are: ");
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
if(i%j==0)
a++;
}
if(a==2){
if(e%2==0
)
System.out.println(i);
e=e+1;
}
a=0
;
}
}
}
OUTPUT
VIII. DELETE DUPLICATE ELEMENTS IN ARRAY
PROGRAM
import java.util.Scanner;
class Delete_Duplicate{
public static void main(String[] args){
Scanner x = new
Scanner(System.in); int i,n;
System.out.println("Enter the value of N =
"); n = x.nextInt();
int[] arr = new int[n];
System.out.println("Enter the Elements one by one = ");
for(i=0;i<n;i++)
arr[i]=
x.nextInt(); for(i=0;
i<n; i++)
for(int j=i+1; j<n; j++)
if(arr[i] == arr[j])
{
for(int k=j; k < n - 1; k++)
arr[k] = arr[k + 1];
n--;
j--;
}
System.out.println("The Array after delete duplicate = ");
for(i=0;i<n;i++)
System.out.println(arr[i]);
x.close();
}
}
OUTPUT
IX. SORT THE ELEMENTS OF THE ARRAY
PROGRAM
import java.util.*;
class
AscendingArray{
public static void main(String[] args){
System.out.println("Enter the value of N = ");
Scanner x = new Scanner(System.in);
int n = x.nextInt();
int[] arr = new
int[n];
System.out.println("Enter the Elements one by one =
"); for(int i=0;i<n;i++){
arr[i]= x.nextInt();
}
Arrays.sort(arr);
System.out.println("The Sorted Array =
"+Arrays.toString(arr)); x.close();
}
}
OUTPUT
X. TWO MATRIX EQUAL
PROGRAM
import java.util.*;
class TME{
public static void main(String[]
args) { int p=0,q=0;
int[][] a = {{2,3,4,5},{3,2,1,0}};
int[][] b = {{2,3,4,5},{3,2,1,0}};
System.out.println(Arrays.deepToString(
a));
System.out.println(Arrays.deepToString(
b)); for(int i=0;i<a.length;i++){
for(int
j=0;j<a[i].length;j++){
p++;
if
(a[i][j]==b[i][j
]) q++;
}
}
if(p==q)
System.out.println("Two Matrices are
same"); else
System.out.println("Two Matrices are Different");
}
}
OUTPUT
STACK IMPLEMENTATION
PROGRAM
import java.util.*;
class arrayStack
{
protected int[] arr;
protected int top, size,
len; public
arrayStack(int n) {
size = n;
len = 0;
arr = new int[size];
top = -1;
}
public boolean isEmpty()
{
return top == -1;
}
public boolean isFull()
{
return top == size -1 ;
}
public int getSize()
{
return len ;
}
public int peek() {
if( isEmpty() )
throw new NoSuchElementException("Underflow
Exception"); return arr[top];
}
public void push(int i)
{ if(top + 1 >= size)
throw new IndexOutOfBoundsException("Overflow Exception");
if(top + 1 < size )
arr[++top] = i;
len++ ;
}
public int pop()
{ if(
isEmpty() )
throw new NoSuchElementException("Underflow
Exception"); len-- ;
return arr[top--];
}
public void display() {
System.out.print("\nStack =
"); if (len == 0) {
System.out.print("Empty\n");
return ;
}
for (int i = top; i >= 0; i--)
System.out.print(arr[i]+" ");
System.out.println();
}
}
class StackImplement {
public static void main(String[] args) {
Scanner scan = new
Scanner(System.in);
System.out.println("Enter Size of Integer Stack
"); int n = scan.nextInt();
arrayStack stk = new arrayStack(n);
System.out.println("\nStack Operations\n ---------- ");
System.out.println("1. push\n2. pop\n3. peek\n4. check empty\n5. check full\n 6.
size\n7.Display\n8.Exit");
do{
System.out.print("Enter your choice: "+" ");
int choice =
scan.nextInt(); switch
(choice) {
case 1 :
System.out.println("Enter integer element to push "+"
"); try{
stk.push( scan.nextInt() );
}
catch (Exception e) {
System.out.println("Error : " + e.getMessage());
}
break;
case 2 :
try {
System.out.print("Popped Element = "+ stk.pop());
}
catch (Exception e) {
System.out.println("Error : " + e.getMessage());
}
break;
case 3 :
try {
System.out.println("Peek Element = " +" "+ stk.peek());
}
catch (Exception e) {
System.out.println("Error : " + e.getMessage());
}
break
; case 4
:
System.out.println("Empty status = " +" "+ stk.isEmpty());
break;
case 5 :
System.out.println("Full status = " +" "+
stk.isFull()); break;
case 6 :
System.out.println("Size = " +" "+ stk.getSize());
break;
case 7:
stk.display();
break;
case 8:
System.out.println("Exit");
return ;
default :
System.out.println("Wrong Entry \n ");
break;
}
} while (true);
}
}
OUTPUT
QUEUE IMPLEMENTATION
PROGRAM
import
java.util.NoSuchElementException;
import java.util.Scanner;
class arrayQueue {
protected int[] Queue
;
protected int front, rear, size,
len; public arrayQueue(int n) {
size = n;
len = 0;
Queue = new
int[size]; front = rear
= -1;
}
public boolean
isEmpty(){ return
front == -1;
}
public boolean isFull() {
return front==0 && rear == size -1 ;
}
public int getSize() {
return len ;
}
public int peek()
{ if (isEmpty())
throw new NoSuchElementException("Underflow
Exception"); return Queue[front];
}
public void insert(int
i){ if (rear == -1) {
front = rear = 0;
Queue[rear] = i;
}
else if (rear + 1 >= size)
throw new IndexOutOfBoundsException("Overflow
Exception"); else if ( rear + 1 < size)
Queue[++rear] =
i; len++ ;
}
public int
remove(){ if
(isEmpty())
throw new NoSuchElementException("Underflow
Exception"); else {
len-- ;
int ele =
Queue[front]; if (
front == rear)
front = rear = -1;
else
front++;
return ele;
}
}
public void display(){
System.out.print("\nQueue =
"); if (len == 0) {
System.out.print("Empty\n");
return ;
}
for (int i = front; i <= rear; i++)
System.out.print(Queue[i]+" ");
System.out.println();
}
}
/* Class QueueImplement
*/ class QueueImplement
{
public static void main(String[] args) {
Scanner scan = new
Scanner(System.in);
System.out.println("Array Queue
Test\n");
System.out.println("Enter Size of Integer
Queue "); int n = scan.nextInt();
arrayQueue q = new arrayQueue(n);
System.out.println("\nQueue Operations\n ---------- ");
System.out.println("1. insert\n2. remove\n3. peek\n4. check empty\n5. check full\n6.
size\n7.Display\n8.Exit");
do{
System.out.print("Enter Your Choice:
"); int choice = scan.nextInt();
switch (choice) {
case 1 :
System.out.println("Enter integer element to
insert"); try {
q.insert( scan.nextInt() );
}
catch(Exception e) {
System.out.println("Error : " +e.getMessage());
}
break;
case 2 :
try {
System.out.println("Removed Element = "+q.remove());
}
catch(Exception e) {
System.out.println("Error : " +e.getMessage());
}
break;
case 3 :
try {
System.out.println("Peek Element = "+q.peek());
}
catch(Exception e) {
System.out.println("Error : "+e.getMessage());
}
break
; case 4
:
System.out.println("Empty status =
"+q.isEmpty()); break;
case 5 :
System.out.println("Full status =
"+q.isFull()); break;
case 6 :
System.out.println("Size = "+
q.getSize()); break;
case 7 :
q.display()
; break;
case 8:
System.out.println("Exit");
return;
default : System.out.println("Wrong Entry \n
"); break;
}
} while(true);
}
}
OUTPUT
INSERTION SORT
PROGRAM
//Java Program to Implement Insertion
Sort import java.util.Scanner;
class InsertionSort {
public static void sort( int arr[]
){ int N = arr.length;
int i, j, temp;
for (i = 1; i< N; i++)
{ j = i;
temp = arr[i];
while (j > 0 && temp < arr[j-1]){
arr[j] = arr[j-1];
j = j-1;
}
arr[j] = temp;
}
}
public static void main(String[] args){
Scanner scan = new Scanner( System.in
); System.out.println("Insertion Sort
Test\n"); int n, i;
System.out.println("Enter number of integer elements
= "); n = scan.nextInt();
int arr[] = new int[ n ];
System.out.println("\nEnter "+ n +" integer elements
= "); for (i = 0; i < n; i++)
arr[i] = scan.nextInt();
sort(arr);
System.out.println("\nElements after sorting
= "); for (i = 0; i < n; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
}
OUTPUT
I. SELECTION SORT
PROGRAM
//Java Program to Implement Selection
Sort import java.util.Scanner;
class SelectionSort {
public static void sort( int arr[]
){ int N = arr.length;
int i, j, pos, temp;
for (i = 0; i < N-1;
i++){ pos = i;
for (j = i+1; j < N;
j++) if (arr[j] <
arr[pos])
pos = j;
temp = arr[i];
arr[i] =
arr[pos];
arr[pos]= temp;
}
}
public static void main(String[] args) {
Scanner scan = new Scanner( System.in
); System.out.println("Selection Sort
Test\n"); int n, i;
System.out.println("Enter number of integer elements
= "); n = scan.nextInt();
int arr[] = new int[ n ];
System.out.println("\nEnter "+ n +" integer elements
= "); for (i = 0; i < n; i++)
arr[i] = scan.nextInt();
sort(arr);
System.out.println("\nElements after sorting
= "); for (i = 0; i < n; i++)
System.out.print(arr[i]+" ");
System.out.println();
}
}
OUTPUT
II. BINARY SEARCH
PROGRAM
// Program: Binary Search
Example import
java.util.Scanner;
class BinarySearchExample{
public static void main(String args[]){
int counter, num, item, array[], first, last,
middle; Scanner input = new
Scanner(System.in); System.out.println("Enter
number of elements:"); num = input.nextInt();
array = new int[num];
System.out.println("Enter " + num + "
integers:"); for (counter = 0; counter < num;
counter++)
array[counter] = input.nextInt();
System.out.println("Enter the search
value:"); item = input.nextInt();
first = 0;
last = num - 1;
middle = (first +
last)/2; while( first <=
last )
{
if ( array[middle] < item )
first = middle + 1;
else if ( array[middle] == item )
{
System.out.println(item + " found at location " + (middle + 1) +
"."); break;
}
else
last = middle - 1;
middle = (first +
last)/2;
}
if ( first > last )
System.out.println(item + " is not found.\n");
}
}
OUTPUT
III. LINEAR SEARCH
PROGRAM
// Program: Linear Search
Example import
java.util.Scanner;
class LinearSearchExample{
public static void main(String
args[]){ int counter, num, item,
array[];
Scanner input = new Scanner(System.in);
System.out.println("Enter number of
elements:"); num = input.nextInt();
array = new int[num];
System.out.println("Enter " + num + "
integers:"); for (counter = 0; counter < num;
counter++) array[counter] = input.nextInt();
System.out.println("Enter the search value:");
item = input.nextInt();
for (counter = 0; counter < num;
counter++){ if (array[counter] == item)
{
System.out.println(item+" is present at location
"+(counter+1)); break;
}
}
if (counter == num)
System.out.println(item + " doesn't exist in array.");
}
}
OUTPUT
PROGRAM
import java.util.Scanner;
class Employee{
String name,addr,mail;int id,mobile;double netsalary,salary,da,hra,pf,scf,bp;
Scanner x = new Scanner(System.in);
public void getemp(){
System.out.print("\n Enter Your Name =
"); name = x.next();
System.out.print("\n Enter Your Employee ID
= "); id = x.nextInt();
System.out.print("\n Enter Your Address =
"); addr = x.next();
System.out.print("\n Enter Your Mail ID = ");
mail = x.next();
System.out.print("\n Enter Your Mobile Number = ");
mobile = x.nextInt();
}
public void cal(Double
bp){ da = 0.97 * bp;
hra = 0.10/100 *
bp; pf = 0.12/100
*bp; scf =
0.001/100 * bp;
salary =
bp+hra+da;
netsalary = salary - pf - scf;
}
public void display(){
System.out.println("\n Name = "+name+"\n Employee ID = "+id+"\n Address =
"+addr+"\n Mail Id = "+mail+"\n Mobile Number = "+mobile+"\n Basic Pay = "+bp+"\n
HRA = "+hra+"\n DA
= "+da+"\n PF = "+pf+"\n Staff Club Fund = "+scf+"\n Salary = "+salary+"\n NetSalary =
"+netsalary);
}
}
class Programmer extends Employee{
double bp;
public void getpro(){
System.out.print("\n Enter Programmer
Details"); getemp();
System.out.print("\n Enter Your Basic Pay =
"); bp = x.nextDouble();
cal(bp);
display()
;
}
}
class AssistantProfessor extends
Employee{ double bp;
public void getasspro(){
System.out.print("\n\n Enter Assistant Professor
Details"); getemp();
System.out.print("\n Enter Your Basic Pay =
"); bp = x.nextDouble();
cal(bp);
display()
;
}
}
class AssociateProfessor extends
Employee{ double bp;
public void getassopro(){
System.out.print("\n\n Enter Associate Professor
Details"); getemp();
System.out.print("\n Enter Your Basic Pay =
"); bp = x.nextDouble();
cal(bp);
display()
;
}
}
class Professor extends
Employee{ double bp;
public void getprof(){
System.out.print("\n\n Enter Professor
Details"); getemp();
System.out.print("\n Enter Your Basic Pay =
"); bp = x.nextDouble();
cal(bp);
display()
;
}
}
class Hierarchy{
public static void main(String[] args){
Programmer pro = new
Programmer();
AssistantProfessor ap = new AssistantProfessor();
AssociateProfessor asp = new AssociateProfessor();
Professor p = new Professor();
Scanner x = new Scanner(System.in);
System.out.println("\n Menu\n - \n 1.Programmer Details\n 2. Assistant Professor Details\n
3.Associate Professor Details \n 4.Professor
Details\n"); System.out.println("Enter Which
You Want:-"); int a = x.nextInt();
switch(a){
case 1:
pro.getpro();
break;
case 2:
ap.getasspro();
break;
case 3:
asp.getassopro();
break;
case 4:
p.getprof()
; break;
default:
System.out.println("Enter the Correct Choice!");
break;
}
}
}
OUTPUT
PROGRAM
interface Shape {
int a=10;
int b=10;
void printArea();
}
class Circle implements Shape{
int r;
public Circle(){
r=a;
}
@Override
public void printArea() {
System.out.println("Area of Circle : "+(3.14*r*r));
}
}
class Rectangle implements Shape{
int l,br;
public Rectangle(){
l=a;
br=b;
}
@Override
public void printArea() {
System.out.println("Area of Rectangle: "+(l*br));
}
}
class Triangle implements Shape{
int h,ba;
public Triangle(){
h=a;
ba=b;
}
@Override
public void printArea() {
System.out.println("Area of Triangle: "+(0.5*ba*h));
}
class Main{
public static void main(String[] args){
Circle c = new Circle();
c.printArea();
Rectangle r = new Rectangle();
r.printArea();
Triangle t = new Triangle();
t.printArea();
}
}
OUTPUT
PROGRAM
import java.util.Scanner;
class NegativeAmtException extends Exception
{
String msg;
NegativeAmtException(String msg)
{
this.msg=msg;
}
public String toString()
{
return msg;
}
}
public class UserDefinedException
{
public static void main(String[]args)
{
Scanner s=new Scanner(System.in);
System.out.print("Enter Amount:");
int a=s.nextInt();
try{
if(a<0)
{
throw new NegativeAmtException("Invalid Amount");
}
System.out.println("Amount Deposited");
}
catch(NegativeAmtException e)
{
System.out.println(e);
}
}
}
OUTPUT
PROGRAM
class NewThread extends Thread
{
public void run()
{
try
{
for(int i=5;i>0;i--)
{
System.out.println("child thread:"+i);
Thread.sleep(200);
}
}
catch(InterruptedException e)
{
System.out.println("child interrupted");
}
System.out.println("Exiting child thread");
}
}
class ExtendThread
{
public static void main(String args[])
{
NewThread NT=new NewThread();
NT.start();
try{
for(int i=5;i>0;i--)
{
System.out.println("main thread:"+i);
Thread.sleep(200);
}
}
catch(InterruptedException e)
{
System.out.println("main thread interrupted");
}
System.out.println("main thread exiting");
}
}
OUTPUT
PROGRAM
import java.io.*;
public class FileInfo
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter a file name");
String fName=br.readLine();
File f=new File(fName);
String result=f.exists()?"exists":"does not exists";
System.out.println("The given file "+result);
if(f.exists())
{
System.out.println("The given file length "+f.length()+"in
byte");
if(fName.endsWith(".jpg")||fName.endsWith(".gif")||fName.endsWith(".png"))
{
System.out.println("The given file is an image file");
}
else if(fName.endsWith(".exe"))
{
System.out.println("The given file is an executable file");
}
else if(fName.endsWith(".txt"))
{
System.out.println("The given file is a text file");
}
else
{
System.out.println("The file type is unknown");
}
}
}
}
OUTPUT