Java Programs: Know the Best Java Programs for Beginners


Java is a popular programming language and is one of the top programming languages, ruling since 1995. Java programs are essential for students as they help aspirants understand how to put the logic in a code.

As a fresher, or a beginner, it can seem a little bit intimidating to start learning Java. However, once you go over the basics, it is smooth sailing after that. We're here to help you with exactly that! Let's first get to know what exactly is Java and then find the best Java programs for beginners.

What is Java?

Java is a general-purpose, high-level, object-oriented programming language that allows programmers to write once and read anywhere (WORA). It means the compiled Java code can run on any platform without performing any recompilation.

James Gosling created Java in May 1995 under Sun Microsystems. Aspirants and beginners can start with a simple Java program.

Basic Java Program Examples

1. Calculate the factorial of a number

This is a factorial program where the number gets calculated by taking the product of all the positive numbers that are less than or equal to that number. “number!” is used to denote it.

import java.util.Scanner;
public class factorials {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);   // accepts input from keyboard
System.out.println("Enter the number:");
int numb = scanner.nextInt();
//Called the user defined function fact
int facto = factFunc(numb);
System.out.println("The Factorial of your given number is: "+ facto);
}
static int factFunc(int no)
{
int res;
if(no == 1){
return 1;
}
res = factFunc(no - 1)* no;
return res;
}
}

2. Check whether the given string is palindrome or not

Palindrome words and numbers are a sequence where each character gets placed in such a way that from the center, it mirrors the same on both sides.

import java.util.Scanner;
class PalindromeChecker
{
   public static void main(String args[])
   {
  	String strr, revrs = "";
  	Scanner sc = new Scanner(System.in);
  	System.out.println("Enter a string:");
  	strr = sc.nextLine();
  	int len = strr.length();
  	for ( int i = len - 1; i >= 0; i-- )
     	revrs = revrs + strr.charAt(i);
  	if (strr.equals(revrs))
     	System.out.println(strr + " is a palindrome");
  	else
     	System.out.println(strr + " is not a palindrome");
   }
}

3. Print the Fibonacci series

It is a number series where the next term gets summed up with its preceding two terms. For Example: 0 1 1 2 3 5 8 13 and so on....

class Fibo { 
public static void main(String args[]) 
{	
 int no1 = 0, no2 = 1, tot, i, count = 15;  
 // count says, till howmuch we want to show our series
 System.out.print(no1 + " " + no2);
 //printing 0 and 1 as the default two initial value to start the series	
 for(i=2; i<count; ++i)
 // the iteration starts from 2 because 0 and 1 have already been defined
 {	
  tot = no1 + no2;	
  System.out.print(" " + tot);   
  no1 = no2;	
  no2 = tot;	
 }	
}
}

4. Display the diamond pattern in Java

This program uses * to display the diamond shape through the Java program.

import java.util.Scanner;
public class diamondPrint
{
public static void main(String args[])
{
int no, space = 1, g, s;
System.out.print(" Please enter the number of rows: ");
Scanner sc = new Scanner(System.in);
no = sc.nextInt();
space = no - 1;
for (s = 1; s<= no; s++)
{
for (g = 1; g<= space; g++)
{
System.out.print(" ");
}
space--;
for (g = 1; g <= 2 * s - 1; g++)
{
System.out.print("*");
}
System.out.println("");
}
space = 1;
for (s = 1; s <= no - 1; s++)
{
for (g = 1; g<= space; g++)
{
System.out.print(" ");
}
space++;
for (g = 1; g<= 2 * (no - s) - 1; g++)
{
System.out.print("*");
}
System.out.println("");
}
}
}

5.  Check whether a number is even or odd

This program checks whether a number is even or odd by checking whether the number, when divided by 2, gives a remainder (% operator) 0 or 1. If any even number gives a remainder 0 in this calculation, the number becomes even, otherwise odd.

import java.util.Scanner;
class EvenOdd
{
  public static void main(String args[])
  {
	int numb;
	System.out.println(" Please enter an Integer number:");
	Scanner sc = new Scanner(System.in);
	numb = sc.nextInt();
	if ( numb % 2 == 0 )
    	System.out.println("The number you have entered is even");
 	else
    	System.out.println("The number you have entered number is odd");
  }
}

6. Check whether a number is positive or negative

This program checks whether a number is positive or negative by checking whether the given number is greater than 0 or not. If it is greater than 0, the number becomes positive, otherwise negative.

