100 Common Java Programs
100 Common Java Programs
import java.util.*;
class FahrenheitToCelsius
{
public static void main(String[] args)
{
float temperatue;
Scanner in = new Scanner(System.in);
System.out.println("Enter Fahrenheit: ");
temperatue = in.nextInt();
temperatue = ((temperatue - 32)*5)/9;
System.out.println("Temperatue in Celsius = "
+ temperatue);
}
}
Output:
Enter Fahrenheit:
98
Temperatue in Celsius = 36.666668
2. Largest Of Three Numbers Program
import java.util.Scanner;
class LargestOfThreeNumbers
{
public static void main(String args[])
{
int x, y, z;
System.out.println("Enter three integers ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
if ( x > y && x > z )
System.out.println("First number is largest.");
else if ( y > x && y > z )
System.out.println("Second number is largest.");
else if ( z > x && z > y )
System.out.println("Third number is largest.");
else
System.out.println("Entered numbers are not distinct.");
}
}
Output:
Enter three integers
100
400
200
Second number is the largest.
3. SWAP Two Numbers Program
import java.util.Scanner;
class SwapTwoNumbers
{
public static void main(String args[])
{
int x, y, temp;
System.out.println("Enter x and y");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
System.out.println("Before Swapping\nx = "+x+"\ny = "+y);
temp = x;
x = y;
y = temp;
System.out.println("After Swapping\nx = "+x+"\ny = "+y);
}
Output:
Enter x and y
100
200
Before Swapping
x = 100
y = 200
After Swapping
x = 200
y = 100
4. Program To Pass Or Fail In Exam
import java.util.Scanner;
class PassOrFailInExam
{
public static void main(String[] args)
{
int marksObtained, passingMarks;
char grade;
passingMarks = 35;
Scanner input = new Scanner(System.in);
System.out.println("Input marks scored by you");
marksObtained = input.nextInt();
if (marksObtained >= passingMarks) {
if (marksObtained > 95)
grade = 'A';
else if (marksObtained > 80)
grade = 'B';
else if (marksObtained > 65)
grade = 'C';
else
grade = 'D';
System.out.println("You passed the exam with grade: " + grade);
}
else {
grade = 'F';
System.out.println("You failed with grade: " + grade);
}
}
}
Output:
Input marks scored by you
70
You passed the exam with grade: C
5. Prime Number Program
import java.util.Scanner;
class CheckPrime
{
public static void main(String args[])
{
int i, num, flag = 1;
System.out.print("Enter the number :");
Scanner s = new Scanner(System.in);
num = s.nextInt();
for(i = 2; i < num; i++)
{
if(num % i == 0)
{
flag = 0;
break;
}
}
if(flag == 1)
System.out.println(""+num+" is a prime number.");
else
System.out.println(""+num+" is not a prime number.");
}
}
OutPut:
Enter the number :29
29 is a prime number.
import java.util.Scanner;
class Reverse_Number
{
public static void main(String args[])
{
int x, y, sum = 0;
Scanner s = new Scanner(System.in);
System.out.print("Enter Number: ");
x = s.nextInt();
while(x > 0)
{
y = x % 10;
sum = sum * 10 + y;
x = x / 10;
}
System.out.println("Reverse of a Number is "+sum);
}
}
OutPut:
Enter Number: 674
Reverse of a Number is 476
7. Check Vowel Or Consonant Program
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
class Vowel_Consonant
{
public static void main(String[] args) throws Exception
{
char ch;
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the character you want to check:");
ch = (char) bf.read();
switch(ch)
{
case 'a':
System.out.println("Character "+ch+" is vowel");
break;
case 'e':
System.out.println("Character "+ch+" is vowel");
break;
case 'i':
System.out.println("Character "+ch+" is vowel");
break;
case 'o':
System.out.println("Character "+ch+" is vowel");
break;
case 'u':
System.out.println("Character "+ch+" is vowel");
break;
default:
System.out.println("Character "+ch+" is consonant");
break;
}
}
}
Output:
Enter the character you want to check:y
Character y is consonant
import java.util.Scanner;
class Check_Leap_Year
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.print("Enter any year:");
int year = s.nextInt();
boolean flag = false;
if(year % 400 == 0)
flag = true;
else if (year % 100 == 0)
flag = false;
else if(year % 4 == 0)
flag = true;
else
flag = false;
if(flag)
System.out.println("Year "+year+" is a Leap Year");
else
System.out.println("Year "+year+" is not a Leap Year");
}
}
Output:
Enter any year:2000
Year 2000 is a Leap Year
9. Factorial Program
import java.util.Scanner;
class Factorial
{
public static void main(String[] args)
{
long n, fact = 1;
Scanner s = new Scanner(System.in);
System.out.print("Enter Any Number: ");
n = s.nextInt();
for(int i = 1; i <= n; i++)
{
fact = fact * i;
}
System.out.println("Factorial of "+n+" is "+fact);
}
}
Output:
Enter Any Number: 7
Factorial of 7 is 5040
class BinaryToDecimal
{
public static int getDecimal(int binary){
int decimal = 0;
int n = 0;
while(true){
if(binary == 0){
break;
} else {
int temp = binary%10;
decimal += temp*Math.pow(2, n);
binary = binary/10;
n++;
}
}
return decimal;
}
Output:
Binary :1010 | Decimal : 10
Binary :1010 | Decimal : 31
11. Get IP Address Program
import java.net.InetAddress;
OutPut:
IP Address := 10.8.19.20
import java.util.Scanner;
public class Power
{
public static void main(String args[])
{
int x;
double y;
System.out.print("Enter any number: ");
Scanner s = new Scanner(System.in);
x = s.nextInt();
y = Math.pow(x , 2);
System.out.println("Square of "+x+" is :"+y);
}
}
OutPut:
Enter any number: 16
Square of 16 is :256.0
import java.util.Scanner;
class Simple_Interest
{
public static void main(String args[])
{
float p, r, t;
Scanner s = new Scanner(System.in);
System.out.print("Enter the Principal : ");
p = s.nextFloat();
System.out.print("Enter the Rate of interest : ");
r = s.nextFloat();
System.out.print("Enter the Time period : ");
t = s.nextFloat();
float si;
si = (r * t * p) / 100;
System.out.print("The Simple Interest is : " + si);
}
}
Output:
Enter the Principal : 100
Enter the Rate of interest : 5
Enter the Time period : 2
The Simple Interest is : 10.0
15. Product of 2 Numbers
import java.io.*;
import java.util.*;
class Program
{
static int product(int x, int y)
{
if (x < y)
return product(y, x);
else if (y != 0)
return (x + product(x, y - 1));
else
return 0;
}
// Driver Code
public static void main (String[] args)
{
int x = 25, y = 12;
System.out.println(product(x, y));
}
}
OutPut:
Product Of Two Number Is:
300
16. Area of a circle
//Area = pi * r2
//where r is radius of circle
class Program
{
static final double PI = Math.PI;
static double findArea(int r)
{
return PI * Math.pow(r, 2);
}
public static void main(String[] args)
{
System.out.println("Area is " + findArea(5));
}
}
OutPut:
Area is 78.53981633974483
17. Fibonacci Series
class Fibonacci{
static int n1=0,n2=1,n3=0;
static void printFibonacci(int count){
if(count>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
System.out.print(" "+n3);
printFibonacci(count-1);
}
}
public static void main(String args[]){
int count=10;
System.out.print(n1+" "+n2);//printing 0 and 1
printFibonacci(count-2);
//n-2 because 2 numbers are already printed
}
}
18. Print Ascii Value
import java.util.Scanner;
public class NthPrimeNum
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n to compute the nth prime number: ");
int n = sc.nextInt();
int num=1, count=0, i;
while (count < n)
{
num=num+1;
for (i = 2; i <= num; i++)
{
if (num % i == 0)
{
break;
}
}
if (i == num)
{
count = count+1;
}
}
System.out.println("The " +n +"th prime number is:
" + num);
}
}
import java.util.*;
class PalindromeExample2
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter your Input");
original = in.nextLine();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("palindrome.");
else
System.out.println("Not a palindrome.");
}
}
Output:
Enter your Input 4554
4554
palindrome
22. duplicate elements
import java.util.Scanner;
class HelloWorld {
public static void main(String[] args) {
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter any number:");
n = s.nextInt();
HelloWorld obj = new HelloWorld();
System.out.print("Natural numbers till "+n+" :");
obj.natural(n,1);
}
int natural(int y, int i)
{
if(i <= y)
{
System.out.print(i+" ");
return(natural(y,++i));
}
return 1;
}
}
Output:
Enter any number:10
Natural numbers till 10 :1 2 3 4 5 6 7 8 9 10
24 Extract Digits from a Given Number
import java.util.Scanner;
public class Extract_Digits
{
public static void main(String args[])
{
int n, m, a, i = 1, counter = 0;
Scanner s=new Scanner(System.in);
System.out.print("Enter any number:");
n = s.nextInt();
m = n;
while(n > 0)
{
n = n / 10;
counter++;
}
while(m > 0)
{
a = m % 10;
System.out.println("Digit at position "
+counter+": "+a + " \n");
m = m / 10;
counter--;
}
}
}
Output:
Enter any number:4789
Digit at position 4: 9
Digit at position 3: 8
Digit at position 2: 7
Digit at position 1: 4
import java.util.Scanner;
public class Year_Week_Day
{
public static void main(String args[])
{
int m, year, week, day;
Scanner s = new Scanner(System.in);
System.out.print("Enter days:");
m = s.nextInt();
year = m / 365;
m = m % 365;
System.out.println("No. of years: "+year + " \n");
week = m / 7;
m = m % 7;
System.out.println("No. of weeks :"+week + " \n");
day = m;
System.out.println("No. of days :"+day + " \n");
}
}
Output:
Enter days:789
No. of years: 2
No. of weeks :8
No. of days :3
26 Binary Equivalent of an Integer
import java.util.Scanner;
public class Binary_Rec
{
public static void main(String[] args)
{
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter the number:");
n = s.nextInt();
Binary_Rec obj = new Binary_Rec();
String m = obj.Binary(n);
System.out.println("Answer:"+m);
}
String Binary(int x)
{
if(x > 0)
{
int a = x % 2;
x = x / 2;
return a + "" + Binary(x);
}
return "";
}
}
Output:
Enter the number:45
Answer:101101
27 Linear Search
import java.util.Scanner;
public class LinearSearch {
public static void main(String args[]) {
int array[] = {1, 2, 3, 4, 5};
int search, i;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number to search:");
search = input.nextInt();
for (i = 0; i < array.length; i++) {
if (array[i] == search) {
System.out.println(search + " is present at location " + (i + 1) + ".");
break;
}
}
if (i == array.length) {
System.out.println(search + " is not present in the array.");
}
}
}
Output:
Enter the number to search:
4
4 is present at location 4.
28. Binary Search
class Programs
{
public static int binarySearch(int arr[], int first,
int last, int key)
{
if (last>=first){
int mid = first + (last - first)/2;
if (arr[mid] == key){
return mid;
}
if (arr[mid] > key){
return binarySearch(arr, first, mid-1, key);
}else{
return binarySearch(arr, mid+1, last, key);
}
}
return -1;
}
public static void main(String args[]){
int arr[] = {10,20,30,40,50};
int key = 30;
int last=arr.length-1;
int result = binarySearch(arr,0,last,key);
if (result == -1)
System.out.println("Element is not found!");
else
System.out.println("Element is found at index: "+result);
}
}
Output:
Element is found at index: 2
29 Selection Sort
class HelloWorld {
public static void main(String[] args)
{
int num1 = 100, num2 = 200, temp;
if (num1 == num2)
System.out.println ("Both numbers are Equal\n");
else
{
temp = num1 > num2 ? num1 : num2;
System.out.println (temp + " is the largest");
}
}
}
Output:
200 is the largest
31. Sum of digits of a number
class HelloWorld {
public static void main(String[] args)
{
int num = 132456;
System.out.println ("Sum of digits : "
+ getSumOfDigits(num));
}
static int getSumOfDigits(int num)
{
if (num == 0)
return 0;
return (num % 10) + getSumOfDigits(num / 10);
}
}
Output:
Sum of digits : 21
class HelloWorld {
public static void main(String[] args)
{
int num = 1234, reverse = 0;
System.out.println ("Reversed Number is : "
+ getReverse(num, reverse));
}
class HelloWorld {
public static void main(String[] args)
{
int num = 153, reverse = 0; //1634
int len = order(num);
import java.util.Scanner;
public class Program
{
public static void main(String args[] )
{
int balance = 10000, withdraw, deposit;
Scanner s = new Scanner(System.in);
while(true)
{
System.out.println("=======ATM=======");
System.out.println("Choose 1 for Withdraw");
System.out.println("Choose 2 for Deposit");
System.out.println("Choose 3 for Check Balance");
System.out.println("Choose 4 for EXIT");
System.out.print("Enter your Choice :");
int n = s.nextInt();
switch(n)
{
case 1:
System.out.print("Enter money to be withdrawn:");
withdraw = s.nextInt();
if(balance >= withdraw)
{
balance = balance - withdraw;
System.out.println("Please collect your money");
}
else
{
System.out.println("Insufficient Balance");
}
System.out.println("");
break;
case 2:
System.out.print("Enter money to be deposited:");
deposit = s.nextInt();
balance = balance + deposit;
System.out.println("Your Money has been successfully depsited");
System.out.println("");
break;
case 3:
System.out.println("Balance : "+balance);
System.out.println("");
break;
case 4:
System.exit(0);
}
}
}
}
Output:
=======ATM=======
Choose 1 for Withdraw
Choose 2 for Deposit
Choose 3 for Check Balance
Choose 4 for EXIT
Enter your Choice :1
Enter money to be withdrawn:5000
Please collect your money
=======ATM=======
Choose 1 for Withdraw
Choose 2 for Deposit
Choose 3 for Check Balance
Choose 4 for EXIT
Enter your Choice :2
Enter money to be deposited:20000
Your Money has been successfully deposited
35. Find GCD of Two Numbers
class HelloWorld {
public static void main(String[] args)
{
int Num1=24, Num2=8, Temp, GCD=0;
while(Num2 != 0)
{
Temp = Num2;
Num2 = Num1 % Num2;
Num1 = Temp;
}
GCD = Num1;
System.out.println("\n GCD = " + GCD);
}
}
Output:
GCD = 4
class RotateRight
{
public static void main(String[] args)
{
int [] arr = new int [] {1, 2, 3, 4, 5};
int n = 2; //No of Position to rotate
System.out.println("Original array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
for(int i = 0; i < n; i++){
int j, last;
last = arr[arr.length-1];
for(j = arr.length-1; j > 0; j--){
arr[j] = arr[j-1];
}
arr[0] = last;
}
System.out.println();
System.out.println("Array after right rotation: ");
for(int i = 0; i< arr.length; i++){
System.out.print(arr[i] + " ");
}
}
}
Output:
Original array:
12345
Array after right rotation:
45123
Output:
1 -- 2
2 -- 3
8 -- 1
3 -- 1
9 -- 1
5 -- 1
38. Find second smallest element in an array
class HelloWorld
{
static int secondSmallest(int arr[], int n)
{
int first = Integer.MAX_VALUE, second = Integer.MAX_VALUE;
}
}
class HelloWorld {
if( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
System.out.println(c + " is an alphabet.");
else
System.out.println(c + " is not an alphabet.");
}
}
System.out.println(" ");
// Lower case
for(c = 'a'; c <= 'z'; ++c)
System.out.print(c + " ");
}
42. Count Number of Digits
while (num != 0) {
// num = num/10
num /= 10;
++count;
}
Output:
Number of digits: 6
Output:
11 13 17 19 23 29 31 37 41 43
47 53 59 61 67 71 73 79 83 89 97
class Main
{
public static void main(String[] args)
{
int low = 100, high = 500;
for(int number = low + 1; number < high; ++number) {
int digits = 0;
int result = 0;
int originalNumber = number;
// number of digits calculation
while (originalNumber != 0) {
originalNumber /= 10;
++digits;
}
originalNumber = number;
// result contains sum of nth power of its digits
while (originalNumber != 0)
{
int remainder = originalNumber % 10;
result += Math.pow(remainder, digits);
originalNumber /= 10;
}
if (result == number) {
System.out.print(number + " ");
}
}
}
}
Output:
153 370 371 407
import java.util.Arrays;
public class Array
{
public static void main(String[] args)
{
int[][] array = {{10, 12}, {13, 14}, {15, 16, 17}};
System.out.println(Arrays.deepToString(array));
}
}
Output:
[[10, 12], [13, 14], [15, 16, 17]]
import java.util.Arrays;
public class Concat
{
public static void main(String[] args)
{
int[] array1 = {10, 11, 12};
int[] array2 = {14,15, 16};
System.out.println(Arrays.toString(result));
}
}
Output:
[10, 11, 12, 14, 15, 16]
import java.util.Arrays;
public class Array
{
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6};
System.out.println(Arrays.toString(array));
}
}
Output:
[1, 2, 3, 4, 5, 6]
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
System.out.println(date);
}
}
Output:
2023-03-25
import java.math.RoundingMode;
import java.text.DecimalFormat;
Output:
1.4529
if(style == style2)
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
if(style.equals(style2))
System.out.println("Equal");
else
System.out.println("Not Equal");
}
}
class Program
{
public static void main(String[] args)
{
// create char variables
char a = '1';
char b = '8';
// convert char variables to int
// Use parseInt()
int num1 = Integer.parseInt(String.valueOf(a));
int num2 = Integer.parseInt(String.valueOf(b));
// print numeric value
System.out.println(num1); // 1
System.out.println(num2); //8
}
}
54. Simple Calculator Program
import java.util.Scanner;
class Program
{
public static void main(String[] args)
{
char operator;
Double number1, number2, result;
Scanner input = new Scanner(System.in);
System.out.println("Choose an operator: +, -, *, or /");
operator = input.next().charAt(0);
System.out.println("Enter first number");
number1 = input.nextDouble();
switch (operator) {
case '+':
result = number1 + number2;
System.out.println(number1 + " + " + number2 + " = " + result);
break;
case '-':
result = number1 - number2;
System.out.println(number1 + " - " + number2 + " = " + result);
break;
case '*':
result = number1 * number2;
System.out.println(number1 + " * " + number2 + " = " + result);
break;
case '/':
result = number1 / number2;
System.out.println(number1 + " / " + number2 + " = " + result);
break;
default:
System.out.println("Invalid operator!");
break;
}
input.close();
}
}
Output:
Choose an operator: +, -, *, or /
Enter first number
1200
Enter second number
400
1200.0 - 400.0 = 800.0
import java.io.*;
import java.util.*;
class Matrix {
public static void printMatrix(int mat[][])
{
// Loop through all rows
for (int[] row : mat)
// converting each row as string
// and then printing in a separate line
System.out.println(Arrays.toString(row));
}
public static void main(String args[])
throws IOException
{
int mat[][] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 } };
print2D(mat);
}
}
System.out.println("value3: ");
String value3 = value1 >= value2 ? "greater" : "lesser";
System.out.print(value3);
}
}
Output:
Value2: 50
value3: greater
Output:
Follow: coding.aryan
60. Find Square root Program
System.out.println(value);
System.out.println(value2);
}
}
Ouput:
4.0
7.0
result = Math.max(100,200);
System.out.println("Max of two number is: " + result);
}
}
Output:
Min of two number is: 100
Max of two number is: 200
import java.util.Random;
public class Program
{
public static void main(String[] args)
{
Random random = new Random();
// Generate 10 random lowercase letters.
for (int i = 0; i < 10; i++)
{
int n = random.nextInt(26);
char value = (char) (n + 97);
// Display our results.
System.out.println(value + "..." +
Integer.toString(n));
}
}
}
OutPut:
q...16
w...22
f...5
w...22
j...9
l...11
j...9
c...2
u...20
f...5
System.out.println(bits0);
System.out.println(bits1);
System.out.println(bitsAll);
}
}
Output:
0
1
31
import java.util.Random;
public class Program
{
public static void main(String[] args)
{
String value = "aryan";
// Loop through all characters in the string.
// ... Use charAt to get the values.
for (int i = 0; i < value.length(); i++)
{
char c = value.charAt(i);
System.out.println(c);
}
}
}
Output:
a
r
y
a
n
65. Java String.join() Method
Output:
Follow...Coding...Aryan
66. Reverse a Sentence Using Recursion
Output:
The reversed sentence: nayrA gnidoC
class Main
{
public static void main(String[] args)
{
// create an object of Long class
Long obj = 45462329L;
System.out.println(a);
}
}
Output:
45462329
class Main {
public static void main(String[] args)
{
// create int variables
int a = 500;
Output:
500
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
// create an object of Scanner
Scanner sc = new Scanner(System.in);
System.out.println("Enter any string");
Output:
Enter any string
Coding. Aryan
Original Str: Coding. Aryan
Final Str: Coding.Aryan
70. Convert Octal to Decimal
import java.util.Scanner;
class Octal_Decimal
{
Scanner scan;
int num;
void getVal()
{
System.out.println("Octal to Decimal");
scan = new Scanner(System.in);
System.out.println("\nEnter the number :");
num = Integer.parseInt(scan.nextLine(), 8);
}
void convert()
{
String decimal = Integer.toString(num);
System.out.println("Decimal Value is : " + decimal);
}
}
class MainClass
{
public static void main(String args[])
{
Octal_Decimal obj = new Octal_Decimal();
obj.getVal();
obj.convert();
}
}
Output:
Octal to Decimal
Enter the number :
10
Decimal Value is : 8
System.out.print(add_Binary(x, y));
}
}
Output:
1110010
import java.util.Arrays;
import java.util.List;
class Program {
Output:
1238439
73. print Even length words
class Program {
public static void printWords(String s)
{
Output:
JavaPrograms
Simplified
74. Find a Sublist in a List
import java.util.*;
Output:
Initial arrlist: [1, 4, 9, 25, 36]
Sublist of arrlist: [4, 9, 25]
import java.util.ArrayList;
import java.util.List;
// Method 2
// Main driver method
public static void main(String[] args)
{
Output:
[Coding, Is]
[Fun, For, Me]
import java.util.*;
Output:
Original List: [Coding, Aryan, Create, Useful, Content]
Final List: [Coding, Content]
import java.util.Collections;
import java.util.List;
import java.io.*;
import java.nio.file.*;
import java.util.stream.Stream;
class Program {
public static void main(String[] args)
throws IOException
{
try (Stream<Path> filepath
= Files.walk(Paths.get("c:\\Demo")))
{
filepath.forEach(System.out::println);
}
catch (IOException e) {
throw new IOException("Directory Not Found!");
}
}
}
Output:
c:\Demo
c:\Demo\article.docx
c:\Demo\test.txt
79. Search for a File in a Directory
return name.startsWith(initials);
}
}
Output:
file.cpp found
import java.io.*;
import java.io.File;
class Program {
public static void main(String[] args)
{
File directory = new File("/home/Test");
if (directory.isDirectory()) {
String arr[] = directory.list();
if (arr.length > 0) {
System.out.println("The directory "
+ directory.getPath()
+ " is not Empty!");
}
else {
System.out.println("The directory "
+ directory.getPath()
+ " is Empty!");
}
}
}
}
import java.io.File;
class Program {
public static void main(String[] args)
{
File directory = new File("/home/Demo");
if (directory.isDirectory()) {
String arr[] = directory.list();
if (arr.length > 0) {
System.out.println("The directory "
+ directory.getPath()
+ " is not Empty!");
}
else {
System.out.println("The directory "
+ directory.getPath()
+ " is Empty!");
}}}}
83. Find common elements in two ArrayLists
Output:
List1: [Hello, Coding, Programming, Developers]
List2: [Hello, Programming, Aryan]
Common elements: [Hello, Programming]
import java.util.*;
Output:
Linked list: [I,R,S]
Updated Linked list: [F,I,R,S,T]
85. Array Copy in Java
Output:
Contents of a[] 2 8 3
Contents of b[] 2 8 3
class Program {
Output:
oCidgnA.yrna
import java.io.File;
public class ChangetoReadOnly {
import java.util.*;
import java.util.*;
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
import java.util.*;
import java.util.*;
import java.util.*;
import java.util.*;
import java.util.*;
import java.util.*;
public class Program
{
public static void main(String args[])
{
int i, j;
int n = 6;
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
// if the sum of (i+j) is even then print 1
if ((i + j) % 2 == 0) {
System.out.print(1 + " ");
}
// otherwise print 0
else {
System.out.print(0 + " ");
}
}
System.out.println();
}
}
}
100. Butterfly Star Pattern
import java.util.*;