0% found this document useful (0 votes)
35 views25 pages

JAVA Programs

The document contains multiple Java programs demonstrating various data structures and algorithms, including Queue, Stack, GCD calculation, time addition, date conversion, special number checking, array shifting, leap year validation, rounding numbers, sentence manipulation, word pattern combination, and string searching. Each program includes user input handling, core logic implementation, and output display. The programs serve as practical examples for understanding fundamental programming concepts in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views25 pages

JAVA Programs

The document contains multiple Java programs demonstrating various data structures and algorithms, including Queue, Stack, GCD calculation, time addition, date conversion, special number checking, array shifting, leap year validation, rounding numbers, sentence manipulation, word pattern combination, and string searching. Each program includes user input handling, core logic implementation, and output display. The programs serve as practical examples for understanding fundamental programming concepts in Java.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

JAVA programs

DATA STRUCTURE PROGRAMS


1) Queue

import java.util.*;
public class Queue
{
int arr[];
int front , rear;
static int size, opt;

Queue(int n)
{
size = n;
arr= new int[size]; front = -1 ; rear = -1;
}

void enqueue(int x)
{
if(rear ==(size-1))
{
System.out.println("Overflow");
}
else if(front == -1 && rear =
rear++;
arr[rear] = x;
}
else
{
rear +=1;
arr[rear] = x;
}
}

int dequeue()
{
if(front == -1 && rear == -1)
{
System.out.println("Underflow");
return -9999;
}
else if(front == rear)
{
int val = arr[front];
front = rear = -1;
return val;
}
else
{
int val = arr[front];
front +=1;
System.out.print("The deleted value is ");
return val;
}
}

void display()
{
if(front == -1 && rear == -1)
{
System.out.println("Empty queue");
}
else
{
System.out.println("The Queue is :");
for(int i = front ; i<= rear ; i++)
{
System.out.print(arr[i] +" | ");
}
System.out.println("");
}
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
/*Scanner so = new Scanner(System.in);*/
System.out.println("Enter the size of the queue");
int n1 = sc.nextInt();
Queue o = new Queue(n1);
System.out.println("Enter 1 : To add an element" + "\nEnter 2 : To remove an element");
while(n1>0)
{
opt= sc.nextInt();
if(opt == 1)
{
System.out.println("Enter input value");
int x1 = sc.nextInt();
o.enqueue(x1);
o.display();
}
else if(opt == 2)
{
int out = o.dequeue();
System.out.println(out);
o.display();
}
}
}

2) Stack

import java.util.*;
public class Stack
{
static int arr2[];
static int size , top ;
Stack(int n)
{
size = n;
arr2 = new int[size]; top = -1;
}

void Push(int x)
{
if(top == (size-1))
{
System.out.println("Overflow");
}
else
{
top += 1;
arr2[top] = x;
}
}

int Pop()
{
if(top == -1)
{
System.out.println("Underflow");
return -9999;
}
else
{
int val = arr2[top];
top -= 1;
System.out.println("The deleted element is "+val);
return val;
}
}
void display()
{
if(top == -1)
{
System.out.println("The Stack is empty");
}
else
{
System.out.println("The Stack is :");
for(int i =top ; i>=0 ; i--)
{
System.out.println(arr2[i]);
}
}
}

public static void main(String args[])


{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the Stack");
int n1 = sc.nextInt();
Stack o = new Stack(n1);
System.out.println("Enter 1 to Add elements"+"\nEnter 2 to remove element");
while(n1>0)
{
int opt= sc.nextInt();
if(opt == 1)
{
System.out.println("Enter the value");
int x = sc.nextInt();
o.Push(x);
o.display();
}
else if(opt ==2)
{
o.Pop();
o.display();
}
}
}
}

3)import java.util.*;
public class Gcd
{
int num1 , num2,a;
Gcd()
{
num1=0;num2=0;a=0;
}

void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the smaller number");
num1 = sc.nextInt();
System.out.println("Enter the larger number");
num2 = sc.nextInt();
a = num1;
}
int gcd(int x , int y)
{
while(a>0)
{
if(x%a==0 && y%a==0)
{
return a;
}
else
{
--a;
gcd(num1,num2);
}
}
return a;
}
void display()
{
int out = gcd(num1,num2);
System.out.println("The gcd of the two numbers "+out);
}
public static void main(String args[])
{
Gcd o = new Gcd();
o.accept();
o.display();
}
}