import java.util.Scanner;
public class PosNeg 
{ 
public static void main(String[] args)  
{ 
int numb;
	System.out.println(" Please enter an Integer number:");
	Scanner sc = new Scanner(System.in);
	numb = sc.nextInt(); 
// checking whether the number is greater than 0 or not 
if(numb > 0) 
{ 
System.out.println("The number is positive."); 
}  
// checking whether the number is less than 0 or not 
else if(numb < 0) 
{ 
System.out.println("The number is negative."); 
} 
// executing when the above two conditions return false 
else 
{ 
System.out.println("The number is zero."); 
} 
} 
} 

7. Print the matrix addition taking matrix size from the user

This program calculates the addition of two matrices and checks whether the two matrices are compatible with addition.

import java.util.Scanner;
class Main
{
   public static void main(String args[])
   {
  	int g, s, c, d, k, p, q, summatn = 0;
  	Scanner sc = new Scanner(System.in);
  	System.out.println("Enter the number of rows and columns of first matrix");
  	g = sc.nextInt();
  	s = sc.nextInt();
  	int first[][] = new int[g][s];
  	System.out.println("Enter elements of first matrix");
  	for (c = 0; c < g; c++)
     	for (d = 0; d < s; d++)
        	first[c][d] = sc.nextInt();
  	System.out.println("Enter the number of rows and columns of second matrix");
  	p = sc.nextInt();
  	q = sc.nextInt();
  	if (s != p)
     	System.out.println("The matrices can't be multiplied with each other.");
  	else
  	{
     	int second[][] = new int[p][q];
     	int multiply[][] = new int[g][q];
     	System.out.println("Enter elements of second matrix");
     	for (c = 0; c < p; c++)
        	for (d = 0; d < q; d++)
           	second[c][d] = sc.nextInt();
     	for (c = 0; c < g; c++)
     	{
        	for (d = 0; d < q; d++)
        	{ 
           	for (k = 0; k < p; k++)
           	{
              	summatn = summatn + first[c][k]*second[k][d];
           	}
           	multiply[c][d] = summatn;
           	summatn = 0;
        	}
     	}
     	System.out.println("Product of the matrices:");
     	for (c = 0; c < g; c++)
     	{
        	for (d = 0; d < q; d++)
               System.out.print(multiply[c][d]+"\t");
        	System.out.print("\n");
     	}
  	}
   }
}

8. Design the pattern of alphabet A

This program uses the asterisk (*) to print the pattern or design of the alphabet A.

import java.util.Scanner;
public class Main {
void display(int no)
{
	// the outer loop for printing the lines + 1
for (int i = 0; i<=no; i++) {
	// the inner for loop for logic execution
for (int j = 0; j<= no / 2; j++) {
	// printing 2 column lines
if ((j == 0 || j == no / 2) && i != 0 ||
	// printing the first line of alphabet
i == 0  && j != no / 2 ||
	// prints middle line
i == no / 2)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Main obj = new Main();
obj.display(7);
}
}

9. Reverse a string

This Java program helps in reversing the string. Whatever the user provides through the input as a string, each character is displayed in reverse order.

import java.util.Scanner;
public class  ReverseString
{
 public static void main(String[] args)
 {
 System.out.println(" Please enter string to see it in reverse form:");
 Scanner sc = new Scanner(System.in);
 String strg = sc.nextLine();
 StringBuilder sb = new StringBuilder(strg);
 System.out.println("The reversed string is:- ");
 System.out.println(sb.reverse().toString());
 }
}

10. Create a calculator program

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter two numbers: ");
double a = sc.nextDouble();
double b = sc.nextDouble();
System.out.print("Enter an operator (+, -, *, /): ");
char op = sc.next().charAt(0);
double res;
switch(op)
{
case '+':
res = a + b;
break;
case '-':
res = a - b;
break;
case '*':
res = a * b;
break;
case '/':
res = a / b;
break;
default:
System.out.printf("Caution! the given operator is incorrect");
return;
}
System.out.printf("%.1f %c %.1f = %.1f", a, op, b, res);
}
}

Conclusion

As a beginner, it is imperative that you do not get overwhelmed by Java programs. These simple, beginner-friendly Java programs will help you get over the fear of not performing well and would allow you to gain confidence to tackle much more complex programs.

If you're interested in learning one of the most important programming languages, Java, enroll in Board Infinity's Core & Advanced Java course. With this, you'll learn the fundamentals of Core & Advanced Java from leading industry experts and receive a certificate upon successful completion of the course.