Herzlich Willkommen

Live processing contents

Wednesday, November 24, 2010

PRIME or COMPOSITE

 PRIME & COMPOSITE

/*author by AGINTA GENIUSA
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

public class PrimaatauKomposit {

    public static void main(String[] args) {
        System.out.println("* Masukan angka* ");
        System.out.println("Angka anda merupakan bilangan prima atau bilangan campuran : ");
        int number = Input.readInt(); /* mengambil dari metode baru
        filterNum(number);
        primeOrNot();
    }
    protected static int num;

    public static void filterNum(int n) {
        num = n;
        if (num <= 0) {
            System.out.println("--hanya angka hitung yang diperbolehkan!--");
            System.exit(0);
        } else if (num == 1) {
            System.out.println("xxx maaf, bukan bilangan prima maupun campuran.xxx");
            System.exit(0);
        }
    }

    public static int pembagi() {
        int count = 0;
        for (int testNum = 1; testNum <= num; testNum++) {
            if (num % testNum == 0) {
                count++;
            }
        }
        return count;
    }

    public static void primeOrNot() {
        if (pembagi() <= 2) {
            System.out.println("--Angka anda termasuk bilangan Prima (PRIME).--");
        } else {
            System.out.println("--Angka anda termasuk bilangan campuran (COMPOSITE).--");
           
        }
    }
}





 /* NEW METHOD TO INPUT



import java.io.*;

public class Input{

  private static String str="";
  private static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  private static int i=0;
  private static double d=0.0;

  public static String readString(){
    try{
      str=br.readLine();
    }
    catch(IOException e){
      System.out.println(e);
    }
    return str;
  }

  public static int readInt(){
    try{
      str=br.readLine();
      i=Integer.parseInt(str);
    }
    catch(IOException e){
      System.out.println(e);
    }
    return i;
  }

  public static double readDouble(){
    try{
      str=br.readLine();
      d=Double.parseDouble(str);
    }
    catch(IOException e){
      System.out.println(e);
    }
    return d;
  }

}

No comments:

Post a Comment