Top 30+ Simple Java Programs Examples With Output

Java programing

This post include list of top 30+ Simple Java Programs Examples on different topics such as Array, String, Sequence,Pattern etc for freshers along with its Output to prepare for java interview and explanation.It consist of various ways To perform java programs for practice.To download java programs PDF  go to download section or Click here.

This post is generally written for fresher to get basic idea and to increase mental thinking capacity and makes,positive towards a big code.Coding is nothing,just a logical game. If you can imagine, you can crack it.

Java Programs Based on Numeric Value

In this section, we will see basic programs which we should know to code and also to implement.These programs are mostly asked in interviews or aptitude programming round. These are in sense more logical. And familiar to every coder.

Practicing such programs gives us analytical concepts and an ability to deal with big business scenarios.Here we will see different programs.

1. Write a program to check whether a number is even or odd.

The below code show how to write Java Program to Find Odd or Even number.We use modulus operator to find remainder in our program.if reminder of number is zero then that number is even other wise odd.

import java.lang.*;
import java.util.*;
import java.math.*;

class evenodd {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("\n Enter a number");
        int n = sc.nextInt();
        if (n % 2 == 0) {
            System.out.println("\n " + n + " is even");
        } else {
            System.out.println("\n " + n + " is odd");
        }
    }
}

Output:

Enter a number
11
11 is odd

2. Write a program to print reverse of a number.

import java.io.*;
import java.lang.*;
import java.util.*;

class reversenumber {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("\n Enter a number ");
        int n = sc.nextInt();
        int value = n;
        int rev = 0;
        while (n > 0) {
            int d = n % 10;
            rev = (rev * 10) + d;
            n = n / 10;
        }
        System.out.println("\n The reverse of " + value + " is " + rev);
    }
}

Output:

Enter a number 
542
The reverse of 542 is 245

3. Write a program to print factorial of a number.

import java.io.*;
import java.lang.*;
import java.util.*;
import java.math.*;

class factorial {
 public static void main(String args[]) {
  Scanner sc = new Scanner(System.in);
  System.out.println("\n Enter a number");
  int n = sc.nextInt();
  BigInteger fact = BigInteger.ONE;
  for (int i = 1; i <= n; i++) {
   fact = fact.multiply(BigInteger.valueOf(i));
  }
  System.out.println("\n Factorial of " + n + " is " + fact);
 }
}

Output:

Enter a number
15
Factorial of 15 is 1307674368000

4. Write a program to check whether a number is prime or not.

import java.io.*;
import java.lang.*;
import java.util.*;

class prime {
 public static void main(String args[]) {
  Scanner sc = new Scanner(System.in);
  System.out.println("\n Enter a number");
  int n = sc.nextInt();
  int flag = 0;
  for (int i = 2; i < n; i++) {
   if (n % i == 0) {
    flag = 0;
    break;
   } else {
    flag = 1;
   }
  }
  if (flag == 0) {
   System.out.println("\n " + n + " is not prime number");
  } else {
   System.out.println("\n " + n + " is a prime number");
  }
 }
}

Output:

Enter a number
53
53 is a prime number

5. Write a program to check the given number is either palindrom or not.

import java.io.*;
import java.lang.*;
import java.util.*;

class numberpalidrome {
 public static void main(String args[]) {
  Scanner sc = new Scanner(System.in);
  System.out.println("\n Enter a number ");
  int n = sc.nextInt();
  int value = n;
  int rev = 0;
  while (n > 0) {
   int d = n % 10;
   rev = (rev * 10) + d;
   n = n / 10;
  }
  if (rev == value) {
   System.out.println("\n " + value + " is a palindrome ");
  } else {
   System.out.println("\n " + value + " is not a palindrome ");
  }
 }
}

Output:

Enter a number 
252
252 is a palindrome 

6. Write a program for x raise y.

import java.io.*;
import java.lang.*;
import java.util.*;

class xraisey {
 public static void main(String args[]) {
  Scanner sc = new Scanner(System.in);
  System.out.println("\n Enter x");
  int x = sc.nextInt();
  System.out.println("\n Enter y");
  int y = sc.nextInt();
  int value = 1;
  for (int i = 0; i < y; i++) {
   value = value * x;
  }
  System.out.println("\n x^y = " + x + "^" + y + " is " + value);
 }
}

Output:

Enter x
5
Enter y
2
x^y = 5^2 is 25

7. Write a program to swap two numbers.

import java.lang.*;
import java.util.*;

class swap {
 public static void main(String args[]) {
  Scanner sc = new Scanner(System.in);
  System.out.println("\n Enter x");
  int x = sc.nextInt();
  System.out.println("\n Enter y");
  int y = sc.nextInt();
  System.out.println("\n Before swapping x=" + x + " and y=" + y);
  int t = x;
  x = y;
  y = t;
  System.out.println("\n After swapping x=" + x + " and y=" + y);
 }
}

Output:

Enter x
25
Enter y
24
Before swapping x=25 and y=24
After swapping x=24 and y=25

8. Write a program to calculate a power of 2 without using any function.

import java.io.*;
import java.util.*;

class poweroftwo {
 public static void main(String args[]) {
  int number, i, answer = 2;
  Scanner sc = new Scanner(System.in);
  System.out.println("\n Enter the number:");
  number = sc.nextInt();

  for (i = 1; i < number; i++) {
   answer = 2 * answer;

  }

  System.out.println("Value of given power is  " + answer);
 }
}

Output:

Enter the number:
4
Value of given power is  16

9. Write a program to find LCM of number.

import java.io.*;
import java.util.*;
import java.lang.*;

class lcm {
 public static void main(String args[])

 {
  Scanner sc = new Scanner(System.in);

  int lcm = 1, a, b;
  System.out.print("\n Enter the 1st number : ");
  a = sc.nextInt();
  System.out.print("\n Enter the 2nd number : ");
  b = sc.nextInt();

  for (int i = a; i <= a * b; i++) {
   if (i % a == 0 && i % b == 0) {
    lcm = i;
    break;
   }
  }
  System.out.println("\n  L.C.M.=" + lcm);
 }
}

Output:

Enter the 1st number : 5
Enter the 2nd number : 2
L.C.M.=10

10. Write a program to swap two numbers without using third variable.

import java.io.*;
import java.lang.*;
import java.util.*;

class swaplogic {
 public static void main(String args[]) {
  Scanner sc = new Scanner(System.in);
  System.out.println("\n Enter x");
  int x = sc.nextInt();
  System.out.println("\n Enter y");
  int y = sc.nextInt();
  System.out.println("\n Before swapping x=" + x + " and y=" + y);
  x = x + y;
  y = x - y;
  x = x - y;
  System.out.println("\n After swapping x=" + x + " and y=" + y);
 }
}

Output:

Enter x
98
Enter y
56
Before swapping x=98 and y=56
After swapping x=56 and y=98

11.Write a program to print 1 to n without using loop.

 import java.lang.*;
 import java.util.*;

 class printloop {
  public static void main(String args[]) {
   Scanner sc = new Scanner(System.in);
   System.out.println("\n Print numbers till ?");
   int n = sc.nextInt();
   System.out.println("\n");
   print(n);
  }
  public static void print(int n) {
   if (n >= 1) {
    print(n - 1);
    System.out.println(n);
   }
  }
 }

Output:

Print numbers till ?
11

1
2
3
4
5
6
7
8
9
10
11

Leave a Reply

Your email address will not be published. Required fields are marked *