From judgingtime @ Yahoo Answers :

I'm writing a Java program that needs to utilize keyboard reader in a terminal I/O prompt so that the user inputs an integer, and the program checks to see if it is prime or composite. If the number is prime; the system also needs to print the prime factorization (for example, prime factors of 16 are 2, 2, 2, and 2.) If anyone can help me out here, it'd be greatly appreciated. I've hit a wall and it's getting late for me. Last and simply, I also need to put a (y/n) at the end to ask if you want to check another number, just so I don't have to re-run the program each and every time. I thank you in advance for any help!

Hello judgingtime, here's your program :

import java.io.*;

class judgingtime

{

static void check()throws IOException

{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

char opt;

int c,i;

do

{

System.out.print("Enter a number : ");

c=Integer.parseInt(br.readLine());

if(prime(c)==true)

System.out.print("\nThe number is prime !");

else

System.out.print("\nThe number is composite !");

System.out.print("\n\nPrime Factors of "+c+" : ");

for(i=1;i<=c;i++)

{

if(c%i==0 && prime(i)==true)

{

System.out.print(i+" X ");

c=c/i;

i=1;

}

if(prime(c)==true)

{

System.out.print(c);

break;

}

}

System.out.print("\n\nDo you want to continue (Y/N) : ");

opt=(br.readLine()).charAt(0);

System.out.print("\n");

}while(opt!='n' && opt!='N');

System.out.print("Thank You");

}

static boolean prime(int a)

{

int i,j=0;

for(i=1;i<=a/2;i++)

{

if(a%i==0)

j++;

}

if(j==1)

return true;

else

return false;

}

}

I'm writing a Java program that needs to utilize keyboard reader in a terminal I/O prompt so that the user inputs an integer, and the program checks to see if it is prime or composite. If the number is prime; the system also needs to print the prime factorization (for example, prime factors of 16 are 2, 2, 2, and 2.) If anyone can help me out here, it'd be greatly appreciated. I've hit a wall and it's getting late for me. Last and simply, I also need to put a (y/n) at the end to ask if you want to check another number, just so I don't have to re-run the program each and every time. I thank you in advance for any help!

Hello judgingtime, here's your program :

**Java Program :**import java.io.*;

class judgingtime

{

static void check()throws IOException

{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

char opt;

int c,i;

do

{

System.out.print("Enter a number : ");

c=Integer.parseInt(br.readLine());

if(prime(c)==true)

System.out.print("\nThe number is prime !");

else

System.out.print("\nThe number is composite !");

System.out.print("\n\nPrime Factors of "+c+" : ");

for(i=1;i<=c;i++)

{

if(c%i==0 && prime(i)==true)

{

System.out.print(i+" X ");

c=c/i;

i=1;

}

if(prime(c)==true)

{

System.out.print(c);

break;

}

}

System.out.print("\n\nDo you want to continue (Y/N) : ");

opt=(br.readLine()).charAt(0);

System.out.print("\n");

}while(opt!='n' && opt!='N');

System.out.print("Thank You");

}

static boolean prime(int a)

{

int i,j=0;

for(i=1;i<=a/2;i++)

{

if(a%i==0)

j++;

}

if(j==1)

return true;

else

return false;

}

}

Judgingtime here. I appreciate the work, Mayank! For some reason though, my IDE isn't executing it. It says the selection does not have a main type. But it does... If it matters, I'm using Eclipse Indigo. I'll just have to pull pieces of code from it. But a very clean code sir and thank you again!

ReplyDeleteThank you for the compliments. Since I use BlueJ, I don't know your format of programs, neither was I able to use the terminalio reader. It said that the class i was importing wasn't available. But that doesn't matter. All you need is the main code and you can easily convert it into your format. Thanks for using my blog.

ReplyDelete