4) public class Adder


{
int a[]= new int[2];
Adder()
{
a[0] = 0 ; a[1] =0;
}
void readtime()
{

Scanner sc = new Scanner(System.in);


System.out.println("Enter the time in hours");
a[0]=sc.nextInt();
System.out.println("Enter the time in minutes");
a[1] = sc.nextInt();
}

void addtime(Adder X , Adder Y)


{
Adder out = new Adder();
out.a[0] = X.a[0]+Y.a[0];
out.a[1] =X.a[1]+Y.a[1];
if((out.a[1])>=60)
{
out.a[1]-=60;
out.a[0] +=1;
}
System.out.print("Total time : ");
out.disptime();
}

void disptime()
{
System.out.println(a[0]+" Hours "+a[1]+" minutes");
}
public static void main(String args[])
{
Adder o1 = new Adder();
Adder o2 = new Adder();
Adder o3 = new Adder();
o1.readtime();
o2.readtime();
System.out.print("Time1 : ");
o1.disptime();
System.out.print("Time 2 : ");
o2.disptime();
o3.addtime(o1,o2);
}
}
5) import java.util.*;
public class Convert
{
int n , d ,m ,y;
int arr[] = {31,28,31,30,31,30,31,31,30,31,30,31};
String arr1[] =
{"January","February","March","April","May","June","July","August","September","October","Nov
ember","December"} ;

Convert()
{
n = 0 ; d=0 ; m=0 ; y=0;
}

void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the day number");
n = sc.nextInt();
System.out.println("Enter the year");
y = sc.nextInt();
if(((y%100 != 0)&&(y%4 == 0)) || ((y%100 ==0)&&(y% 400 ==0)))
{
arr[1] =29;
}
}
void day_to_date()
{
for(int i =0 ; i<12 ; i++)
{
if(n>arr[i])
{
n= n-arr[i];
}
else
{
d=n;
m = i;
break;

}
}
}

void display()
{
System.out.println("The Date is : "+"\n"+arr1[m]+" "+d+","+y);
}

public static void main(String args[])


{
Convert o = new Convert();
o.accept();
o.day_to_date();
o.display();
}

6)import java.util.*;
public class Special
{
int n;
Special()
{
n =0;
}
void read()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
n = sc.nextInt();
}
int factorial(int x)
{
if(x==0)
{
return 1;
}
else
{
return x*factorial(x-1);
}
}
Boolean isSpecial()
{
int temp = n;
int sum =0;
do{
int d = temp%10 ;
int out = factorial(d);
sum +=out;
temp=temp/10;
}while(temp!=0);
if(sum==n)
{
return true ;
}
else
{
return false ;
}
}
void display()
{
boolean chk = isSpecial();
if(chk == true)
{
System.out.println("The number is a Special number");
}
else
{
System.out.println("The number is not a Special number");
}
}

public static void main(String args[])


{
Special o = new Special();
o.read();
o.isSpecial();
o.display();
}
}

7)import java.util.*;
public class z_shift
{
Scanner sc = new Scanner(System.in);
int arr[][]; int n;
int arr1[][];
void input()
{
System.out.println("Enter the no. of columns");
n = sc.nextInt();
arr = new int[2][n];
arr1 = new int[2][n];
System.out.println("Enter the data");
for(int i =0 ; i<2 ;i++)
{
for(int j = 0;j<n;j++)
{
arr[i][j] = sc.nextInt();
}
}
}

void dis()
{
System.out.println("The original array");
for(int i=0;i<2;i++)
{
for(int j=0;j<n ;j++)
{
System.out.print(arr[i][j] +" ");
}
System.out.println("");
}
}

void shift()
{
for(int i = 0;i<2;i++)
{
for(int j = 0; j<n;j++)
{
if(i==0)
{
arr1[i][j]= arr[i+1][j];
}
else
{
if(j==0)
{
arr1[i][j] = arr[0][n-1];
}
else
{
arr1[i][j] = arr[i-1][j-1];
}
}
}
}
}
void display()
{
System.out.println("The shifted array");
for(int i=0;i<2;i++)
{
for(int j=0;j<n ;j++)
{
System.out.print(arr1[i][j] +" ");
}
System.out.println("");
}
}

public static void main(String args[])


{
z_shift o = new z_shift();
o.input();
o.dis();
o.shift();
o.display();
}
}

