Basic Java Programs

Java program to reverse a number


Following program prints reverse of a given number, i.e., if input is 268 then the output will be 862.

Source code :

import java.util.Scanner;

class Rev
{
   public static void main(String arg[])
   {
      int x, rev = 0;

      System.out.println("Enter a number to reverse");
      Scanner sc = new Scanner(System.in);
      x = sc.nextInt();

      while(x != 0)
      {
          rev = rev * 10;
          rev = rev + x%10;
          x = x/10;
      }

      System.out.println("Reverse of the number is = " + rev);
   }

}


Java program to print prime numbers:


This program prints prime numbers. And also Remember that smallest prime number is 2.

Source code:

import java.util.*;

class PrimeNum
{
   public static void main(String args[])
   {
      int n, status = 1, num = 3;

      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of prime numbers you want");
      n = in.nextInt();

      if (n >= 1)
      {
         System.out.println("First "+n+" prime numbers are :-");
         System.out.println(2);
      }

      for ( int count = 2 ; count <=n ;  )
      {
         for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
         {
            if ( num%j == 0 )
            {
               status = 0;
               break;
            }
         }
         if ( status != 0 )
         {
            System.out.println(num);
            count++;
         }
         status = 1;
         num++;
      }         
   }

}

Java program to find odd or even


Following java program finds a number is odd or even. If the number is divisible by two then it will be even, otherwise, it is odd. We use the modulus operator to find remainder in our program.

Java programming source code
import java.util.Scanner;

class OddOrEven
{
   public static void main(String args[])
   {
      int x;
      System.out.println("Enter an integer to check if it is odd or even ");
      Scanner in = new Scanner(System.in);
      x = in.nextInt();

      if ( x % 2 == 0 )
         System.out.println("You entered an even number.");
      else
         System.out.println("You entered an odd number.");
   }
}

How to take input from a user in Java

Get input from a user: we are using Scanner class to get input from the user. This program asks the user to enter an integer, a float, and a string; then they are printed on the screen. Scanner class is present in java.util package so we import this package into our program. We first create an object of Scanner class and then we use the methods of Scanner class. 
Consider the statement:

 Scanner a = new Scanner(System.in);

Here Scanner is the class name, a is the name of the object, new keyword is used to allocate the memory and System.in is the input stream. Following methods of Scanner class are used in the program:

1) nextInt to input an integer
2) nextFloat to input a float
3) nextLine to input a string

Program:

import java.util.Scanner;

class GetInputFromUser
{
   public static void main(String args[])
   {
      int a;
      float b;
      String s;

      Scanner in = new Scanner(System.in);

      System.out.println("Enter an integer");
      a = in.nextInt();
      System.out.println("You entered integer "+a);

      System.out.println("Enter a float");
      b = in.nextFloat();
      System.out.println("You entered float "+b);   

      System.out.println("Enter a string");
      s = in.nextLine();
      System.out.println("You entered string "+s);
   }

}

Share this

Related Posts

Previous
Next Post »