Skip to main content

JAVA Basic Programs

JAVA Basic Programs

Creating Hello java example
class Simple{  
    public static void main(String args[]){  
     System.out.println("Hello Java");  
    }  
}  
Output:Hello Java

Understanding first java program

Let's see what is the meaning of class, public, static, void, main, String[], System.out.println().
Ø  class keyword is used to declare a class in java.
Ø  public keyword is an access modifier which represents visibility, it means it is visible to all.
Ø  static is a keyword, if we declare any method as static, it is known as static method. The core advantage of static method is that there is no need to create object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create object to invoke the main method. So it saves memory.
Ø  void is the return type of the method, it means it doesn't return any value.
Ø  main represents startup of the program.
Ø  String[] args is used for command line argument. We will learn it later.
Ø  System.out.println() is used print statement.

 Even Odd Number Example

public class FindEvenOrOddNumber {
public static void main(String[] args) {
  //create an array of 10 numbers
   int[] numbers = new int[]{1,2,3,4,5,6,7,8,9,10};
  for(int i=0; i < numbers.length; i++){
  /*
   * use modulus operator to check if the number is even or odd. 
    * If we divide any number by 2 and reminder is 0 then the number is
    * even, otherwise it is odd.
    */
     if(numbers[i]%2 == 0)
    System.out.println(numbers[i] + " is even number.");
    else
   System.out.println(numbers[i] + " is odd number.");
                         }
                  }
} 

Output of the program would be
1 is odd number.
2 is even number.
3 is odd number.
4 is even number.
5 is odd number.
6 is even number.
7 is odd number.
8 is even number.
9 is odd number.
1    10 is even number.

Factorial Example
public class NumberFactorial {
 public static void main(String[] args) {
 int number = 5;
 / / Factorial of any number is !n For example, factorial of 4 is 4*3*2*1.
 int factorial = number;
 for(int i =(number - 1); i > 1; i--)
  {
      factorial = factorial * i;
  }
  System.out.println("Factorial of a number is " + factorial);
        }
}

Output of the Factorial program would be
Factorial of a number is 120

Swap Numbers Without Using Third Variable Java Example
public class SwapElementsWithoutThirdVariableExample {
 public static void main(String[] args) {
 int num1 = 10;
 int num2 = 20;
 System.out.println("Before Swapping");
 System.out.println("Value of num1 is :" + num1);
 System.out.println("Value of num2 is :" +num2);
 //add both the numbers and assign it to first
  num1 = num1 + num2;
  num2 = num1 - num2;
  num1 = num1 - num2;
  System.out.println("Before Swapping");
  System.out.println("Value of num1 is :" + num1);
  System.out.println("Value of num2 is :" +num2);
        }
 }
 Output
Before Swapping
Value of num1 is :10
Value of num2 is :20
Before Swapping
Value of num1 is :20
Value of num2 is :10

 Print Alphabet
class Alphabets
{
   public static void main(String args[])
   {
  char ch; 
       for( ch = 'a' ; ch <= 'z' ; ch++ )
         System.out.print(ch);
   }
}

 Output
abcdefghijklmnopqrstuvwxz


Program to display the prime numbers from 1 to 100

It will display the prime numbers between 1 and 100.
class PrimeNumbers
{
   public static void main (String[] args)
   {                   
       int i =0;
       int num =0;
       //Empty String
       String  primeNumbers = "";
       for (i = 1; i <= 100; i++)        
       {                              
          int counter=0;      
          for(num =i; num>=1; num--)
              {
             if(i%num==0)
                 {
                        counter = counter + 1;
                 }
              }
              if (counter ==2)
              {
                 //Appended the Prime number to the String
                 primeNumbers = primeNumbers + i + " ";
              }        
       }   
       System.out.println("Prime numbers from 1 to 100 are :");
       System.out.println(primeNumbers);
   }
}

Output:
Prime numbers from 1 to 100 are :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Fahrenheit to Celsius
import java.util.*;
 class FahrenheitToCelsius {
 public static void main(String[] args) {
 float temperatue = 100;
 System.out.println(" temperatue in Fahrenheitis 100");
 temperatue = ((temperatue - 32)*5)/9;
 System.out.println("Temperatue in Celsius = " + temperatue);
  }
}

Output:
 temperatue in Fahrenheit is 100
Temperatue in Celsius =37.77778

prints multiplication table of a number
import java.util.Scanner;

class MultiplicationTable
{
   public static void main(String args[])
   {
      int n=4, c;
      System.out.println("Multiplication table of "+n+" is :-");
      for ( c = 1 ; c <= 10 ; c++ )
         System.out.println(n+"*"+c+" = "+(n*c));
   }
}

Output
Enter an integer to print it's multiplication table 4
Multiplication table of 4  is :-
4*1=4
4*2=8
4*3=12
4*4=16
4*5=20
4*6=24
4*7=28
4*8=32
4*9=36
4*10=40

Finds largest of three numbers 
import java.util.Scanner;
 class LargestOfThreeNumbers
{
   public static void main(String args[])
   {
      int x=5, y=54, z=34;
       if ( x > y && x > z )
         System.out.println("First number is largest.");
      else if ( y > x && y > z )
         System.out.println("Second number is largest.");
      else if ( z > x && z > y )
         System.out.println("Third number is largest.");
      else  
         System.out.println("Entered numbers are not distinct.");
   }
}
Output:

Second number is largest

Comments