8)import java.util.*;
public class leap
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
int day = sc.nextInt();
if((year%100 != 0 )&&(year %4 ==0)&&(day>366))
{
System.out.println("Invalid year");
System.exit(0);
}
else if((year %100 ==0)&&(year %400 ==0)&&(day>366))
{
System.out.println("Invalid year");
System.exit(0);
}

else if(day >365)


{
System.out.println("Invalid year");
System.exit(0);
}

int month[] = {31,28,31,30,31,30,31,31,30,31,30,31};


if(((year %100 != 0)&&(year %4 == 0)) || ((year %100 ==0)&&(year % 400 ==0)))
{
month[1] =29;
}
int c =0;
do{
if(day >= month[c])
{
day -= month[c];
}
else
{
System.out.println(day+"/" +(c+1));
System.exit(0);
}
c++;
}while(true);
}
}

9)import java.util.*;
public class Round_off
{
Scanner sc = new Scanner(System.in);
String num ; int n,l, pos;
char arr[] ;
Round_off()
{
num ="" ; n =0; l =0; pos =0;
}
void input()
{
System.out.println("Enter the number");
num = sc.nextLine();
System.out.println("Enter the number of decimal places to be rounded of");
n = sc.nextInt();
l = num.length() +1;
arr = new char[l];
for(int i= 0;i<l;i++)
{
arr[i]= num.charAt(i);
System.out.println(arr[i]);
}
}
void round()
{
for(int j=0; j<l; j++)
{
if(num.charAt(j) == '.')
{
pos =j;
}
}

for(int k=0;k<(pos+n);k++)
{
System.out.print(arr[k]);
}
}
public static void main(String args[])
{
Round_off o = new Round_off();
o.input();
o.round();
}
}

10)import java.util.*;
public class sentence
{
Scanner sc = new Scanner(System.in);
String sen; int length; String word;
char arr[];
sentence()
{
sen =""; length =0; word = "";
}

void input()
{
System.out.println("Enter your sentence");
sen = sc.nextLine();
}
void convert()
{
String sen1 = " "+ sen;
length = sen1.length();
arr = new char[length+1];
for(int i=0;i< length ;i++)
{
char ch = sen1.charAt(i);
arr[i] = ch;
}
for(int j =0;j< length ; j++)
{
char c = arr[j];
if(c == ' ')
{
char c1 = arr[j+1];
int c2= (int)c1-32;
arr[j+1] = (char)c2;
}
}
for(int k =1;k< length+1 ; k++)
{
System.out.print(arr[k]);
}
}

void word()
{
System.out.println("");
System.out.print("Word \t Vowel \t Consonant");
int cnt1 =0 ; int cnt2 =0;
int start =0;
sen = sen +" ";
int len = sen.length();
char arr2[] = new char[len];
for(int i =0;i<len; i++)
{
arr2[i]= sen.charAt(i);
}

for(int j=0;j<len;j++ )
{
char c5 = sen.charAt(j);
if(c5 == ' ')
{
word = sen.substring(start,j);
start = j+1;
word = word.toUpperCase();
System.out.println("");

for(int i =0;i<word.length() ; i++)


{
char c3 = word.charAt(i);
if(c3 == 'A' || c3== 'E' ||c3== 'I' || c3=='O' ||c3== 'U')
{
cnt1++;
}
else
{
cnt2++;
}
}
System.out.print(word +" \t " +cnt1 +" \t " + cnt2);
System.out.println("");
cnt1=0;cnt2=0;
}
}
}

public static void main(String args[])


{
sentence o = new sentence();
o.input();
o.convert();
o.word();
}
}

