• August 23, 2014
  • Core Java
  • Comments Off on Important Java Programs

Important Java Programs

Program to print alphabets both in small and capital.

/**
 * This program is used to print alphabets both in small and capital.
 * @author javawithease
 */
public class Alphabets {
      public static void main(String args[]){
            char ch;
            System.out.println("Small Alphabets: ");
            for( ch = 'a' ; ch <= 'z' ; ch++ ){
               System.out.println(ch);
            }           
 
            System.out.println("Capital Alphabets: ");
 
            for( ch = 'A' ; ch <= 'Z' ; ch++ ){
               System.out.println(ch);
            }
         }
}

Output:

Small Alphabets:
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
Capital Alphabets:
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

Download this example.

Program to perform arithmetic operations.

/**
 * This program is used to perform arithmetic operations.
 * @author javawithease
 */
public class ArithmeticOperations {
      /**
       * This method is used to add two numbers.
       * @param num1
       * @param num2
       */
      static void addition(int num1, int num2){
            System.out.print("Addition of given numbers = ");
            System.out.println(num1 + num2);
      }     
 
      /**
       * This method is used to subtract two numbers.
       * @param num1
       * @param num2
       */
      static void subtraction(int num1, int num2){
            System.out.print("Subtraction of given numbers = ");
            System.out.println(num1 - num2);
      }     
 
      /**
       * This method is used to multiply two numbers.
       * @param num1
       * @param num2
       */
      static void multiplication(int num1, int num2){
            System.out.print("Multiplication of given no.s = ");
            System.out.println(num1 * num2);
      }     
 
      /**
       * This method is used to divide two numbers.
       * @param num1
       * @param num2
       */
      static void division(int num1, int num2){
            System.out.print("Division of given given no.s = ");
            System.out.println(num1 / num2);
      }     
 
      /**
       * This method is used to modulo divide two numbers.
       * @param num1
       * @param num2
       */
 
      static void moduloDivision(int num1, int num2){
            System.out.print("ModuloDivision of given no.s = ");
            System.out.println(num1 % num2);
      }
 
      public static void main(String args[]){
            //method call from main method
            addition(20, 10);
            subtraction(40, 30);
            multiplication(20, 30);
            division(20, 4);
            moduloDivision(20, 3);
      }
}

Output:

Addition of given numbers = 30
Subtraction of given numbers = 10
Multiplication of given numbers = 600
Division of given given numbers = 5
ModuloDivision of given numbers = 2

Download this example.

Program to find that given number is Armstrong or not.

/**
 * This program is used to find that given number is Armstrong or not.
 * @author javawithease
 */
public class ArmstrongNumber {
      /**
       *This method is used to find that given number is Armstrong or not
       *@param num
       */
      static void armstrong(int num){
            int newNum = 0, reminder, temp;
            temp = num;
            //find sum of all digit's cube of the number.
            while(temp != 0){
                  reminder = temp % 10;
                  newNum = newNum + reminder*reminder*reminder;
                  temp = temp/10;
            }
            //Check if sum of all digit's cube of the number is
            //equal to the given number or not.
            if(newNum == num){
                  System.out.println(num +" is armstrong.");
            }else{
                  System.out.println(num +" is not armstrong.");
            }
      }     
 
      public static void main(String args[]){
            //method call
            armstrong(407);
      }
}

Output:

407 is armstrong.

Download this example.

 Program to check that given number is even or odd.

/**
 * This program is used to check that given number is even or odd.
 * @author javawithease
 */
public class EvenOrOdd {
      /**
       * This method is used to check that given no is even or odd.
       * @param num
       */
      static void evenOdd(int num){
            if(num % 2 == 0){
                  System.out.println("Given number is even.");
            }else{
                  System.out.println("Given number is odd.");
            }
      }
 
      public static void main(String args[]){
            //method call, no need of object.
            evenOdd(123);
      }
}

Output:

Given number is odd.

Download this example.

Program to calculate Factorial of given number.

/**
 * This program is used to calculate Factorial of given number.
 * @author javawithease
 */
public class Factorial {
      /**
       * This method is used to calculate Factorial of given no.
       * @param num
       */
 
      static void factorialNumber(int num){
            int fact = 1;
            //Factorial of 0 and 1 is 1.
            if(num == 0 || num == 1){
              System.out.println("factorial of " + num + " is 1.");
            }
 
            //for calculating factorial, number should be non negative.
            if(num > 0){
              //calculate factorial.
              for(int i = 2; i <= num; i++){
                  fact = fact*i;
              }
              System.out.println("factorial of " + num + " is " +fact);
            }else{
              System.out.println("no. should be non negative.");
            }          
      }     
 
