Thursday, 5 December 2013

Unknown

To Find Minimum of Two Numbers using conditional operator

/*
 To find minimum of 2 Numbers using ternary operator
 */

class Minoftwo {

    public static void main(String args[]) {
//taking value as command line argument.
//Converting String format to Integer value
        int i = Integer.parseInt(args[0]);
        int j = Integer.parseInt(args[1]);
        int result = (i < j) ? i : j;
        System.out.println(result + " is a minimum value");
    }
}
Read More
Unknown

To Find Maximum of Two Numbers.

/*
 To Find Maximum of 2 Numbers using if else
 */

class Maxoftwo {

    public static void main(String args[]) {
//taking value as command line argument.
//Converting String format to Integer value
        int i = Integer.parseInt(args[0]);
        int j = Integer.parseInt(args[1]);
        if (i > j) {
            System.out.println(i + " is greater than " + j);
        } else {
            System.out.println(j + " is greater than " + i);
        }
    }
}
Read More
Unknown

pyramid of numbers using for loops

/*
 Generate Pyramid For a Given Number Example
 This Java example shows how to generate a pyramid of numbers for given
 number using for loop example.
 */

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class GeneratePyramidExample {

    public static void main(String[] args) throws Exception {
        BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter Number:");
        int as = Integer.parseInt(keyboard.readLine());
        System.out.println("Enter X:");
        int x = Integer.parseInt(keyboard.readLine());
        int y = 0;
        for (int i = 0; i <= as; i++) {

            for (int j = 1; j <= i; j++) {
                System.out.print(y + "\t");
                y = y + x;
            }
            System.out.println("");
        }
    }
}
/*
 Output of this example would be
 Enter Number:
 5
 Enter X:
 1
 0
 1 2
 3 4 5
 6 7 8 9
 10 11 12 13 14
 ----------------------------------------------
 17
 Enter Number:
 5
 Enter X:
 2
 0
 2 4
 6 8 10
 12 14 16 18
 20 22 24 26 28
 ----------------------------------------------
 Enter Number:
 5
 Enter X:
 3
 0
 3 6
 9 12 15
 18 21 24 27
 30 33 36 39 42
 */

Read More
Unknown

Factorial of a number using recursion

/*
 This program shows how to calculate
 Factorial of a number using recursion function.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class JavaFactorialUsingRecursion {

    public static void main(String args[]) throws NumberFormatException,
            IOException {
        System.out.println("Enter the number: ");
//get input from the user
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int a = Integer.parseInt(br.readLine());
//call the recursive function to generate factorial
        int result = fact(a);
        System.out.println("Factorial of the number is: " + result);
    }

    static int fact(int b) {
        if (b <= 1) //if the number is 1 then return 1
        {
            return 1;
        } else //else call the same function with the value - 1
        {
            return b * fact(b - 1);
        }
    }
}
/*
Output of this Java example would be
Enter the number:
5
Factorial of the number is: 120
*/
Read More
Unknown

Calculate Circle Area using radius

/*
 This program shows how to calculate
 area of circle using it's radius.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CalculateCircleAreaExample {

    public static void main(String[] args) {
        int radius = 0;
        System.out.println("Please enter radius of a circle");
        try {
//get the radius from console
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            radius = Integer.parseInt(br.readLine());
        } //if invalid value was entered
        catch (NumberFormatException ne) {
            System.out.println("Invalid radius value" + ne);
            System.exit(0);
        } catch (IOException ioe) {
            System.out.println("IO Error :" + ioe);
            System.exit(0);
        }
        /*
         * Area of a circle is
         * pi * r * r
         * where r is a radius of a circle.
         */
//NOTE : use Math.PI constant to get value of pi
        double area = Math.PI * radius * radius;
        System.out.println("Area of a circle is " + area);
    }
}
/*
Output of Calculate Circle Area using Java Example would be

Please enter radius of a circle
19
Area of a circle is 1134.1149479459152
*/
Read More
Unknown

Nested Switch

/*
Statements Example
This example shows how to use nested switch statements in a
java program.
*/

public class NestedSwitchExample {

    public static void main(String[] args) {
        /*
         * Like any other Java statements, switch statements
         * can also be nested in each other as given in
         * below example.
         */
        int i = 0;
        int j = 1;
        switch (i) {
            case 0:
                switch (j) {
                    case 0:
                        System.out.println("i is 0, j is 0");
                        break;
                    case 1:
                        System.out.println("i is 0, j is 1");
                        break;

                    default:
                        System.out.println("nested default case ! ");
                }
                break;
            default:
                System.out.println("No matching case found!!");
        }
    }
}
/*
Output would be,
i is 0, j is 1
*/
Read More
Unknown

Reversed pyramid using for loops & decrement operator.


/*
Pyramid 5 Example
This Java Pyramid example shows how to generate pyramid or triangle
like given below using for loop.
12345
1234
123
12
1
*/
public class JavaPyramid5 {

    public static void main(String[] args) {
        for (int i = 5; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                System.out.print(j + 1);
            }
            System.out.println("");
        }
    }
}
/*
Output of the example would be
12345
1234
123
12
1
*/
Read More
Unknown

Pyramid of stars using nested for loops

/*
 Java Pyramid 1 Example
 This Java Pyramid example shows how to generate pyramid or triangle
 like given below using for loop.
 *
 **
 ***
 ****
 *****
 */

public class JavaPyramid1 {

    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 0; j < i; j++) {
                System.out.print("*");
            }
//generate a new line
            System.out.println("");
        }
    }
}
/*
Output of the above program would be
*
**
***
****
*****
*/
Read More

Wednesday, 4 December 2013

Unknown

Generate prime numbers between 1 & given number

/*
 Prime Numbers Java Example
 This Prime Numbers Java example shows how to generate prime numbers
 between 1 and given number using for loop.
 */

public class GeneratePrimeNumbersExample {

    public static void main(String[] args) {
//define limit
        int limit = 100;
        System.out.println("Prime numbers between 1 and " + limit);
//loop through the numbers one by one
        for (int i = 1; i < 100; i++) {
            boolean isPrime = true;
//check to see if the number is prime
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }
// print the number
            if (isPrime) {
                System.out.print(i + " ");
            }
        }
        
    }
}
/*
Output of Prime Numbers example would be
Prime numbers between 1 and 100
1 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
*/
Read More
Unknown

Palindrome Number


/*
 This program shows how to check for in the given list of numbers
 whether each number is palindrome or not
 */

public class JavaPalindromeNumberExample {

    public static void main(String[] args) {
//array of numbers to be checked
        int numbers[] = new int[]{121, 13, 34, 11, 22, 54};
//iterate through the numbers
        for (int i = 0; i < numbers.length; i++) {
            int number = numbers[i];
            int reversedNumber = 0;
            int temp = 0;
            /*
             * If the number is equal to it's reversed number, then
             * the given number is a palindrome number.
             *
             * For ex,121 is a palindrome number while 12 is not.
             */
//reverse the number
            while (number > 0) {
                temp = number % 10;
                number = number / 10;
                reversedNumber = reversedNumber * 10 + temp;
                
            }
            if (numbers[i] == reversedNumber) {
                System.out.println(numbers[i] + " is a palindrome");
            } else {
                System.out.println(numbers[i] + " not a palindrome ");
            }
        }
    }
}
/*
Output of Java Palindrome Number Example would be
121 is a palindrome number
13 is not a palindrome number
34 is not a palindrome number
11 is a palindrome number
22 is a palindrome number
54 is not a palindrome number
*/
Read More