Basic Simple Java Programs: Java is a popular programming language used by a large number of developers worldwide. Many tech giants actively recruit Java developers for many roles like Software developers, Software engineers, etc. To get opportunities as a Java developer candidates must start solving basic simple Java programs to build their concept and solve real-time problems.
Go through this article to get some basic simple Java problems covered. Start your career as a Java Developer with our Decode Java with DSA Course with industry-relevant projects, certificates, and 100% placement assistance.
What is Java Programming?
Sun Microsystems (now Oracle Corporation) created Java, a popular high-level, general-purpose programming language, in 1995. It was designed to be platform-independent, meaning that Java programs can run on any device or operating system that has a Java Virtual Machine (JVM) installed.
KEY TAKEAWAYS:
- Java’s Popularity: Java remains a favored programming language globally, with tech giants actively seeking Java developers for various roles like software developers and engineers.
- Importance of Basic Java Skills: To seize opportunities as a Java developer, candidates must adeptly solve basic Java programs to solidify their understanding and tackle real-world challenges effectively.
- Career Development with Java: Starting a career in Java development is facilitated by mastering basic Java programming skills, which can lead to advanced training opportunities like the Decode Java with DSA Course, offering industry-relevant projects, certifications, and 100% placement assistance.
Features of Basic Java Program Code
Java is a versatile and powerful programming language that is used in a wide range of applications, including web development, mobile app development (Android), desktop applications, and enterprise software. Java programming is loaded with several features. A few features of Java programming are mentioned below:
- Java is an object-oriented programming (OOP) language, which means it focuses on creating objects that contain both data and methods. This approach promotes modularity, flexibility, and reusability of code.
- Java programs are compiled into an intermediate bytecode format, which can be executed on any device or operating system that has a Java Virtual Machine (JVM) installed.
- Java comes with a comprehensive standard library, known as the Java API (Application Programming Interface), which provides a wide range of pre-built classes and methods for common tasks such as I/O, networking, data manipulation, and more.
- Java has a large and active community of developers, which means there are abundant resources, libraries, frameworks, and tools available to help developers build and maintain Java applications.
- Java is widely used in enterprise environments for developing server-side applications, web applications, and large-scale systems. Popular frameworks like Spring and Hibernate are commonly used in Java enterprise development.
Some Basic Java Program Codes
There are beginner-level programming questions that are frequently asked in the interviews to assess the basic understanding of Java. Candidates must be familiar with these basic Java program codes before the interview.
1. Write a Simple Java Program to print Hello World:
Ans: The output screen will display “Hello, World”.
Simple Java Programs |
public class HelloWorld {
public static void main(String[] args) { System.out.println(“Hello, world!”); } } |
2. Write a Simple Java Program to add two numbers:
Simple Java Programs |
public class AddTwoNumbers {
public static void main(String[] args) { int num1 = 5; int num2 = 10; int sum = num1 + num2; System.out.println(“Sum: ” + sum); } } |
- Write a Simple Java Program to determine if a number is even or odd:
Simple Java Programs |
public class EvenOdd {
public static void main(String[] args) { int num = 7; if(num % 2 == 0) { System.out.println(num + ” is even.”); } else { System.out.println(num + ” is odd.”); } } } |
3. Write a Simple Java Program to obtain the factorial of a number:
The factorial of any positive number is the product of all the natural numbers less than or equal to that particular number. The program to obtain the factorial of any number in Java can be done by following code:
Simple Java Programs |
public class Factorial {
public static void main(String[] args) { int n = 5; int factorial = 1; for(int i = 1; i <= n; i++) { factorial *= i; } System.out.println(“Factorial of ” + n + ” is ” + factorial); } } |
4. Write a Simple Java Program to print Fibonacci series
The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding numbers, usually starting from 0 and 1. The Fibonacci series can be obtained in Java using the following code:
Simple Java Programs |
public class Fibonacci {
public static void main(String[] args) { int n = 10; int a = 0; int b = 1; System.out.print(“Fibonacci Series: “); for(int i = 1; i <= n; i++) { System.out.print(a + ” “); int sum = a + b; a = b; b = sum; } } } |
5. Write a Simple Java Program for calculating the area and perimeter of a rectangle:
The perimeter of the rectangle refers to the length of the boundary of the rectangle whereas, the area of the rectangle refers to the space occupied by the rectangle. The program to obtain area and perimeter of the rectangle in Java can be done by the following code:
Simple Java Programs |
public class Rectangle {
public static void main(String[] args) { double length = 5.0; double width = 3.0; double area = length * width; double perimeter = 2 * (length + width); System.out.println(“Area: ” + area); System.out.println(“Perimeter: ” + perimeter); } } |
6. Write a Simple Java Program to calculate the maximum of the two numbers
The two numbers need to be compared, the bigger of the two is termed the maximum of the two numbers. The program to obtain the maximum of the two numbers in Java is as follows:
Simple Java Programs |
public class MaxOfTwoNumbers {
public static void main(String[] args) { int num1 = 10; int num2 = 20; int max = (num1 > num2) ? num1 : num2; System.out.println(“Maximum of ” + num1 + ” and ” + num2 + ” is ” + max); } } |
7. Write a Simple Java Program to reverse a String
Reversal of a string in Java can be performed using the reverse() method of the string as an input and then uses two pointers to swap the characters from the beginning and the end of the array until they meet in the middle. The program to reverse a string in Java can be done as follows:
Simple Java Programs |
public class ReverseString {
public static void main(String[] args) { String str = “Hello, World!”; String reversed = “”; for(int i = str.length() – 1; i >= 0; i–) { reversed += str.charAt(i); } System.out.println(“Reversed string: ” + reversed); } } |
8. Write a Simple Java Program to check if the number is prime:
A prime number is a number that is only divisible by 1 and the number itself. The program to check whether a number is prime or not can be performed by the following code:
Simple Java Programs |
public class PrimeNumber {
public static void main(String[] args) { int num = 17; boolean isPrime = true; if(num <= 1) { isPrime = false; } else { for(int i = 2; i <= Math.sqrt(num); i++) { if(num % i == 0) { isPrime = false; break; } } } if(isPrime) { System.out.println(num + ” is a prime number.”); } else { System.out.println(num + ” is not a prime number.”); } } } |
9. Write a Simple Java program to calculate the Simple Interest:
Simple Interest is the 100th division of the product of the principal, rate, and time. The program to obtain the Simple Interest in Java can be done as follows:
Simple Java Programs |
public class SimpleInterest {
public static void main(String[] args) { double principal = 1000; double rate = 5; double time = 2; double simple_Interest = (principal * rate * time) / 100; System.out.println(“Simple Interest: ” + simple_Interest); } } |
10. Write a Simple Java Program to calculate the LCM of two numbers:
LCM stands for the Least Common Multiple. More than one number is required to obtain the LCM. The program to obtain the LCM of two numbers in Java is as follows:
Simple Java Programs |
public class Main {
public static void main(String[] args) { System.out.print(“Enter the first integer: “); int x = scanner.nextInt(); System.out.print(“Enter the second integer: “); int y = scanner.nextInt(); lcm = (x > y) ? x : y; while(true) { if( lcm % x == 0 && lcm % y == 0 ) { System.out.printf(“The LCM of %d and %d: %d.”, x, y, lcm); break; } ++lcm; } } } |
11. Write a Simple Java Program to swap two numbers using Java:
Swapping of the two numbers can be done using a temporary variable as well as without a temporary variable. The program to swap the two numbers
Simple Java Programs |
public class SwapVariables {
public static void main(String[] args) { int firstNumber = 5; int secondNumber = 10; System.out.println(“Before swapping:”); System.out.println(“First Number: ” + firstNumber); System.out.println(“Second Number: ” + secondNumber); int temp = firstNumber; firstNumber = secondNumber; secondNumber = temp; System.out.println(“\nAfter swapping:”); System.out.println(“First Number: ” + firstNumber); System.out.println(“Second Number: ” + secondNumber); } } |
12. Write a Simple Java Program to check if two arrays are equal or not.
Ans: Two arrays are equal when the number of elements in both arrays are equal.
Basic Simple Java Programs |
class Solution{
//Function to check if two arrays are equal or not. public static boolean check(long A[], long B[], int N) { //Your code here Arrays.sort(A); Arrays.sort(B); for(int i=0;i<N;i++){ if(A[i]==B[i]){ continue; } else{ return false; } } return true; } } |
13. Write a Simple Java Program to find the largest element in a given array.
Ans: You will be given an array arr[] of size N and you need to find the largest element in the given array.
Basic Simple Java Programs |
class Largest {
public int largest(int arr[], int n) { int max = Integer.MIN_VALUE; for(int i=0;i<arr.length;i++) max = Math.max(max,arr[i]); return max; } } |
14. Write a Simple Java Program to check if N is the power of 2
Ans: To check whether the given number N is a power of 2 or N can be expressed in the form of 2X.
Basic Simple Java Programs |
class Solution{
// Function to check if given number n is a power of two. public static boolean isPowerofTwo(long n){ long temp=n; if(n ==1){ return true; } if(n == 0){ return false; } // Your code here while(temp>1){ if(temp % 2 !=0){ return false; } temp /= 2; } return true; } } |
15. Write a Simple Java Program to count nodes of a Linked List
Ans: To count the total number of nodes in a linked list below.
Basic Simple Java Programs |
class Solution { public static int getCount(Node head) { if(head==null) return 0; int count=0; Node temp = head; while(temp!=null){ Count++; temp=temp.next; } return count; } } |
Basic Java Programming: Importance of Optimised Code
The importance of writing optimised code in Java programming cannot be overstated. Optimised code brings several benefits, including:
- Improved Performance: Optimised code typically executes faster and consumes fewer system resources. This is critical, especially in performance-sensitive applications such as enterprise systems, real-time applications, and large-scale web applications, where every millisecond counts.
- Scalability: Optimised code is often more scalable, meaning it can handle increasing workloads efficiently without experiencing a significant degradation in performance. This is essential for applications that need to accommodate growing user bases or increasing data volumes.
- Cost Efficiency: Optimized code can help reduce operational costs by requiring fewer computing resources to run efficiently. This is particularly important for cloud-based applications, where resource consumption directly affects operational expenses.
- Maintainability: Well-optimized code tends to be cleaner, more organized, and easier to understand, making it simpler to debug and maintain. This is especially important for long-term projects where code readability and maintainability are critical for ongoing development and support.
Basic Java Program Code FAQs
How do I learn Java Programming?
Java Programming can be learned from different courses that are available online on YouTube, as well as a lot of paid courses. A detailed analysis of Java programming and its characteristics are mentioned above in the article.
What are the features of Java?
Java is an object-oriented programming language that comes with a comprehensive standard library. Java programming language is a platform-independent computer language. A detailed overview of the features of Java is mentioned above in the article.
Is Java easy for beginners?
Java is a versatile high-level language that can be learned through practice and proper learning. Students need to be patient and consistent to learn any programming language.