      public static void main(String args[]){
            //method call
            factorialNumber(5);
      }
}

Output:

factorial of 5 is 120

Download this example.

Program to find factorial of given number by recursion. 

/**
 * This program is used to find factorial of given number by recursion.
 * @author javawithease
 */
public class FactorialByRecursion {
      /**
       * This method is used to find factorial of given no. by recursion.
       * @param num
       * @return int
       */
      static int fact(int num){
            //Factorial of 0 is 1.
            if (num == 0)
                  return 1;
            else
                  return num * fact(num-1);
      }     
 
      public static void main(String args[]){
            int num = 5, factorial;
            //method call
            if(num > 0){
               factorial = fact(num);
               System.out.println("Factorial of "+num+" is "+ factorial);
            }else{
                  System.out.println("Number should be non negative.");
            }          
      }
}

Output:

Factorial of 5 is 120

Download this example.

Program to print fibonacci series.

/**
 * This program is used to print fibonacci series.
 * @author javawithease
 */
 
public class FibonacciSeries {
      /**
       * This method is used to print fibonacci series.
       * @param num
       */
      static void fibonacci(int num){
            int f1, f2=0, f3=1;
            for(int i=1;i <=num;i++){
                  System.out.println(f3);
                  f1 = f2;
                  f2 = f3;
                  f3 = f1 + f2;
            }
      }     
 
      public static void main(String args[]){
            int num = 6;
            //method call
            if(num > 0){
                  fibonacci(num);
            }else{
                  System.out.println("No. should be greater than zero.");
            }
      }
 
}

Output:

1
1
2
3
5
8

Download this example.

Program to find that given number is Palindrome or not.

/**
 * This program is used to find that given number is Palindrome or not.
 * @author javawithease
 */
public class PalindromeNumber {
      static void palindrome(int num){
            int newNum = 0, reminder, temp;
            temp = num;
            //find sum of all digit's of the number.
            while(temp != 0){
                  reminder = temp % 10;
                  newNum = newNum*10 + reminder;
                  temp = temp/10;
            }
            //Check if sum of all digit's of the number
            //is equal to the given number or not.
            if(newNum == num){
                  System.out.println(num +" is palindrome.");
            }else{
                  System.out.println(num +" is not palindrome.");
            }
      }    
 
      public static void main(String args[]){
            //method call
            palindrome(12321);
      }
}

Output:

12321 is palindrome.

Download this example.

Program to find that given number is prime or not.

/**
 * This program is used to find that given number is prime or not.
 * @author javawithease
 */
 
public class PrimeNumber {
      /**
       * This method is used to find that given number is prime or not.
       * @param num
       */
      static void primeNumber(int num){
            int count = 0;
            //0 and 1 are not prime numbers.
            if(num == 0 || num == 1){
                  System.out.println(num + " is not prime.");
            }else{
                  for(int i = 1; i <= num/2; i++){
                        if(num % i == 0){
                              count++;
                        }
                  }
                  if(count > 1){
                        System.out.println(num + " is not prime.");
                  }else{
                        System.out.println(num + " is prime.");
                  }
            }
      }    
 
      public static void main(String args[]){
            //method call
            primeNumber(37);
      }
}

Output:

37 is prime.

Download this example.

Program to swap two numbers without using third or temp variable.

/**
 * This program is used to swap two numbers without using third variable.
 * @author javawithease
 */
public class SwapNumbers {
      /**
       * This method is used to swap no.s without using third variable.
       * @param num1
       * @param num2
       */
      static void swapNumbers(int num1, int num2){
            num1 = num1 + num2;
            num2 = num1 - num2;
            num1 = num1 - num2;
 
            System.out.println("After swapping: "+ num1 + " and " + num2);
      }     
 
      public static void main(String args[]){
            int num1 = 20;
            int num2 = 30;
           System.out.println("Before swapping:"+ num1 + " and " + num2);
            //method call
            swapNumbers(num1, num2);
      }
 
}

Output:

Before swapping no.s are : 20 and 30
After swapping no.s are : 30 and 20

Download this example.
 
Next Topic: How to set permanent path in java?
Previous Topic: Hello world java program.

Related Topics:

Some important definitions for java programs.
Variable and data types.
Hello world java program.
Important Java Programs.
How to set permanent path in java?

 


Comments are closed.