Pages

Java Programming


1. Basic Java Program

public class HelloWorld {
      public static void main(String [ ] args) {
            System.out.println("Hello! You are Learning Java Programming.");
      }
}

Output :

Hello! You are Learning Java Programming.

2. Addition of Two Variables

public class Addition {
      public static void main(String [ ] args) {
            int a = 10;
            int b = 10;
            int c = a + b;
            System.out.println("Addition of Two variables : " + c);
      }
}

Output :

Addition of Two variables : 20.

3. Subtraction of Two Variables

public class Subtraction {
      public static void main(String [ ] args) {
            int a = 20;
            int b = 10;
            int c = a - b;
            System.out.println("Subtraction of Two variables : " + c);
      }
}

Output :

Subtraction of Two variables : 10.

4. Multiplication of Two Variables

public class Multiplication {
      public static void main(String [ ] args) {
            int a = 10;
            int b = 10;
            int c = a * b;
            System.out.println("Multiplication of Two variables : " + c);
      }
}

Output :

Multiplication of Two variables : 100.

5. Division of Two Variables

public class Division {
      public static void main(String [ ] args) {
            int a = 20;
            int b = 5;
            int c = a / b;
            System.out.println("Division of Two variables : " + c);
      }
}

Output :

Division of Two variables : 4.

6. Modulus of Two Variables

public class Modulus {
      public static void main(String [ ] args) {
            int a = 20;
            int b = 3;
            int c = a % b;
            System.out.println("Modulus of Two variables : " + c);
      }
}

Output :

Modulus of Two variables : 2.

7. Print Integers

public class PrintIntegers {
      public static void main(String [ ] args) {
            System.out.print("Display the Integers Numbers are : ");
            for(int i = 0; i ≤ 10; i++) {
                  System.out.print(i + " ");
            }
      }
}

Output :

Display the Integers Numbers are : 0 1 2 3 4 5 6 7 8 9 10.

8. Even Numbers

public class EvenNumbers {
      public static void main(String [ ] args) {
            System.out.print("Display the Even Numbers are : ");
            for(int i = 0; i ≤ 10; i++) {
                  if(i%2 == 0) {
                        System.out.print(i + " ");
                  }
            }
      }
}

Output :

Display the Even Numbers are : 0 2 4 6 8 10.

9. Odd Numbers

public class OddNumbers {
      public static void main(String [ ] args) {
            System.out.print("Display the Odd Numbers are : ");
            for(int i = 0; i ≤ 10; i++) {
                  if(i%2 != 0) {
                        System.out.print(i + " ");
                  }
            }
      }
}

Output :

Display the Even Numbers are : 1 3 5 7 9.

10. Alphabets

public class Alphabets {
      public static void main(String [ ] args) {
            char ch;
            System.out.print("Display the Alphabets (A - Z) : ");
            for(ch = 'a'; ch ≤ 'z'; ch++) {
                  System.out.print(ch + " ");
            }
      }
}

Output :

Display the Alphabets (A - Z) : a b c d e f g h i j k l m n o p q r s t u v w x y z.

11. Convert Fahrenheit to Celsius

public class FahrenheitToCelsius {
      public static void main(String [ ] args) {
            float temperatue = 100F;
            temperatue = ((temperatue - 32)*5)/9;
            System.out.print(" Temperatue in Celsius : ");
      }
}

Output :

Temperatue in Celsius : 37.77778

12. Multiplication Table

public class MultiplicationTable {
      public static void main(String [ ] args) {
            int a = 8;
            System.out.println("Display the Multiplication Table : ");
            for(int i = 1; i ≤ 10; i++) {
                  System.out.println(n + " * " + i + " = " + n*i);
            }
      }
}

Output :

Display the Multiplication Table :
8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80


13. Find Odd or Even Numbers

public class EvenOdd {
      public static void main(String [ ] args) {
            int b = 5;
            if(b%2 == 0) {
                  System.out.print(b + " is a Even Number.");
            } else {
                  System.out.print(b + " is a Odd Number.");
            }
      }
}

Output :

5 is a Odd Number.

14. Swapping with Third Variable

public class SwappingTemporary {
      public static void main(String [ ] args) {
            int a = 20;
            int b = 10;
            int temp;
            System.out.println("Before Swapping a = " + a + " b = " + b);
            temp = a;
            a = b;
            b = temp;
            System.out.println("After Swapping \n a = " + a + " \n b = " + b);
      }
}

Output :

Before Swapping a = 20 b = 10
After Swapping a = 10 b = 20

15. Swapping without Third Variable

public class SwappingVariable {
      public static void main(String [ ] args) {
            int a = 20;
            int b = 10;
            System.out.println("Before Swapping a = " + a + " b = " + b);
            a = a + b;
            b = a - b;
            a = a - b;
            System.out.println("After Swapping \n a = " + a + " \n b = " + b);
      }
}

