Posts

Showing posts from July, 2012

Algorithm Sample - Multiplication Table

package algorithmsample33;

/**
 *
 * @author Nurhak
 */
public class AlgorithmSample33 {

    public static void main(String[] args) {
        for (int i = 0; i <= 10; i++) {
            System.out.println(i + " Multiplication Table");
            for (int j = 0; j <= 10; j++) {
                System.out.println(i+" X "+j+" = "+(i * j));
            }
        }
    }
}

Algorithm Sample - Working with even and odd numbers

package algorithmsample32;

import java.util.Scanner;

/**
 *
 * @author Nurhak
 */
public class AlgorithmSample32 {

    public static void main(String[] args) {
        int number = 0;
        int sumEvenNumbers = 0;
        int sumOddNumbers = 0;
        String data = "";

        try {
            Scanner scan = new Scanner(System.in);
            System.out.println("Please enter 10 numbers.");

            for (int i = 1; i <= 10; i++) {
                System.out.println("Number : " + i);
                data = scan.nextLine();
                number = Integer.parseInt(data);

                if (number % 2 == 0) {
                    sumEvenNumbers = sumEvenNumbers + number;
                } else {
                    sumOddNumbers = sumOddNumbers + number;
                }
            }

            System.out.println("Sum of even numbers: " + sumEvenNumbers
                    + " Sum of odd numbers: " + sumOddNumbers);
       …

Algorithm Sample - Factorial - Reverse Engineering

package algorithmsample31;

import java.util.Scanner;

/**
 *
 * @author Nurhak
 */
public class AlgorithmSample31 {

    public static void main(String[] args) {
        String data = "";
        int number = 0;
        int factorial = 0;

        try {
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter the factorial fuction result please:");
            data = scan.nextLine();
            factorial = Integer.parseInt(data);
            number = factorial;
            for (int i = 1; i <= (factorial - 1); i++) {
                factorial = factorial / i;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(number + " equals to " + factorial + " !");
    }
}

Algorithm Sample - Factorial

package algorithmsample30;

import java.util.Scanner;

/**
 *
 * @author Nurhak
 */
public class AlgorithmSample30 {

    public static void main(String[] args) {
        int number = 0;
        int factorial = 1;
        String data = "";

        try {
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter the number.");
            data = scan.nextLine();
            number = Integer.parseInt(data);

            for (int i = number; i >= 1; i += -1) {
                factorial = factorial * i;
            }
            System.out.println(number + " ! = " + factorial);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Algorithm Sample - Finding the biggest and smallest number

package algorithmsample29;

import java.util.Scanner;

/**
 *
 * @author Nurhak
 */
public class AlgorithmSample29 {

    public static void main(String[] args) {
        int number = 0;
        int biggestNumber = 0;
        int smallestNumber = 0;
        String data = "";
        try {
            Scanner scan = new Scanner(System.in);
            for (int i = 1; i <= 10; i++) {
                System.out.println("Enter the number " + i);
                data = scan.nextLine();
                number = Integer.parseInt(data);
                if (number > biggestNumber) {
                    biggestNumber = number;
                }
                if (number < smallestNumber) {
                    smallestNumber = number;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("----------------");
        System.out.println("The biggest number is: " + biggest…

Algorithm Sample - Listing the first 10 even numbers which are greater than 0

package algorithmsample28;

import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Nurhak
 */
public class AlgorithmSample28 {

    public static void main(String[] args) {
        int i = 0;
        ArrayList list = new ArrayList();
        do {
            if (i % 2 == 0) {
                System.out.println(i);
                list.add(i);
            }
            i++;
        } while (list.size() < 10);

        System.out.println("List of elements: ");
        Iterator it = list.iterator();
        while (it.hasNext()) {
            Object object = it.next();
            System.out.println(object);
        }
    }
}

Algorithm Sample - Absolute value of a math function

package algorithmsample24;

import java.util.Scanner;

/**
 *
 * @author Nurhak
 */
public class AlgorithmSample24 {

    public static void main(String[] args) {
        int number1 = 0;
        int number2 = 0;
        int subResult = 0;
        String data = "";

        try {
            Scanner scan = new Scanner(System.in);
            System.out.println("Please enter the first number.");
            data = scan.nextLine();
            number1 = Integer.parseInt(data);

            System.out.println("Please enter the second number.");
            data = scan.nextLine();
            number2 = Integer.parseInt(data);

            subResult = number1 - number2;
            System.out.println("Absolute value of subtraction is: " + Math.abs(subResult));
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
}

Algorithm Sample - Multiple of the numbers that are inserted by the user

package algorithmsample22;

import java.util.Scanner;

/**
 *
 * @author Nurhak
 */
public class AlgorithmSample22 {

    public static void main(String[] args) {
        int number = 0;
        int result = 1;
        String data = "";

        try {
            Scanner scan = new Scanner(System.in);
            for (int i = 1; i <= 5; i++) {
                System.out.println("Enter the number: "+i);
                data = scan.nextLine();
                number = Integer.parseInt(data);
                result = result * number;
            }
            System.out.println("Multiple of the numbers that are inserted by the user: " + result);
        } catch (Exception e) {
            System.err.print(e);
        }
    }
}

Algorithm Sample - Reprocessing the inserted value according to an algorithm

package algorithmsample9;

import java.util.Scanner;

/**
 *
 * @author Nurhak
 */
public class AlgorithmSample9 {

    public static void main(String[] args) {
        int number = 0;
        String data = "";

        try {
            Scanner scan = new Scanner(System.in);
            System.out.println("Please enter an integer value!");
            data = scan.nextLine();
            number = Integer.parseInt(data);

            for (int i = 1; i <= number * 2; i++) {
                System.out.println(i + " --> " + number);
            }
        } catch (Exception e) {
            System.err.println("An error occured! Please enter an integer value!");
        }
    }
}

Algorithm Sample - Inch to Cm convertion

package algorithmsample7;

import java.util.Scanner;

/**
 *
 * @author Nurhak
 */
public class AlgorithmSample7 {

    public static void main(String[] args) {
        String data = "";
        double dataInch = 0;
        double dataCm = 0;
        try {
            Scanner scan = new Scanner(System.in);

            System.out.println("Please enter data (inch)");
            data = scan.nextLine();
            dataInch = Double.parseDouble(data);

            dataCm = dataInch * 2.54;
            System.out.println(dataInch + " inch equals to " + dataCm + " cm.");

        } catch (Exception e) {
            System.err.println("You probably entered a wrong value like a String value!");
        }
    }
}

Algorithm Sample - Area of a Triangle

package algorithmsample6;

import java.util.Scanner;

/**
 *
 * @author Nurhak
 */
public class AlgorithmSample6 {

    public static void main(String[] args) {
        int base = 0;
        int height = 0;
        double area = 0;
        String data = "";

        try {
            Scanner scan = new Scanner(System.in);
            System.out.println("Please enter the base value(mm).");
            data = scan.nextLine();
            base = Integer.parseInt(data);

            System.out.println("Please enter the height value(mm).");
            data=scan.nextLine();
            height = Integer.parseInt(data);

            area = (base * height) / 2;
            System.out.println("Area of the triangle is: " + area + " mm2");

        } catch (Exception e) {
            System.err.println("Please enter an integer value!");
        }
    }
}

Algorithm Sample - Area and Perimeter of a Rectangle

package algorithmsample5;

import java.util.Scanner;

/**
 *
 * @author Nurhak
 */
public class AlgorithmSample5 {

    public static void main(String[] args) {
        double side1 = 0;
        double side2 = 0;
        double area = 0;
        double perimeter = 0;
        String value = "";

        try {
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter the length(mm) of first side.");
            value = scan.nextLine();
            side1 = Double.parseDouble(value);

            System.out.println("Enter the length(mm) of second side.");
            value = scan.nextLine();
            side2 = Double.parseDouble(value);

            area = side1 * side2;
            perimeter = (side1 + side2) * 2;

            System.out.println("Area : " + area + " mm2 and Perimeter : " + perimeter + " mm");
        } catch (Exception e) {
        }
    }
}

Algorithm Sample - Division of two numbers that are inserted by the user

package algorithmsample4;

import java.util.Scanner;

/**
 *
 * @author Nurhak
 */
public class AlgorithmSample4 {

    public static void main(String[] args) {
        double result = 0;
        double number1 = 0;
        double number2 = 0;
        String number = "";

        try {
            Scanner scan = new Scanner(System.in);
            System.out.println("Please enter the first number.");
            number = scan.nextLine();
            number1 = Double.parseDouble(number);

            System.out.println("Please enter the second number.");
            number = scan.nextLine();
            number2 = Double.parseDouble(number);

            result = number1 / number2;
            System.out.println("When we divide first number to second one, the result is: " + result);
        } catch (Exception e) {
        }

    }
}

Algorithm Sample - Sum of two numbers that are inserted by the user

package algorithmsample3;

import java.util.Scanner;

/**
 *
 * @author Nurhak
 */
public class AlgorithmSample3 {

    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int total=0;
        int number1=0;
        int number2=0;

        System.out.println("Please enter the first number:");
        number1=scan.nextInt();

        System.out.println("Please enter the second number:");
        number2=scan.nextInt();

        total=number1+number2;

        System.out.println("Result: "+total);
    }
}

Algorithm Sample - Sum of the numbers from 1 to 100(1 and 100 included)

package algorithmsample2;

/**
 *
 * @author Nurhak
 */
public class AlgorithmSample2 {

    public static void main(String[] args) {
        int total = 0;
        int x = 0;
        for (x = 1; x <= 100; x++) {
            System.out.println("Immediate Total: " + total);
            total = total + x;
        }
        System.out.println("************************");
        System.out.println("Total: " + total);
    }
}

Algorithm Sample - Are you legally an adult?

package algorithmsample1;
import java.util.Scanner;
/**  *  * @author Nurhak  */ public class AlgorithmSample1 {
    public static void main(String[] args) {         Scanner scan = new Scanner(System.in);         int age = 0;
        System.out.println("Please enter your age!");         try {             age = scan.nextInt();             if (age >= 18) {                 System.out.println("You are legally an adult!");             } else {                 System.out.println("You are not legally an adult!");             }         } catch (Exception e) {             System.out.println("Please enter a valid value!");         }     } }