11)import java.util.*;
public class Word_pattern
{
Scanner sc = new Scanner(System.in);
String sen1 ,sen2;
char arr1[] , arr2[];
Word_pattern()
{
sen1 = ""; sen2 = "";
}
void input()
{
System.out.println("Enter two words of same length");
sen1 = sc.nextLine();
sen2 = sc.nextLine();
int l1 = sen1.length();
int l2 = sen2.length();
if(l1 == l2)
{
System.out.println("Input valid");
arr1 = new char[l1];
arr2 = new char[l2];
}
else
{
System.out.println("Input invalid");

}
}

void combine()
{
for(int i=0 ; i<sen1.length() ; i++)
{
char c1 = sen1.charAt(i);
arr1[i] = c1;
}
for(int j=0 ;j<sen1.length() ; j++)
{
char c2 = sen2.charAt(j);
arr2[j] = c2;
}
for(int k =0;k<sen1.length() ; k++)
{
System.out.print(arr1[k]);
System.out.print(arr2[k]);
}

public static void main(String args[])


{
Word_pattern o = new Word_pattern();
o.input();
o.combine();
}
}
12)import java.util.*;
public class Search
{
static String word="";
static int c= 0;
static int pos = 0;
static int find(String w, int i, String wor)
{
String s="";
if(i== w.length())
{
return c;
}
else
{
char ch = w.charAt(i);
if(ch == ' ')
{
s = w.substring(pos,i);
if(s.equalsIgnoreCase(wor))
{
c++;
}
else
{
s = "";
pos = i+1;
find(w,pos,wor);
}

}
else
{
find(w,i+1,wor);
}
}
return c;
}

public static void main(String args[])


{
Search o = new Search();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sentence");
String sen = sc.nextLine();
sen = sen+" ";
System.out.println("Enter the word to find");
String w1 = sc.nextLine();
int out = find(sen,0,w1);
if(out ==1)
{
System.out.println("word found");
}
}
}

13)import java.util.*;
public class Number
{
long num ;
static int arr[] = {0,0,0,0,0,0,0,0,0,0};
static int sum =0;
Number()
{
num =0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
num= sc.nextLong();
}

void digit_freq()
{
while(num>0)
{
int d = (int)(num%10);
arr[d]+=1;
num/=10;
}
}

static int digit_sum()


{
for(int i=0; i<10; i++)
{
sum += i*arr[i];
}
return sum;
}

public static void main(String args[])


{
Number o = new Number();
o.digit_freq();
for(int i=0; i<10; i++)
{
System.out.println(i+" "+arr[i]);
}
int out = digit_sum();
System.out.println("Sum of all digits = "+out);
}
}

14)import java.util.*;
public class valid
{
static int n;
static int sum =0;
valid()
{
n=0;
}
static void getnum(int num)
{
n = num;
}

static int sumdigits(int inp)


{
if(inp>0)
{
int d = inp%10;
sum += d;
inp/=10;
sumdigits(inp);
}
else
{
if(sum >9)
{
inp = sum;
sum =0;
sumdigits(inp);
}
}
return sum;
}

void dovalid()
{
System.out.println(sum);
if(sum ==1)
{
System.out.println("The number is valid");
}
else
{
System.out.println("The number is invalid");
}
}

public static void main(String args[])


{
valid o = new valid();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
n = sc.nextInt();
o.sumdigits(n);
o.dovalid();
}
}

