Java

Java Code Examples25 min read

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.

Java Code Examples
Java Code Examples

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.

Example 2: This program inputs from user and then adds that given two numbers.

Example 3: Write a Java program to swap any two given numbers. The number must be received by user at run-time.

Example 4: Write a program in Java to find and print area of a square.

Example 5: Write a program in Java to find and print perimeter of a square.

Example 6: Write a program in Java that calculates area of rectangle.

Example 7: Write a Java program to find and print perimeter of a rectangle.

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.

Example 9: Write a Java program to calculate area of circle.

Example 10: Write a Java program to calculate circumference of a circle.

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.

Example 12: Write a Java program to check odd or even number. The number must be received by user at run-time.

Example 13: Write a Java program to find the largest of three given numbers.

Example 14:  Write a Java program that checks whether an year is a leap year or not.

Example 15: Write a simple calculator program in Java that performs four basic arithmetic operations.

Example 16: Write a Java program to find the price that has to be paid after applying the discount (if any). 

Shopping AmountDiscount
<=800No discount
>800 and <=150010%
>1500 and <=250015%
>2500 and <=500020%
>500030%

Example 17: This program prints Hello, World 10 number of times, using for loop.

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.

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.

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.

Example 21: Write a Java program to check prime number using for loop.

Example 22: This program prints Hello, World 10 number of times, using while loop, instead of for.

Example 23: Write a Java program to check prime number using while loop.

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.

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.

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.

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.

Example 28: Write a Java program to print multiplication table of 2

Example 29: Write a Java program to print multiplication table of a number that  entered by user.

Example 30: Write a simple calculator program in Java that performs four basic arithmetic operations using switch case, instead of if…else.

Example 31: Write a simple calculator program in Java that performs four basic arithmetic operations using user-defined functions. 

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.

Example 33: Write a Java program to find the sum of digits of a given number using while loop.

Example 34: Write a program in Java to find grade of student using ifelse.

Average Mark RangeGrade
>= 94A
>= 90 and < 94A-
>= 87 and < 90B+
>= 83 and < 87B
>= 80 and < 83B-
>= 77 and < 80C+
>= 73 and < 77C
>= 70 and < 73C-
>= 67 and < 70D+
>= 63 and < 67D
>= 60 and < 63D-
< 60F

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.

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.

Example 37: Write a Java program to print Pascal’s triangle.

Example 38: Write a Java program to perform linear search.

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.

Example 40: Write a Java program to find and print the largest number in an array of 10 numbers. 

Example 41: Write a Java program to find the smallest number in an array of n numbers.

Example 42: Write a Java program to print the reverse of an array. The array must be received by user. 

Example 43: Write a Java program to insert an element in an array at the end.

Example 44: Write a Java program to remove a given element from a given array. 

Example 45:  Write a Java program to merge two arrays. Both the array must be received by user at run-time of the program.

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.

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.

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. 

Example 49:  Write a Java program to find the length of a given string.

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 Comment

Leave a Comment