Output :

Before Swapping a = 20 b = 10
After Swapping a = 10 b = 20

16. Finds Largest of Three Numbers

public class Largest {
      public static void main(String [ ] args) {
            int x = 10;
            int y = 9;
            int z = 4;
            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 :

First number is largest.

17. Finds Smallest of Three Numbers

public class Smallest {
      public static void main(String [ ] args) {
            int x = 10;
            int y = 9;
            int z = 4;
            if ( x < y && x < z )
                  System.out.println("First number is smallest.");
            else if ( y < x && y < z )
                  System.out.println("Second number is smallest.");
            else if ( z < x && z < y )
                  System.out.println("Third number is smallest.");
            else
                  System.out.println("Entered numbers are not distinct.");
      }
}

Output :

First number is smallest.

18. Finds Quotient and Remainer of two integers.

public class QuotientRemainer {
      public static void main(String [ ] args) {
            int x = 10;
            int y = 9;
            int rem = x % y;
            System.out.println("Remainer : " + rem);
            int quot = x / y;
            System.out.println("Quotient : " + quot);
      }
}

Output :

Remainer : 1, Quotient : 1

19. Counts Number of Vowels, Consonants, Digits, Tabs and Blank Spaces in a String

public void class StringExample {
      public static void main(String args[]) throws IOException {
            String str = "Kiran, I am Studying 10 Class";
            int vowels = 0, digits = 0, blanks = 0;
            char ch;
            for(int i = 0; i < str.length(); i ++) {
                  ch = str.charAt(i);
                  if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
                        vowels ++;
                  else if(Character.isDigit(ch))
                        digits ++;
                  else if(Character.isWhitespace(ch))
                        blanks ++;
            }
            System.out.println("Vowels : " + vowels);
            System.out.println("Digits : " + digits);
            System.out.println("Blanks : " + blanks);
      }
}

Output :

Vowels : 7, Digits : 2, Blanks : 5

20. Check Vowel (or) Consonant in a String

public class VowelConsonant {
      public static void main(String[] args) throws Exception {
            char n = 'a';
            switch(n) {
                  case 'a':
                  System.out.println("The given character "+ n +" is vowel");
                  break;
                  case 'e':
                  System.out.println("The given character "+ n +" is vowel");
                  break;
                  case 'i':
                  System.out.println("The given character "+ n +" is vowel");
                  break;
                  case 'o':
                  System.out.println("The given character "+ n +" is vowel");
                  break;
                  case 'u':
                  System.out.println("The given character "+ n +" is vowel");
                  break;
                  default:
                  System.out.println("The given character "+ n +" is consonant");
                  break;
            }
      }
}

Output :

The given character a is vowel.

21. Convert Binary To Decimal

public class BinaryToDecimal {
      public int getDecimalFromBinary(int binary) {
            int decimal = 0;
            int power = 0;
            while(true) {
                  if(binary == 0) {
                        break;
                  } else {
                        int tmp = binary%10;
                        decimal += tmp*Math.pow(2, power);
                        binary = binary/10;
                        power++;
                  }
            }
            return decimal;
      }
      public static void main(String a[]) {
            BinaryToDecimal bd = new BinaryToDecimal();
            System.out.println("10010 ===> " + bd.getDecimalFromBinary(10010));
      }
}

Output :

10010 ===> 38.

22. Print Diamond Pattern

public class Diamond {
      public static void main(String args [ ] ) {
            int n, i, j, space = 1;
            n = 5;
            space = n - 1;
            for (j = 1; j ≤ n; j++) {
                  for (i = 1; i ≤ space; i++) {
                        System.out.print(" ");
                  }
                  space--;
                  for (i = 1; i ≤ 2 * j - 1; i++) {
                        System.out.print("*");
                  }
                  System.out.println("");
            }
            space = 1;
            for (j = 1; j≤n - 1; j++) {
                  for (i = 1; i ≤ space; i++) {
                        System.out.print(" ");
                  }
                  space++;
                  for (i = 1; i ≤ 2 * (n - j) - 1; i++) {
                        System.out.print("*");
                  }
                  System.out.println("");
            }
      }
}

Output :

     *
   ***
  *****
 *******
*********
 *******
  *****
   ***
     *

23. Reverse Number Program

public class ReverseNumber {
      public static void main(String args[]) {
            int n = 12345, reverse = 0;
            while( n != 0 ) {
                  reverse = reverse * 10;
                  reverse = reverse + n%10;
                  n = n/10;
            }
            System.out.println("Reverse number is "+reverse);
      }
}

Output :

Reverse number is : 54321

24. Bizz Fizz Program

public class BuzzFizz {
    public static void main(String [] args) {
        int no = 5;
        for(int i = 1; i≤no; i++) {
            if((i % (3*5)) == 0) {
                System.out.println("FizzBuzz");
            } else if ((i % 5) == 0) {
                System.out.println("Buzz");
            } else if ((i % 3) == 0) {
                System.out.println("Fizz");
            } else {
                System.out.println(i);
            }
        }
    }
}

Output :

1
2
Fizz
4
Buzz

25. Count the Range of Prime Numbers

public class CountPrime {
    public static void main(String[] args) {
        int n = 100, value = 0;
        for(int i = 0; i≤n; i++) {
            int count = 0;
            for(int j = 2; j<i; j++) {
                if(i%j == 0) {
                    count++;
                    break;
                }
            }
            if(count == 0) {
                value = value + 1;
            }
        }
        System.out.println("Count the Range of Prime Number : " + value);
    }
}

Output :

Count the Range of Prime Number : 27

26. A integer array is given as input. find the difference between each element. Return the index of the largest element which has the largest difference gap.

Input: {2,3,4,2,3}
Logic: 2 - 3 = 1, 3 - 4 = 1, 4 - 2 = 2, 2 - 3 = 1. 2 is the max diff between 4 and 2,return the index of 4(2)
Output: 2
package com.jaladhi;
public class Third {
      public static int getDiffArray(int[] n1){
            int n2,n3 = 0,n4 = 0,i;
            for(i = 0;i<n1.length-1;i++){
                  n2 = Math.abs(n1[i]-n1[i+1]);
                  if(n2 > n3){
                        n3 = n2;
                        n4 = i+1;
                  }
            }
            return n4;
      }
      public static void main(String[] args) {
            int[] n1 = {2,9,4,7,6};
            System.out.println(getDiffArray(n1));
      }
}

Output :

1

27. Given two integer arrays. merge the common elements into a new array. find the sum of the elements.

Input 1: {1,2,3,4}
Input 2: {3,4,5,6}

Logic : {3,4}
Output : 7

package com.jaladhi;
import java.util.*;
public class Four {
      public static int mergeArray(int a[],int b[]){
            List l1 = new ArrayList();
            List l2 = new ArrayList();
            int i,d = 0;
            for(i = 0;i<a.length;i++)
                  l1.add(a[i]);
            for(i = 0;i<b.length;i++)
                  l2.add(b[i]);
            l1.retainAll(l2);
            for(i = 0;i<l1.size();i++)
                  d += (Integer) l1.get(i);
            return d;
      }
      public static void main(String[] args) {
            int[] a = {1,2,3,4};
            int[] b = {3,4,5,6};
            System.out.println(mergeArray(a,b));
      }
}

Output :

7

28. Given two arrayslist. retrieve the odd position elements form first input and even position elements from second list. Put it into the new arraylist at the same positions from where they are retrieved from. (consider 0th position as even position, and two lists are of same size)

Input 1 :{12,1,32,3}
Input 2 :{0,12,2,23}
Output : {0,1,2,3}
package com.jaladhi;
public class Five {
      public static int[] retrievePosition(int[] a,int[] b){
            int[] c=new int[a.length];
            int i;
            for(i = 0; i<a.length;i++){
                  if(i%2 == 0)
                        c[i] = b[i];
                  if(i%2 != 0)
                        c[i] = a[i];
            }
            return c;
      }
      public static void main(String[] args) {
            int[] a = {12,1,32,3};
            int[] b = {0,12,2,23};
            int[] c = retrievePosition(a,b);
            for(int d : c)
                  System.out.println(d);
      }
}

Output :

0   1   2   3

29. Sum of fibonaaci series upto given input.

Input : 3
Logic : 1+1+2
Output : 4
package com.jaladhi;
import java.util.*;
public class Six {
      public static int sumOfFibonacci(int n){
            int a = -1,b = 1,c = 0,d = 0;
            for(int i = 0;i ≤ n;i++){
                  c = a+b;
                  a = b;
                  b = c;
                  d += c;
            }
            return d;
      }
      public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            int n = s.nextInt();
            System.out.println(sumOfFibonacci(n));
      }
}

Output :

4

30. Retrieve the odd numbers till given input number. add and subtract it consecutively and return the result.

Input : 9
Output : 1+3-5+7-9=-3
package com.jaladhi;
import java.util.*;
public class Seven {
      public static int consecutiveSumSubofOddNos(int n){
            List l1 = new ArrayList();
            for(int i = 1;i≤n;i++)
                  if(i%2!=0)
                        l1.add(i);
            int n1 = l1.get(0);
            for(int i = 1; i < l1.size();i++)
                  if(i%2 != 0)
                        n1 = n1 + l1.get(i);
                  else
                        n1 = n1 - l1.get(i);
            return n1;
      }
      public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            int n = s.nextInt();
            System.out.println(consecutiveSumSubofOddNos(n));
      }
}

Output :

-3