import java.util.*;
public class Longest_word
{
String sen , s1 , longest ;
int a , l2 ;
Longest_word()
{
sen = "" ; s1 = "" ; longest= " ";
a = 0 ; l2 = 0 ;
}
void input()
{
Scanner ls = new Scanner(System.in);
System.out.print(" THIS PROGRAM WILL HELP YOU FIND THE LONGEST WORD IN YOUR
SENTENCE " + "\n Enter your sentence : ");
sen = ls.nextLine();
sen += " ";
}
void seperate_check()
{
int start = 0 ;
a = sen.length() -1 ;
for(int j = 0 ; j <= a ; j++)
{
char sp = sen.charAt(j);
if(sp == ' ')
{
s1 = sen.substring(start,j);
start = j+1 ;
int l = s1.length();
l2 = longest.length();
if(l >= l2)
{
longest = s1 ;
}
else
{
longest = longest ;
}
}
}
System.out.println("The longest word in your sentence is " + longest );
System.out.println("Its length is " + l2);
}
public static void main(String args[])
{
Longest_word o = new Longest_word();
o.input();
o.seperate_check();
}
}

import java.util.*;
public class Rev_prime
{
int n1 , n2 , c , r , n_rev ;
String num , Rev_num ;
Scanner sc = new Scanner(System.in);
Rev_prime()
{
n1 = 0; r = 0; c = 0; n_rev = 0;
String num = ""; Rev_num = "";
}
void Prime()
{
System.out.print("To check whether a number is prime or not " + "\n Enter your Number : ");
n1 = sc.nextInt();

for(int i =1; i<=n1 ; i++)


{
r = n1%i;
if(r==0)
{
c++ ;
}
}
if(c==2)
{
System.out.println("The no. is prime ");
}
else
{
System.out.println("The no. is composite");
}
}

void Reverse()
{
System.out.print("To Reverse a Given Number"+"\n Enter the number : ");
n2 = sc.nextInt();
num = String.valueOf(n2);
int a = num.length() -1 ;
for(int i = a ; i >= 0 ; i--)
{
char ch = num.charAt(i);
Rev_num += ch ;

}
System.out.println("Your reversed number = " + Rev_num);
}
public static void main(String args[])
{
Rev_prime ob = new Rev_prime();
ob.Prime();
ob.Reverse();
}

import java.util.*;
public class Switch_Rev
{
String input , word , sen1 , sen2 , b , s1 , longest ;
char b1 , sp ;
Scanner sc = new Scanner(System.in);
void inp()
{
input = ""; word =""; sen1 = ""; sen2 = ""; b= "";
longest = ""; s1 = "";
System.out.println("Enter Pallindrome to check whether a word is pallindrome or not"+"\n Enter
Frequecy_vowel to check the frequecy of a vowel in a sentence" + "\n Enter Largest_word to
find the largest word of your sentence ");
input = sc.nextLine();
}
void Pro()
{
switch(input)
{
case "Pallindrome" :
System.out.println("Enter the word ");
word = sc.nextLine();

int a = word.length() - 1;
for(int i = a; i >= 0 ; i--)
{
b += word.charAt(i);
}
if(word.equalsIgnoreCase(b))
{
System.out.println(word+" is a Pallindrome");
}
else
{
System.out.println(word+" is not a Pallindrome");
}

break ;

case "Frequency_vowel" :

int n = 0 ;
System.out.println("Enter the sentence : ");
sen1 = sc.nextLine();
int a1 = sen1.length() -1 ;
for(int j = 0 ; j<= a1 ; j++)
{
b1 = sen1.charAt(j);
if(b1=='a' || b1 =='e' || b1 == 'i' || b1 =='o' || b1 =='u' ||b1 =='A' || b1 =='E' || b1 == 'I' || b1
=='O' || b1 =='U' )
{
n++;
}
}
System.out.println("There are "+n+" vowels in your sentence.");

break ;

case "Largest_word" :
System.out.println("Enter your Sentence : ");
sen2 = sc.nextLine();
sen2 += " ";
longest =" ";

int start = 0 ; int l2 = 0; int a2 = 0;


a2 = sen2.length() -1 ;
for(int k = 0 ; k <= a2 ; k++)
{
char sp = sen2.charAt(k);
if(sp == ' ')
{
String s1 = sen2.substring(start,k);
start = k+1 ;
int l = s1.length();
l2 = longest.length() ;
if(l >= l2)
{
longest = s1 ;
l2 = l;
}
else
{
longest = longest ;
}
}
}
System.out.println("The longest word in your sentence is " + longest+" \n Its length is " + l2);
}
}
public static void main(String args[])
{
Switch_Rev ob = new Switch_Rev();
ob.inp();
ob.Pro();
}
}

import java.util.*;
public class call_by_ref_onevar_area
{
Scanner sc = new Scanner(System.in);
int a = 0 ; static double ar = 0.0 ; static int n = 0 ;
static double are = 0.0 ;

void input(int x)
{

a=x;
}
call_by_ref_onevar_area area()
{
call_by_ref_onevar_area o1 = new call_by_ref_onevar_area();
o1.a = sc.nextInt();

call_by_ref_onevar_area o2 = new call_by_ref_onevar_area();


o2.a = sc.nextInt();

call_by_ref_onevar_area o3 = new call_by_ref_onevar_area();


double s = (double)(a + o1.a + o2.a)/2.0;

&nbsp; o3.ar = (double)Math.sqrt(s*(s-a)*(s - o1.a)*(s - o2.a));

return o3;
}
public static void main(String args[])
{
Scanner so = new Scanner(System.in);
System.out.println("Enter the sides of a Triangle ");
call_by_ref_onevar_area o4 = new call_by_ref_onevar_area();
n = so.nextInt();
o4.input(n);

System.out.println("The Area = " + o4.area().ar);


}
}

You might also like