Java Programs Compilation
Topic | Code | Input | Output
Array Programs
• Rotate Array Left by d Positions
• Move All Zeros to End of Array
• Find Three Largest Numbers in Array
• Find the Missing Number (1 to N)
• Find Kth Largest Element in Array
• Find Index of Kth Largest Element
• Find Kth Smallest Element in Array
• Find Index of Kth Smallest Element
• Bulb Switcher - Perfect Square Bulb Numbers (O(sqrt N))
• Bulb Switcher - Count of Bulbs ON (O(sqrt N))
• Total Handshakes Among N People (nC2)
• Survival on Island - Minimum Days to Buy Food
Number Programs
• Check Armstrong Number
• Count Number of Digits
• Check Palindrome Number
• Check Prime Number
• Reverse a Number
• Sum of Digits of a Number
String Programs
• Check Palindrome String (ignores case/punctuation)
• Reverse Order of Words in a Sentence
Other Programs
• Convert 12-Hour Time Format to 24-Hour Format
Array Programs
Rotate Array Left by d Positions
File: [Link]
Code:
//Rotate Array (Left Rotate by d)
import [Link];
public class Darray {
static void reverse(int arr[], int start, int end)
{
while(start<end){
int t = arr[start];
arr[start] = arr[end];
arr[end] = t;
start++;
end--;
}
}
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);
int n =[Link]();
int arr[] = new int[n];
for(int i=0; i<n; i++)
{
arr[i]= [Link]();
}
int d= [Link]()%n;
reverse(arr,0, d-1);
reverse(arr,d, n-1);
reverse(arr,0, n-1);
for(int x: arr)
{
[Link](x+ " ");
}
}}
Sample Input:
n = 7
arr = 1 2 3 4 5 6 7
d = 2
Sample Output:
3
4
5
6
7
1
2
Move All Zeros to End of Array
File: [Link]
Code:
import [Link].*;
class ChocolateArray {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n =[Link]();
int a[] = new int[n];
int j=0;
for(int i=0;i<n;i++)
{
a[i] = [Link]();
if(a[i]!=0)
{
a[j] = a[i];
j++;
}
}
while(j<n)
{
a[j]=0;
j++;
}
for(int i =0; i<n; i++)
{
[Link](a[i]+" ");
}
}
}
Sample Input:
n = 5
arr = 0 1 0 3 12
Sample Output:
1 3 12 0 0
Find Three Largest Numbers in Array
File: [Link]
Code:
import [Link].*;
public class ThreeLargestNumber {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int arr[] = new int[n];
for(int i=0;i<n;i++)
arr[i]=[Link]();
[Link](arr);
[Link](arr[n-1]+" "+arr[n-2]+" "+arr[n-3]);
}
}
Sample Input:
n = 6
arr = 3 2 1 5 6 4
Sample Output:
6 5 4
Find the Missing Number (1 to N)
File: [Link]
Code:
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import [Link].*;
class MissingNumber
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
int n = [Link]();
int a[] = new int[n];
int sum = 0;
for(int i=0; i<n-1; i++)
{
a[i]= [Link]();
sum+=a[i];
}
int total = n*(n+1)/2;
[Link](total-sum);
}
}
Sample Input:
n = 5
(enter n-1 = 4 numbers)
arr = 1 2 4 5
Sample Output:
3
Find Kth Largest Element in Array
File: [Link]
Code:
import [Link].*;
class KthLargestElement {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n= [Link]();
int a[] = new int[n];
for(int i=0; i<n; i++)
{
a[i]=[Link]();
}
int k = [Link]();
if(k>0 && k<=n){
[Link](a);
[Link](a[n-k]);
}
else{
[Link]("invalid");}
}
}
Sample Input:
n = 6
arr = 3 2 1 5 6 4
k = 2
Sample Output:
5
Find Index of Kth Largest Element
File: [Link]
Code:
import [Link].*;
class KthLargestIndexValue {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int arr[] = new int[n];
int temp[] = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = [Link]();
temp[i] = arr[i];
}
int k = [Link]();
[Link](temp);
int kth = temp[n-k];
for(int i = 0; i < n; i++) {
if(arr[i] == kth) {
[Link](i);
break;
}
}
}
}
Sample Input:
n = 6
arr = 3 2 1 5 6 4
k = 2
Sample Output:
3
Find Kth Smallest Element in Array
File: [Link]
Code:
import [Link].*;
class KthSmallestElement {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n= [Link]();
int a[] = new int[n];
for(int i=0; i<n; i++)
{
a[i]=[Link]();
}
int k = [Link]();
if(k>0 && k<=n){
[Link](a);
[Link](a[k-1]);
}
else
{
[Link]("invalid");
}
}
}
Sample Input:
n = 6
arr = 3 2 1 5 6 4
k = 2
Sample Output:
2
Find Index of Kth Smallest Element
File: [Link]
Code:
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import [Link].*;
class KthSmallestIndexValue {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int arr[] = new int[n];
int temp[] = new int[n];
for(int i=0; i<n; i++)
{
arr[i]=[Link]();
temp[i]=arr[i];
}
int k= [Link]();
[Link](temp);
int kth = temp[k-1];
for(int i=0; i<n; i++)
{
if(arr[i]==kth)
{
[Link](i);
break;
}
}
}}
Sample Input:
n = 6
arr = 3 2 1 5 6 4
k = 2
Sample Output:
1
Bulb Switcher - Perfect Square Bulb Numbers (O(sqrt N))
File: [Link]
Code:
import [Link].*;
public class BulbNumbersON {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int limit = (int)[Link](n);
for(int i=1;i<=limit; i++)
{
[Link](i*i+" ");
}
}
}
Sample Input:
n = 10
Sample Output:
1
4
9
Bulb Switcher - Count of Bulbs ON (O(sqrt N))
File: [Link]
Code:
import [Link].*;
public class BulbSwitcherCountBulbsON {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int count = (int)[Link](n);
[Link](count);
}
}
Sample Input:
n = 10
Sample Output:
3
Total Handshakes Among N People (nC2)
File: [Link]
Code:
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import [Link].*;
class Handshake
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
int t = [Link]();
while(t>0)
{
long n = [Link]();
long handshakes = (n*(n-1))/2;
[Link](handshakes);
t--;
}
}
}
Sample Input:
t = 2
n = 3
n = 5
Sample Output:
3
10
Survival on Island - Minimum Days to Buy Food
File: [Link]
Code:
import [Link].*;
public class SurvivalOnIsland {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int N = [Link]();
int M =[Link]();
int S= [Link]();
int sundays = S/7;
int Buyfood = S-sundays;
int totalfood = M*S;
int minBuyDays;
if(totalfood%N==0)
{
minBuyDays = totalfood/N;
}
else{
minBuyDays = totalfood/N+1;
}
if(M>N || minBuyDays>Buyfood)
{
[Link](minBuyDays);
}
}
}
Sample Input:
N = 5
M = 10
S = 8
Sample Output:
16
Note: This program only prints an answer
when (M > N) or (minBuyDays > BuyFood)
is true. If neither condition holds,
it produces no output at all - this is
a logic gap worth fixing before an exam.
Number Programs
Check Armstrong Number
File: [Link]
Code:
import [Link].*;
class Armstrong
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
int n = [Link]();
int digit = [Link](n).length();
int temp = n;
int sum = 0;
while(temp!=0)
{
int d = temp%10;
sum = sum + (int)[Link](d, digit);
temp = temp/10;
}
if(sum == n)
{
[Link](sum);
}
else{
[Link]("not");
}
}
}
Sample Input:
n = 153
Sample Output:
153
(153 is an Armstrong number since
1^3 + 5^3 + 3^3 = 153.
For a non-Armstrong input like 123,
the program prints "not".)
Count Number of Digits
File: [Link]
Code:
import [Link];
public class Count {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int count = 0;
// Special case: if number is 0, it has 1 digit
if (n == 0) {
count = 1;
} else {
while (n != 0) {
count++; // increase digit count
n = n / 10; // remove last digit
}
}
[Link](count);
}
}
Sample Input:
n = 12345
Sample Output:
5
Check Palindrome Number
File: [Link]
Code:
import [Link].*;
class Palindrome
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
int n = [Link]();
int temp = n;
int rev = 0;
while(temp!=0)
{
rev = rev*10 + temp%10;
temp = temp/10;
}
if(rev==n)
{
[Link]("palindrome");
}
else
{
[Link]("not");
}
}
}
Sample Input:
n = 121
Sample Output:
palindrome
Check Prime Number
File: [Link]
Code:
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import [Link].*;
class PrimeOrNot
{
static boolean isPrime(int n)
{
if(n<2) return false;
for(int i=2; i*i<=n; i++)
{
if(n%i == 0) return false;
}
return true;
}
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
int n = [Link]();
if(isPrime(n))
{
[Link]("prime");
}
else
{
[Link]("not");
}
}}
Sample Input:
n = 17
Sample Output:
prime
Reverse a Number
File: [Link]
Code:
import [Link];
public class Reverse {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int rev = 0;
// Loop until n becomes 0
while (n != 0) {
int digit = n % 10; // get last digit
rev = rev * 10 + digit; // add digit to reversed number
n = n / 10; // remove last digit
}
[Link](rev);
}
}
Sample Input:
n = 1234
Sample Output:
4321
Sum of Digits of a Number
File: [Link]
Code:
import [Link];
public class SumDigit{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int n = [Link]();
int sum = 0;
// Loop until n becomes 0
while (n != 0) {
int digit = n % 10; // get last digit
sum = sum + digit; // add digit to sum
n = n / 10; // remove last digit
}
[Link](sum);
}
}
Sample Input:
n = 12345
Sample Output:
15
String Programs
Check Palindrome String (ignores case/punctuation)
File: [Link]
Code:
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import [Link].*;
class PalindromeString {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String s=[Link]();
String clean = [Link]("[^a-zA-z0-9]","").toLowerCase();
String rev = new StringBuilder(clean).reverse().toString();
if([Link](rev))
{
[Link]("palindrome");
}
else
{
[Link]("not");
}
}
}
Sample Input:
Input line:
"A man a plan a canal Panama"
Sample Output:
palindrome
Reverse Order of Words in a Sentence
File: [Link]
Code:
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
import [Link].*;
class StringReverse
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
String s=[Link]();
String word[] = [Link]().split("\\s+");
for(int i=[Link]-1; i>=0; i--)
{
[Link](word[i]);
if(i!=0)
{
[Link](" ");
}
}
}}
Sample Input:
Input line:
"Hello World Java"
Sample Output:
Java
World
Hello
Other Programs
Convert 12-Hour Time Format to 24-Hour Format
File: [Link]
Code:
import [Link].*;
public class Train {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String s = [Link]();
int hour = [Link]([Link](0, 2));
String type = [Link](8);
if([Link]("AM")){
if(hour == 12){
[Link]("00");
}
else{
[Link]("%02d", hour);
}
}
else{
if(hour != 12){
hour = hour + 12;
}
[Link](hour);
[Link]([Link](2, 8));
}
}
Sample Input:
Input:
"07:45:00AM"
Sample Output:
07:45:00
(For a PM example like "02:30:00PM",
the output would be "14:30:00".)