In 1991, James Gosling and his team developed Java at Sun Microsystems, Inc. The main reason behind the motivation of James and his team was that languages like C/C++ were developed for specific hardware types and cannot run its program in devices with different specifications. That’s why Java language is also known for the portability.
Java is one of the world’s most important, widely used computer programming language.
Unlike some other computer programming languages whose influence (shape) has waned (declined) with the passage of time, Java’s has grown stronger.

Today, Java is still the first and best choice in developing web-based applications.
Java is a powerful object-oriented programming language.
Java is very similar to C++ but it is specially used for web page designing. The Java programming language is a general purpose language, like C and C++, any kind of problem can be solved in this language.
The byte code interpreter program is used to run the Java program. This program is also called the Java Virtual Machine (JVM). The bytecode interpreter is not dependent on any specific computer. It can be used on any computer.
Java Examples
Example 1: This example covers a program in Java that prints Hello, World. To print Hello, World on the output screen in Java programming, just place the string “Hello, World” inside System.out.println() as shown in the program given below.
1 2 3 4 5 6 7 | public class Main { public static void main(String[] args) { System.out.println("Hello, World"); } } |
Example 2: This program inputs from user and then adds that given two numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.util.Scanner; public class Main { public static void main(String[] args) { int number1, number2, sum; Scanner scan = new Scanner(System.in); System.out.print("Enter the First Number: "); number1 = scan.nextInt(); System.out.print("Enter the Second Number: "); number2 = scan.nextInt(); sum = number1 + number2; System.out.println("\nResult = " +sum); } } |
Example 3: Write a Java program to swap any two given numbers. The number must be received by user at run-time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.Scanner; public class Main { public static void main(String[] args) { int a, b, temp; Scanner s = new Scanner(System.in); System.out.print("Enter the First Number: "); a = s.nextInt(); System.out.print("Enter the Second Number: "); b = s.nextInt(); temp = a; a = b; b = temp; System.out.println("\na = " +a); System.out.println("b = " +b); } } |
Example 4: Write a program in Java to find and print area of a square.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import java.util.Scanner; public class Main { public static void main(String[] args) { float side, area; Scanner s = new Scanner(System.in); System.out.print("Enter the Side Length of Square: "); side = s.nextFloat(); area = 4*side; System.out.println("\nArea = " +area); } } |
Example 5: Write a program in Java to find and print perimeter of a square.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import java.util.Scanner; public class Main { public static void main(String[] args) { float s, perimeter; Scanner scan = new Scanner(System.in); System.out.print("Enter the Side Length of Square: "); s = scan.nextFloat(); perimeter = 4*s; System.out.println("\nPerimeter = " +perimeter); } } |
Example 6: Write a program in Java that calculates area of rectangle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.util.Scanner; public class Main { public static void main(String[] args) { float len, bre, area; Scanner s = new Scanner(System.in); System.out.print("Enter the Length of Rectangle: "); len = s.nextFloat(); System.out.print("Enter the Breadth of Rectangle: "); bre = s.nextFloat(); area = len*bre; System.out.println("\nArea = " +area); } } /* https://www.code4example.com */ |
Example 7: Write a Java program to find and print perimeter of a rectangle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.util.Scanner; public class Main { public static void main(String[] args) { float length, breadth, perimeter; Scanner s = new Scanner(System.in); System.out.print("Enter the Length of Rectangle: "); length = s.nextFloat(); System.out.print("Enter the Breadth of Rectangle: "); breadth = s.nextFloat(); perimeter = (2*length) + (2*breadth); System.out.println("\nPerimeter = " +perimeter); } } |
Example 8: Created basically to print area and perimeter both, of a rectangle, whose length and breadth will get entered by user at run-time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Enter the Length and Breadth of Rectangle: "); float a = s.nextFloat(); float b = s.nextFloat(); float ar = a*b; float pr = 2*(a+b); System.out.println("\nArea = " +ar); System.out.println("Perimeter = " +pr); } } |
Example 9: Write a Java program to calculate area of circle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import java.util.Scanner; public class Main { public static void main(String[] args) { float r, area; Scanner s = new Scanner(System.in); System.out.print("Enter the Radius of Circle: "); r = s.nextFloat(); area = (float)(3.14*r*r); System.out.println("\nArea = " +area); } } |
Example 10: Write a Java program to calculate circumference of a circle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import java.util.Scanner; public class Main { public static void main(String[] args) { float r, circum; Scanner s = new Scanner(System.in); System.out.print("Enter the Radius of Circle: "); r = s.nextFloat(); circum = (float)(2*3.14*r); System.out.println("\nCircumference = " +circum); } } |
Example 11: This is basically the combined version of previous two programs. That is, I’ve combined both the program, so that in a single program, we can see the area and circumference both at one execution of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.util.Scanner; public class Main { public static void main(String[] args) { float r, area, circum; Scanner s = new Scanner(System.in); System.out.print("Enter the Radius of Circle: "); r = s.nextFloat(); area = (float)(3.14*r*r); circum = (float)(2*3.14*r); System.out.println("\nArea = " +area); System.out.println("Circumference = " +circum); } } |
Example 12: Write a Java program to check odd or even number. The number must be received by user at run-time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.Scanner; public class Main { public static void main(String[] args) { int num; Scanner scan = new Scanner(System.in); System.out.print("Enter a Number: "); num = scan.nextInt(); if(num%2==0) { System.out.println("\nIt is an Even Number."); } else { System.out.println("\nIt is an Odd Number."); } } } |
Example 13: Write a Java program to find the largest of three given numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.util.Scanner; public class Main { public static void main(String[] args) { int a, b, c, large; Scanner scan = new Scanner(System.in); System.out.print("Enter the First Number: "); a = scan.nextInt(); System.out.print("Enter the Second Number: "); b = scan.nextInt(); System.out.print("Enter the Third Number: "); c = scan.nextInt(); if(a>b && a>c) large = a; else if(b>a && b>c) large = b; else large = c; System.out.println("\nLargest = " +large); } } /* https://www.code4example.com */ |
Example 14: Write a Java program that checks whether an year is a leap year or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.util.Scanner; public class Main { public static void main(String[] args) { int year; Scanner scan = new Scanner(System.in); System.out.print("Enter the Year: "); year = scan.nextInt(); if(year%4==0 && year%100!=0) System.out.println("\nIt is a Leap Year."); else if(year%400==0) System.out.println("\nIt is a Leap Year."); else System.out.println("\nIt is not a Leap Year."); } } |
Example 15: Write a simple calculator program in Java that performs four basic arithmetic operations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import java.util.Scanner; public class Main { public static void main(String[] args) { float a, b, res; int choice; Scanner scan = new Scanner(System.in); System.out.println("1. Addition"); System.out.println("2. Subtraction"); System.out.println("3. Multiplication"); System.out.println("4. Division"); System.out.print("Enter Your Choice (1-4): "); choice = scan.nextInt(); if(choice>=1 && choice<=4) { System.out.print("\nEnter any Two Number: "); a = scan.nextFloat(); b = scan.nextFloat(); if(choice==1) res = a+b; else if(choice==2) res = a-b; else if(choice==3) res = a*b; else res = a/b; System.out.println("\nResult = " +res); } else System.out.println("\nInvalid Choice!"); } } |
Example 16: Write a Java program to find the price that has to be paid after applying the discount (if any).
Shopping Amount | Discount |
---|---|
<=800 | No discount |
>800 and <=1500 | 10% |
>1500 and <=2500 | 15% |
>2500 and <=5000 | 20% |
>5000 | 30% |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import java.util.Scanner; public class Main { public static void main(String[] args) { float totalCost, costToPaid, discount; Scanner scan = new Scanner(System.in); System.out.print("Enter the Total Amount of Shopping: "); totalCost = scan.nextFloat(); if(totalCost<=800) { costToPaid = totalCost; } else if(totalCost>800 && totalCost<=1500) { discount = (totalCost*10)/100; costToPaid = totalCost - discount; } else if(totalCost>1500 && totalCost<=2500) { discount = (totalCost*15)/100; costToPaid = totalCost - discount; } else if(totalCost>2500 && totalCost<=5000) { discount = (totalCost*20)/100; costToPaid = totalCost - discount; } else { discount = (totalCost*30)/100; costToPaid = totalCost - discount; } System.out.println("\nThe cost to be Paid is: " + costToPaid); } } |
Example 17: This program prints Hello, World 10 number of times, using for loop.
1 2 3 4 5 6 7 8 | public class Main { public static void main(String[] args) { for(int i=0; i<10; i++) System.out.println("Hello, World"); } } |
Example 18: Write a Java program to find the sum of n numbers. The value of n and n numbers must be received by user at run-time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.Scanner; public class Main { public static void main(String[] args) { int n, i, num, sum=0; Scanner scan = new Scanner(System.in); System.out.print("Enter the Value of n: "); n = scan.nextInt(); System.out.print("Enter " +n+ " Numbers: "); for(i=0; i<n; i++) { num = scan.nextInt(); sum = sum + num; } System.out.println("\nSum = " +sum); } } |
Example 19: Write a Java program to find factorial of a number. The number must be received by user at run-time of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.util.Scanner; public class Main { public static void main(String[] args) { int num, i, fact=1; Scanner s = new Scanner(System.in); System.out.print("Enter a Number: "); num = s.nextInt(); for(i=num; i>=1; i--) { fact = fact*i; } System.out.println("\nFactorial Result = " +fact); } } |
Example 20: Write a Java program to reverse a number using for loop. The number to reverse, must be received by user at run-time of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.Scanner; public class Main { public static void main(String[] args) { int num, rem, rev; Scanner s = new Scanner(System.in); System.out.print("Enter a Number: "); num = s.nextInt(); for(rev=0; num!=0; num=num/10) { rem = num%10; rev = (rev*10) + rem; } System.out.println("\nReverse = " +rev); } } |
Example 21: Write a Java program to check prime number using for loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import java.util.Scanner; public class Main { public static void main(String[] args) { int num, i, count=0; Scanner s = new Scanner(System.in); System.out.print("Enter a Number: "); num = s.nextInt(); for(i=2; i<num; i++) { if(num%i == 0) { count++; break; } } if(count==0) System.out.println("\nIt is a Prime Number."); else System.out.println("\nIt is not a Prime Number."); } } |
Example 22: This program prints Hello, World 10 number of times, using while loop, instead of for.
1 2 3 4 5 6 7 8 9 10 11 12 | public class Main { public static void main(String[] args) { int i=0; while(i<10) { System.out.println("Hello, World"); i++; } } } |
Example 23: Write a Java program to check prime number using while loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.util.Scanner; public class Main { public static void main(String[] args) { int num, i=2, count=0; Scanner s = new Scanner(System.in); System.out.print("Enter a Number: "); num = s.nextInt(); while(i<num) { if(num%i == 0) { count++; break; } i++; } if(count==0) System.out.println("\n" +num+ " is a Prime Number."); else System.out.println("\n" +num+ " is not a Prime Number."); } } /* https://www.code4example.com */ |
Example 24: Write a Java program to reverse a number. The number to reverse, must be received by user at run-time of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.Scanner; public class Main { public static void main(String[] args) { int num, rem, rev=0; Scanner s = new Scanner(System.in); System.out.print("Enter a Number: "); num = s.nextInt(); while(num!=0) { rem = num%10; rev = (rev*10) + rem; num = num/10; } System.out.println("\nReverse = " +rev); } } |
Example 25: Write a Java program to find the sum of n numbers using array. The value of n and n numbers must be received by user at run-time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter the Value of n: "); int n = scan.nextInt(); int[] arr = new int[n]; System.out.print("Enter " +n+ " Numbers: "); for(int i=0; i<n; i++) arr[i] = scan.nextInt(); int sum = 0; for(int i=0; i<n; i++) sum += arr[i]; System.out.println("\nSum = " +sum); } } |
Example 26: Write a Java program to reverse a number using array. The number to reverse, must be received by user at run-time of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.util.Scanner; public class Main { public static void main(String[] args) { int num, len, i; Scanner s = new Scanner(System.in); System.out.print("Enter a Number: "); num = s.nextInt(); len = String.valueOf(num).length(); int[] arr = new int[len]; for(i=0; i<len; i++) { arr[i] = num%10; num = num/10; } System.out.print("\nReverse = "); for(i=0; i<len; i++) System.out.print(arr[i]); } } |
Example 27: Write a Java program to find factorial of a number using while loop. The number must be received by user at run-time of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.Scanner; public class Main { public static void main(String[] args) { int fact=1; Scanner s = new Scanner(System.in); System.out.print("Enter a Number: "); int num = s.nextInt(); int i = num; while(i>=1) { fact = fact*i; i--; } System.out.println("\nFactorial of " +num+ " is " +fact); } } |
Example 28: Write a Java program to print multiplication table of 2.
1 2 3 4 5 6 7 8 9 10 11 | public class Main { public static void main(String[] args) { int num=2, i; System.out.println("\n---Multiplication Table of 2---"); for(i=1; i<=10; i++) System.out.println(num*i); } } |
Example 29: Write a Java program to print multiplication table of a number that entered by user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter the Number: "); int num = scan.nextInt(); System.out.println("\n---Multiplication Table of " +num+ "---"); for(int i=1; i<=10; i++) System.out.println(num+ " * " +i+ " = " +(num*i)); } } |
Example 30: Write a simple calculator program in Java that performs four basic arithmetic operations using switch case, instead of if…else.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import java.util.Scanner; public class Main { public static void main(String[] args) { float a, b, res=0; int choice; Scanner scan = new Scanner(System.in); while(true) { System.out.println("1. Addition"); System.out.println("2. Subtraction"); System.out.println("3. Multiplication"); System.out.println("4. Division"); System.out.println("5. Exit"); System.out.print("Enter Your Choice (1-5): "); choice = scan.nextInt(); switch(choice) { case 1: System.out.print("\nEnter any Two Number: "); a = scan.nextFloat(); b = scan.nextFloat(); res = a+b; break; case 2: System.out.print("\nEnter any Two Number: "); a = scan.nextFloat(); b = scan.nextFloat(); res = a-b; break; case 3: System.out.print("\nEnter any Two Number: "); a = scan.nextFloat(); b = scan.nextFloat(); res = a*b; break; case 4: System.out.print("\nEnter any Two Number: "); a = scan.nextFloat(); b = scan.nextFloat(); res = a/b; break; case 5: return; default: System.out.println("\nInvalid choice!"); break; } System.out.println("\nResult = " +res+ "\n"); } } |
Example 31: Write a simple calculator program in Java that performs four basic arithmetic operations using user-defined functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | import java.util.Scanner; public class Main { public static void main(String[] args) { float a, b, res=0; int choice; Scanner scan = new Scanner(System.in); while(true) { init(); choice = scan.nextInt(); switch(choice) { case 1: msg(); a = scan.nextFloat(); b = scan.nextFloat(); res = add(a, b); break; case 2: msg(); a = scan.nextFloat(); b = scan.nextFloat(); res = sub(a, b); break; case 3: msg(); a = scan.nextFloat(); b = scan.nextFloat(); res = mul(a, b); break; case 4: msg(); a = scan.nextFloat(); b = scan.nextFloat(); res = div(a, b); break; case 5: return; default: System.out.println("\nInvalid choice!"); break; } System.out.println("\nResult = " +res+ "\n"); } } public static void init() { System.out.println("1. Addition"); System.out.println("2. Subtraction"); System.out.println("3. Multiplication"); System.out.println("4. Division"); System.out.println("5. Exit"); System.out.print("Enter Your Choice (1-5): "); } public static void msg() { System.out.print("\nEnter any Two Number: "); } public static float add(float x, float y) { return x+y; } public static float sub(float x, float y) { return x-y; } public static float mul(float x, float y) { return x*y; } public static float div(float x, float y) { return x/y; } } |
Example 32: Write a Java program to find and print the arithmetic mean of n numbers, entered by user at run-time of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.util.Scanner; public class Main { public static void main(String[] args) { int n, i, sum=0; float armean; Scanner scan = new Scanner(System.in); System.out.print("How many numbers to enter ? "); n = scan.nextInt(); int[] arr = new int[n]; System.out.print("Enter " +n+ " Numbers: "); for(i=0; i<n; i++) { arr[i] = scan.nextInt(); sum = sum + arr[i]; } armean = sum/n; System.out.println("\nArithmetic Mean = " +armean); } } |
Example 33: Write a Java program to find the sum of digits of a given number using while loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.Scanner; public class Main { public static void main(String[] args) { int num, digit, sum=0; Scanner s = new Scanner(System.in); System.out.print("Enter a Number: "); num = s.nextInt(); while(num!=0) { digit = num%10; sum = sum + digit; num = num/10; } System.out.println("\nSum of Digits = " +sum); } } } |
Example 34: Write a program in Java to find grade of student using if–else.
Average Mark Range | Grade |
---|---|
>= 94 | A |
>= 90 and < 94 | A- |
>= 87 and < 90 | B+ |
>= 83 and < 87 | B |
>= 80 and < 83 | B- |
>= 77 and < 80 | C+ |
>= 73 and < 77 | C |
>= 70 and < 73 | C- |
>= 67 and < 70 | D+ |
>= 63 and < 67 | D |
>= 60 and < 63 | D- |
< 60 | F |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | import java.util.Scanner; public class Main { public static void main(String[] args) { float[] marks = new float[8]; float sum=0, avg; int i; Scanner scan = new Scanner(System.in); System.out.print("Enter Marks Obtained in 8 Subjects: "); for(i=0; i<8; i++) marks[i] = scan.nextFloat(); for(i=0; i<8; i++) sum = sum + marks[i]; avg = sum/8; System.out.print("\nGrade = "); if(avg>=94) System.out.println("A"); else if(avg>=90 && avg<94) System.out.println("A-"); else if(avg>=87 && avg<90) System.out.println("B+"); else if(avg>=83 && avg<87) System.out.println("B"); else if(avg>=80 && avg<83) System.out.println("B-"); else if(avg>=77 && avg<80) System.out.println("C+"); else if(avg>=73 && avg<77) System.out.println("C"); else if(avg>=70 && avg<73) System.out.println("C-"); else if(avg>=67 && avg<70) System.out.println("D+"); else if(avg>=63 && avg<67) System.out.println("D"); else if(avg>=60 && avg<63) System.out.println("D-"); else System.out.println("F"); } } |
Example 35: Write a Java program to print Fibonacci series upto n. The value of n must be received by user at run-time of the program. For example, if user enters 5 as value of n, then the program should print first 5 terms of Fibonacci series.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import java.util.Scanner; public class Main { public static void main(String[] args) { int a=0, b=1, c=0, n; Scanner s = new Scanner(System.in); System.out.print("Enter the value of n: "); n = s.nextInt(); System.out.print("\nFibonacci Series: " +a+ " " +b+ " "); c = a+b; n = n-2; while(n>0) { System.out.print(c+ " "); a = b; b = c; c = a+b; n--; } } } |
Example 36: write a Java program to convert decimal number to binary. The decimal number must be received by user at run-time of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.util.Scanner; public class Main { public static void main(String[] args) { int decimal, i=0; int[] binary = new int[20]; Scanner scan = new Scanner(System.in); System.out.print("Enter the Decimal Number: "); decimal = scan.nextInt(); while(decimal != 0) { binary[i] = decimal%2; i++; decimal = decimal/2; } System.out.print("\nEquivalent Binary Value = "); for(i=(i-1); i>=0; i--) System.out.print(binary[i]); } } |
Example 37: Write a Java program to print Pascal’s triangle.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class Main { public static void main(String[] args) { int row=5, i, j, space, num; for(i=0; i<row; i++) { for(space=row; space>i; space--) { System.out.print(" "); } num=1; for(j=0; j<=i; j++) { System.out.print(num+ " "); num = num*(i-j)/(j+1); } System.out.print("\n"); } } } |
Example 38: Write a Java program to perform linear search.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.util.Scanner; public class Main { public static void main(String[] args) { int i, num, pos=0; int[] arr = new int[10]; Scanner s = new Scanner(System.in); System.out.print("Enter 10 Elements: "); for(i=0; i<10; i++) arr[i] = s.nextInt(); System.out.print("Enter an Element to Search: "); num = s.nextInt(); for(i=0; i<10; i++) { if(num==arr[i]) { pos = i+1; break; } } if(pos==0) System.out.println("\nThe element not found!"); else System.out.println("\nThe element found at position: " +pos); } } |
Example 39: Write a Java program to perform binary search based on 10 elements. All the 10 elements and the element to search using binary search, must be received by user at run-time of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import java.util.Scanner; public class Main { public static void main(String[] args) { int size=10, i, search, first, last, middle; int[] arr = new int[size]; Scanner scan = new Scanner(System.in); System.out.print("Enter 10 Elements (in Ascending): "); for(i=0; i<size; i++) { arr[i] = scan.nextInt(); } System.out.print("Enter an Element to Search: "); search = scan.nextInt(); first = 0; last = size-1; middle = (first+last)/2; while(first<=last) { if(arr[middle]<search) { first = middle+1; } else if(arr[middle]==search) { System.out.println("\nThe element is available at Index No." +middle); break; } else { last = middle-1; } middle = (first+last)/2; } if(first>last) { System.out.println("\nThe element is not available in given array"); } } } |
Example 40: Write a Java program to find and print the largest number in an array of 10 numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import java.util.Scanner; public class Main { public static void main(String[] args) { int i, large; int[] arr = new int[10]; Scanner scan = new Scanner(System.in); System.out.print("Enter 10 Numbers: "); for(i=0; i<10; i++) arr[i] = scan.nextInt(); large = arr[0]; for(i=1; i<10; i++) { if(large<arr[i]) large = arr[i]; } System.out.println("\nLargest Number = " +large); } } |
Example 41: Write a Java program to find the smallest number in an array of n numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.util.Scanner; public class Main { public static void main(String[] args) { int tot, i, small; Scanner scan = new Scanner(System.in); System.out.print("Enter the Size of Array: "); tot = scan.nextInt(); int[] arr = new int[tot]; System.out.print("Enter " +tot+ " Numbers: "); for(i=0; i<tot; i++) arr[i] = scan.nextInt(); small = arr[0]; for(i=1; i<tot; i++) { if(small>arr[i]) small = arr[i]; } System.out.println("\nSmallest Number = " +small); } } |
Example 42: Write a Java program to print the reverse of an array. The array must be received by user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.Scanner; public class Main { public static void main(String[] args) { int i; Scanner scan = new Scanner(System.in); System.out.print("Enter the Size of Array: "); int tot = scan.nextInt(); int[] arr = new int[tot]; System.out.print("Enter " +tot+ " Elements for the Array: "); for(i=0; i<tot; i++) arr[i] = scan.nextInt(); System.out.println("\nReverse of Given Array is: "); for(i=(tot-1); i>=0; i--) System.out.print(arr[i]+ " "); } } |
Example 43: Write a Java program to insert an element in an array at the end.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.Scanner; public class Main { public static void main(String[] args) { int i, element; int[] arr = new int[11]; Scanner scan = new Scanner(System.in); System.out.print("Enter 10 Elements: "); for(i=0; i<10; i++) arr[i] = scan.nextInt(); System.out.print("Enter an Element to Insert: "); element = scan.nextInt(); arr[i] = element; System.out.println("\nNow the new array is: "); for(i=0; i<11; i++) System.out.print(arr[i]+ " "); } } |
Example 44: Write a Java program to remove a given element from a given array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import java.util.Scanner; public class Main { public static void main(String[] args) { int i, j, size=10, element; int[] arr = new int[size]; Scanner scan = new Scanner(System.in); System.out.print("Enter 10 Elements: "); for(i=0; i<size; i++) arr[i] = scan.nextInt(); System.out.print("Enter the Element to Remove: "); element = scan.nextInt(); for(i=0; i<size; i++) { if(element==arr[i]) { for(j=i; j<(size-1); j++) arr[j] = arr[j+1]; System.out.println("\nRemoved the element successfully!"); break; } } System.out.println("\nThe new array is: "); for(i=0; i<(size-1); i++) System.out.print(arr[i]+ " "); } } |
Example 45: Write a Java program to merge two arrays. Both the array must be received by user at run-time of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | int i, k=0; int[] a = new int[5]; int[] b = new int[5]; int[] c = new int[10]; Scanner scan = new Scanner(System.in); System.out.print("Enter 5 Elements for First Array: "); for(i=0; i<5; i++) a[i] = scan.nextInt(); System.out.print("Enter 5 Elements for Second Array: "); for(i=0; i<5; i++) b[i] = scan.nextInt(); // copying the first array to the third array for(i=0; i<5; i++, k++) c[k] = a[i]; // copying the second array to the third array for(i=0; i<5; i++, k++) c[k] = b[i]; System.out.println("\nThe merged array is: "); for(i=0; i<10; i++) System.out.print(c[i]+ " "); |
Example 46: write a Java program to perform addition of two 3*3 matrices. Elements of both matrices must be received by user at run-time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | import java.util.Scanner; public class Main { public static void main(String[] args) { int i, j; int[][] a = new int[3][3]; int[][] b = new int[3][3]; int[][] c = new int[3][3]; Scanner s = new Scanner(System.in); System.out.print("Enter 9 elements for first matrix: "); for(i=0; i<3; i++) { for(j=0; j<3; j++) { a[i][j] = s.nextInt(); } } System.out.print("Enter 9 elements for second matrix: "); for(i=0; i<3; i++) { for(j=0; j<3; j++) { b[i][j] = s.nextInt(); } } for(i=0; i<3; i++) { for(j=0; j<3; j++) { c[i][j] = a[i][j] + b[i][j]; } } System.out.println("\n----Addition Result----"); for(i=0; i<3; i++) { for(j=0; j<3; j++) { System.out.print(c[i][j]+ " "); } System.out.print("\n"); } } } |
Example 47: Write a Java program to subtract two 3*3 matrices. Elements of both matrices must be received by user at run-time of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import java.util.Scanner; public class Main { public static void main(String[] args) { int i, j; int[][] mat1 = new int[3][3]; int[][] mat2 = new int[3][3]; int[][] mat3 = new int[3][3]; Scanner scan = new Scanner(System.in); System.out.print("Enter Matrix 1 Elements: "); for(i=0; i<3; i++) { for(j=0; j<3; j++) { mat1[i][j] = scan.nextInt(); } } System.out.print("Enter Matrix 2 Elements: "); for(i=0; i<3; i++) { for(j=0; j<3; j++) { mat2[i][j] = scan.nextInt(); } } // subtracting matrices for(i=0; i<3; i++) { for(j=0; j<3; j++) { mat3[i][j] = mat1[i][j] - mat2[i][j]; } } System.out.println("\nResult of Matrix 1 - Matrix 2 is:"); for(i=0; i<3; i++) { for(j=0; j<3; j++) { System.out.print(mat3[i][j]+ " "); } System.out.print("\n"); } } } |
Example 48: Write a Java program to find the transpose of a matrix. Elements of the matrix must be received by user at run-time of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | import java.util.Scanner; public class Main { public static void main(String[] args) { int i, j; int[][] matOrig = new int[3][3]; int[][] matTran = new int[3][3]; Scanner scan = new Scanner(System.in); System.out.print("Enter 9 Elements of the Matrix: "); for(i=0; i<3; i++) { for(j=0; j<3; j++) { matOrig[i][j] = scan.nextInt(); } } System.out.println("\n----Original Matrix----"); for(i=0; i<3; i++) { for(j=0; j<3; j++) { System.out.print(matOrig[i][j]+ "\t"); } System.out.print("\n"); } // copying the transpose of matOrig to matTran for(i=0; i<3; i++) { for(j=0; j<3; j++) { matTran[j][i] = matOrig[i][j]; } } System.out.println("\n----Transpose of Matrix----"); for(i=0; i<3; i++) { for(j=0; j<3; j++) { System.out.print(matTran[i][j]+ "\t"); } System.out.print("\n"); } } } |
Example 49: Write a Java program to find the length of a given string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | String str; int len=0; Scanner scan = new Scanner(System.in); System.out.print("Enter the String: "); str = scan.nextLine(); char[] strChars = str.toCharArray(); for(char ch: strChars) len++; System.out.println("\nLength of String = " +len); |
Example 50: Write a Java program to concatenate or append the second string into the first. Both the string must be received by user at run-time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.util.Scanner; public class Main { public static void main(String[] args) { String a, b; Scanner scan = new Scanner(System.in); System.out.print("Enter the First String: "); a = scan.nextLine(); System.out.print("Enter the Second String: "); b = scan.nextLine(); a = a.concat(b); System.out.println("\nFirst string after concatenation: " +a); } } |