I am new with JAVA (computer programming by the way). The following program checks if the input is binary or not. And it should prompt user to re-enter integers until binary number is entered. But this program is doing exactly opposite.
It is asking me to re-enter integers if input is binary, and the program terminates when non-binary is entered.
I need a serious help, please.Here is my output
public static void main(String[] args) {
int value, userValue;
int binaryDigit = 0, notBinaryDigit = 0;
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("Please enter positive integers: ");
userValue = scan.nextInt();
value = userValue;
while (userValue > 0) {
if ((userValue % 10 == 0) || (userValue % 10 == 1)) {
binaryDigit++;
} else {
notBinaryDigit++;
}
userValue = userValue / 10;
}
if (notBinaryDigit > 0) {
System.out.println(value + " is a not a Binary Number.");
break;
} else {
System.out.println(value + " is a Binary Number.");
}
}
}
It might be too late for the answer. But the code can be simplified much if you make use of the method.
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("Please enter positive integers: ");
int userValue = scan.nextInt();
if (isBinary(userValue)) {
System.out.println(userValue + " is a Binary Number.");
break;
} else {
System.out.println(userValue + " is a not Binary Number.");
}
}
}
public static boolean isBinary(int input) {
while (input > 0) {
if ((input % 10 != 0) && (input % 10 != 1)) {
return false;
}
input = input / 10;
}
return true;
}
}
Change your code
from this
if (notBinaryDigit > 0) {
System.out.println(value + " is a not a Binary Number.");
break;
} else {
System.out.println(value + " is a Binary Number.");
}
to this
if (notBinaryDigit > 0) {
System.out.println(value + " is a not a Binary Number.");
notBinaryDigit--;
} if(binaryDigit >0 {
System.out.println(value + " is a Binary Number.");
binaryDigit--;
break;
}
and it will ask for values and tell if it is binary or not and it terminates if it is binary
Error coming due to Scanner class.
you can run your program and check your logic by removing the Scanner class like this.
public class Test{
public static void main(String []args){
int value, userValue=34;
int binaryDigit = 0, notBinaryDigit = 0;
value = userValue;
while (userValue > 0) {
if ((userValue % 10 == 0) || (userValue % 10 == 1)) {
binaryDigit++;
} else {
notBinaryDigit++;
}
userValue = userValue / 10;
}
if (notBinaryDigit > 0) {
System.out.println(value + " is a not a Binary Number.");
} else {
System.out.println(value + " is a Binary Number.");
}
}
}
Related
I write program to solve several problems with numbers. My program should:
Welcome users;
Display the instructions;
Ask for a request;
Terminate the program if a user enters zero;
List item
If a number is not natural, print an error message;
Print the properties of the natural number;
Continue execution from step 3, after the request has been processed.
I got an error:
The program should continue to work till the user enter 0.
Here is my code:
package numbers;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to Amazing Numbers!\n" +
"\n" +
"Supported requests:\n" +
"- enter a natural number to know its properties;\n" +
"- enter 0 to exit.\n" +
"\n" +
"Enter a request:");
int number = scanner.nextInt();
if (number >= 0) {
System.out.println("Properties of " + number);
System.out.println("\t even: " + (number % 2 == 0));
System.out.println("\t odd: " + (number % 2 != 0));
System.out.println("\t buzz: " + checkBuzzNumber(number));
System.out.println("\t duck: " + checkDuckNumber(number));
System.out.println("\t palindromic: " + checkPalindromicNumber(number));
} else {
System.out.println("The first parameter should be a natural number or zero.");
}
}
public static boolean checkBuzzNumber(int number) {
return number % 7 == 0 || String.valueOf(number).endsWith("7");
}
public static boolean checkDuckNumber(int number) {
String num = String.valueOf(number);
boolean flag = false;
if (num.startsWith("0")) {
return false;
} else {
for (int i = 1; i < num.length(); i++) {
if (num.charAt(i) == '0') {
flag = true;
break;
}
}
return flag;
}
}
public static boolean checkPalindromicNumber(int number) {
int rem, rev = 0, temp;
temp = number;
while (number != 0) {
rem = number % 10;
rev = rev * 10 + rem;
number = number / 10;
}
if (temp == rev)
return true;
else
return false;
}
}
You are just a loop away from getting the desired output, below is the code with comments to understand properly. Also, I have commented the code which you wrote but I removed from there.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// System.out.println("Welcome to Amazing Numbers!\n" +
// "\n" +
// "Supported requests:\n" +
// "- enter a natural number to know its properties;\n" +
// "- enter 0 to exit.\n" +
// "\n" +
// "Enter a request:");
// int number = scanner.nextInt();
//-------------------------------------------------------------
while (true)// added an infinite while loop, in order to display and take input continuously
{
System.out.println("Welcome to Amazing Numbers!\n" +
"\n" +
"Supported requests:\n" +
"- enter a natural number to know its properties;\n" +
"- enter 0 to exit.\n" +
"\n" +
"Enter a request:");
int number = scanner.nextInt();
if (number == 0)// checks for the number to be zero or not.
{
break; // jump statement, breaks out of the loop.
}
if (number > 0) {// if entered number is greater than zero,
System.out.println("Properties of " + number);
System.out.println("\t even: " + (number % 2 == 0));
System.out.println("\t odd: " + (number % 2 != 0));
System.out.println("\t buzz: " + checkBuzzNumber(number));
System.out.println("\t duck: " + checkDuckNumber(number));
System.out.println("\t palindromic: " + checkPalindromicNumber(number));
System.out.println();// for a new line
}
else // if number is less than zero,
{
System.out.println("The first parameter should be a natural number or zero.\n");
}
}
scanner.close();//good practice to close the scanner.
//____________________________________________________________
// if (number >= 0) {
// System.out.println("Properties of " + number);
// System.out.println("\t even: " + (number % 2 == 0));
// System.out.println("\t odd: " + (number % 2 != 0));
// System.out.println("\t buzz: " + checkBuzzNumber(number));
// System.out.println("\t duck: " + checkDuckNumber(number));
// System.out.println("\t palindromic: " + checkPalindromicNumber(number));
// } else {
// System.out.println("The first parameter should be a natural number or zero.");
// }
}
public static boolean checkBuzzNumber(int number) {
return number % 7 == 0 || String.valueOf(number).endsWith("7");
}
public static boolean checkDuckNumber(int number) {
String num = String.valueOf(number);
boolean flag = false;
if (num.startsWith("0")) {
return false;
} else {
for (int i = 1; i < num.length(); i++) {
if (num.charAt(i) == '0') {
flag = true;
break;
}
}
return flag;
}
}
public static boolean checkPalindromicNumber(int number) {
int rem, rev = 0, temp;
temp = number;
while (number != 0) {
rem = number % 10;
rev = rev * 10 + rem;
number = number / 10;
}
if (temp == rev)
return true;
else
return false;
}
}
I would like to handle this situation about inputting wrong string, but error keeps happening because of the else if argument.
Tried try, catch but don't know how to apply it to this code.
import java.util.Scanner;
public class game
{
public static void main(String[] args) {
System.out.println("let's start the multiplication game.");
System.out.println("which times table do you want to choose?");
System.out.println("if you want to do it by your choice, please input number among 2~9. Or if you want to do it randomly, please input number 0");
System.out.println("press \"q\" if you want to quit");
System.out.println("==>");
String input;
Scanner s = new Scanner(System.in);
input = s.nextLine();
int answer;
int multiplier = (int)(Math.random()*8+2);
int random = (int)(Math.random()*8+2);
if (input.equals("q"))
{
System.out.print("quit the game.");
}
else if (Integer.parseInt(input) == 0)
{
System.out.println(random+"times table has been made automatically.");
System.out.print(random+" X "+multiplier+" = "+"? input your answer ==> ");
answer = s.nextInt();
if (answer == random*multiplier)
{
System.out.print("You're right!");
}
else
{
System.out.print("Wrong! The right answer is "+random*multiplier+".");
}
}
else if (Integer.parseInt(input)>=2 && Integer.parseInt(input)<=9)
{
int number = Integer.parseInt(input);
System.out.println("You chose"+number+" times table.");
System.out.print(number+" X "+multiplier+" = "+"? input your answer ==> ");
answer = s.nextInt();
if (answer == number*multiplier)
{
System.out.print("You're right!");
}
else
{
System.out.print("Wrong! The right answer is "+number*multiplier+".");
}
}
else
{
System.out.print("Error. Please try again.");
}
}
}
I expect the result from the else block, but when I input wrong string like "c" or "f" and etc, number format exception error: For input string: "c" (or "f" and etc) happens. Thanks for reading this, and hope you solve this problem.
int check = 0;
try{
check = Integer.parseInt(input);
}catch(NumberFormatException e){
System.out.println("Wrong input");
continue;
}
If you loop this input processing,if it enters catch,you may take input again or you can do what you want when invalid input is entered.If there is no exception,check is your input value,then you can use it in your if and else-if statements like
if(check>0){
...
}
Before Else blocks, you can parse the input like this:
if (input.equals("q")) {
System.out.print("quit the game.");
return;
}
int parsedInput = 0;
try {
parsedInput = Integer.parseInt(input);
} catch (NumberFormatException ex) {
System.out.print("Error. Please try again.");
return;
}
Then you can write if-else for integer input like below:
if (parsedInput == 0) { ... }
else if (parsedInput >= 2 && parsedInput <= 9) {...}
Last else is not needed because I moved it to catch block.
try this it can handle exception
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
System.out.println("let's start the multiplication game.");
System.out.println("which times table do you want to choose?");
System.out.println(
"if you want to do it by your choice, please input number among 2~9. Or if you want to do it randomly, please input number 0");
System.out.println("press \"q\" if you want to quit");
System.out.println("==>");
String input;
Scanner s = new Scanner(System.in);
input = s.nextLine();
int answer;
int multiplier = (int) (Math.random() * 8 + 2);
int random = (int) (Math.random() * 8 + 2);
if (input.equals("q")) {
System.out.print("quit the game.");
}
try {
int check_input = Integer.parseInt(input);
if (check_input == 0) {
System.out.println(random + "times table has been made automatically.");
System.out.print(random + " X " + multiplier + " = " + "? input your answer ==> ");
answer = s.nextInt();
if (answer == random * multiplier) {
System.out.print("You're right!");
} else {
System.out.print("Wrong! The right answer is " + random * multiplier + ".");
}
} else if (check_input >= 2 && check_input <= 9) {
int number = Integer.parseInt(input);
System.out.println("You chose" + number + " times table.");
System.out.print(number + " X " + multiplier + " = " + "? input your answer ==> ");
answer = s.nextInt();
if (answer == number * multiplier) {
System.out.print("You're right!");
} else {
System.out.print("Wrong! The right answer is " + number * multiplier + ".");
}
}
else {
System.out.print("Error. Please try again.");
}
} catch (Exception e) {
System.out.print("Error. Please try again.");
}
}
}
You just need to check the user provided input is number or not. Before parsing it.
I have added new isNumberic method which is returning boolean flag depending user's input and verify it.
public class Game {
public static void main(String[] args) {
System.out.println("let's start the multiplication game.");
System.out.println("which times table do you want to choose?");
System.out.println(
"if you want to do it by your choice, please input number among 2~9. Or if you want to do it randomly, please input number 0");
System.out.println("press \"q\" if you want to quit");
System.out.println("==>");
String input;
Scanner s = new Scanner(System.in);
input = s.nextLine();
int answer;
int multiplier = (int) (Math.random() * 8 + 2);
int random = (int) (Math.random() * 8 + 2);
Game game = new Game();
boolean isvalid = game.isNumeric(input);
if (input.equals("q")) {
System.out.print("quit the game.");
}
else if (isvalid && Integer.parseInt(input) == 0) {
System.out.println(random + "times table has been made automatically.");
System.out.print(random + " X " + multiplier + " = " + "? input your answer ==> ");
answer = s.nextInt();
if (answer == random * multiplier) {
System.out.print("You're right!");
} else {
System.out.print("Wrong! The right answer is " + random * multiplier + ".");
}
}
else if (isvalid && Integer.parseInt(input) >= 2 && Integer.parseInt(input) <= 9) {
int number = Integer.parseInt(input);
System.out.println("You chose" + number + " times table.");
System.out.print(number + " X " + multiplier + " = " + "? input your answer ==> ");
answer = s.nextInt();
if (answer == number * multiplier) {
System.out.print("You're right!");
} else {
System.out.print("Wrong! The right answer is " + number * multiplier + ".");
}
}
else {
System.out.print("Error. Please try again.");
}
}
public static boolean isNumeric(String str)
{
for (char c : str.toCharArray())
{
if (!Character.isDigit(c)) return false;
}
return true;
}
}
You can't send values like a , b, c etc for
Integer.parseInt(String s)
https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)
I have try catch block to catch numberformat exception with message to enter valid integer
public static void main(String args[]) {
System.out.println("let's start the multiplication game.");
System.out.println("which times table do you want to choose?");
System.out.println("if you want to do it by your choice, please input number among 2~9. Or if you want to do it randomly, please input number 0");
System.out.println("press \"q\" if you want to quit");
System.out.println("==>");
String input;
Scanner s = new Scanner(System.in);
input = s.nextLine();
int answer;
int multiplier = (int)(Math.random()*8+2);
int random = (int)(Math.random()*8+2);
try {
if (input.equals("q"))
{
System.out.print("quit the game.");
}
else if (Integer.parseInt(input) == 0)
{
System.out.println(random+"times table has been made automatically.");
System.out.print(random+" X "+multiplier+" = "+"? input your answer ==> ");
answer = s.nextInt();
if (answer == random*multiplier)
{
System.out.print("You're right!");
}
else
{
System.out.print("Wrong! The right answer is "+random*multiplier+".");
}
}
else if (Integer.parseInt(input)>=2 && Integer.parseInt(input)<=9)
{
int number = Integer.parseInt(input);
System.out.println("You chose"+number+" times table.");
System.out.print(number+" X "+multiplier+" = "+"? input your answer ==> ");
answer = s.nextInt();
if (answer == number*multiplier)
{
System.out.print("You're right!");
}
else
{
System.out.print("Wrong! The right answer is "+number*multiplier+".");
}
}
else
{
System.out.print("Error. Please try again.");
}
}
catch (NumberFormatException e ) {
System.out.print("pleae enter Valid integer");
}
It's obvious that you are getting this error because you are calling Integer.parseInt on a string variable which doesn't contain an Integer. What you should be doing is putting a check once you get the input to check if the string contains what you need. Something like below
while (true)
{
// Check that the input is between 0 and 9 or q.
if (!((input.charAt(0) >= '0' && input.charAt(0) <= '9') || input.charAt(0) == 'q'))
{
// If it is not, ask for input again.
System.out.println("Input is not correct. If you want to do it by your choice, please input number among 2~9. Or if you want to do it randomly, please input number 0");
input = s.nextLine();
}
else {
// If input is correct, break this look.
break;
}
}
Below is the whole code
public class game
{
public static void main(String[] args)
{
System.out.println("let's start the multiplication game.");
System.out.println("which times table do you want to choose?");
System.out.println("if you want to do it by your choice, please input number among 2~9. Or if you want to do it randomly, please input number 0");
System.out.println("press \"q\" if you want to quit");
System.out.println("==>");
String input;
Scanner s = new Scanner(System.in);
input = s.nextLine();
while (true)
{
if (!((input.charAt(0) >= '0' && input.charAt(0) <= '9') || input.charAt(0) == 'q'))
{
System.out.println("Input is not correct. If you want to do it by your choice, please input number among 2~9. Or if you want to do it randomly, please input number 0");
input = s.nextLine();
}
else {
break;
}
}
int answer;
int multiplier = (int) (Math.random() * 8 + 2);
int random = (int) (Math.random() * 8 + 2);
if (input.equals("q"))
{
System.out.print("quit the game.");
}
else if (Integer.parseInt(input) == 0)
{
System.out.println(random + "times table has been made automatically.");
System.out.print(random + " X " + multiplier + " = " + "? input your answer ==> ");
answer = s.nextInt();
if (answer == random * multiplier)
{
System.out.print("You're right!");
}
else
{
System.out.print("Wrong! The right answer is " + random * multiplier + ".");
}
}
else if (Integer.parseInt(input) >= 2 && Integer.parseInt(input) <= 9)
{
int number = Integer.parseInt(input);
System.out.println("You chose" + number + " times table.");
System.out.print(number + " X " + multiplier + " = " + "? input your answer ==> ");
answer = s.nextInt();
if (answer == number * multiplier)
{
System.out.print("You're right!");
}
else
{
System.out.print("Wrong! The right answer is " + number * multiplier + ".");
}
}
else
{
System.out.print("Error. Please try again.");
}
}
}
Verify input in two separate if/else blocks. Add this code to your "else" statement to catch strings other than "q".
else{
try {
int inputNumber = Integer.parseInt(input);
if(inputNumber == 0) {
// code..
} else if (inputNumber >= 2 && inputNumber <= 9) {
// code..
}
} catch(java.lang.NumberFormatException e) {
System.out.print("Error. Please try again.");
}
}
why don't use try catch?
if (input.equals("q")) {
System.out.print("quit the game.");
}
try {
Integer i = Integer.parseInt(input);
}catch (NumberFormatException e){
System.out.print("Error. Please try again.");
return;
}
This is the program that checks if the input integer is binary or not, now I need to create a loop that will prompt the user to renter integers until binary number is entered.
import java.util.Scanner;
public class BinaryNumbers {
public static void main(String[] args) {
int value, userValue;
int binaryDigit = 0, notBinaryDigit = 0;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter positive integers: ");
userValue = scan.nextInt();
value = userValue;
while (userValue > 0) {
if ((userValue % 10 == 0) || (userValue % 10 == 1)) {
binaryDigit++;
} else {
notBinaryDigit++;
}
userValue = userValue / 10;
}
if (notBinaryDigit == 0) {
System.out.println(value + " is a Binary Number.");
} else {
System.out.println(value + " is not a Binary Number.");
}
}
}
Why not using Regular Expressions?
All those checking on user inputs by assuming them as numbers (by calling Scanner#nextInt or Scanner#nextFloat or ...) are very breakable. How can be so sure user won't enter anything wrong?
It's better to hold the user input in a String variable and check it against being binary integer (which has to be 32 bit at most as defined in many languages such as java) using Regex is more safe:
import java.util.Scanner;
public class CheckBinaryInteger {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean isValid = false;
String userInput = "";
do {
System.out.print("Please Enter a binary integer: ");
userInput = sc.next();
isValid = userInput != null && !userInput.trim().isEmpty()
&& userInput.matches("[01]{1,32}");//assume every digit as one bit
if(!isValid)
System.out.println("invalid binary integer entered! ");
}while(!isValid);
System.out.println("Valid input: "+userInput);
}
}
Hope this helps.
import java.util.Scanner;
public class BinaryNumbers {
public static void main(String[] args) {
int value, userValue;
Scanner scan = new Scanner(System.in);
while(true){
int binaryDigit = 0, notBinaryDigit = 0;
System.out.println("Please enter positive integers: ");
userValue = scan.nextInt();
value = userValue;
while (userValue > 0) {
if ((userValue % 10 == 0) || (userValue % 10 == 1)) {
binaryDigit++;
} else {
notBinaryDigit++;
}
userValue = userValue / 10;
}
if (notBinaryDigit == 0) {
System.out.println(value + " is a Binary Number.");
return; //does the trick ;)
} else {
System.out.println(value + " is not a Binary Number.");
}
}
}
}
A simple return can end the program then and there :)
import java.util.Scanner;
class calculatePremium
{
public static void main(String[] args) {
int value, userValue;
int binaryDigit = 0, notBinaryDigit = 0;
Scanner scan = new Scanner(System.in);
while(true) /*use a while loop to iterate till you get the binary number*/
{
binaryDigit = 0; notBinaryDigit = 0;
System.out.println("Please enter positive integers: ");
userValue = scan.nextInt();
value = userValue;
while (userValue > 0) {
if ((userValue % 10 == 0) || (userValue % 10 == 1)) {
binaryDigit++;
} else {
notBinaryDigit++;
}
userValue = userValue / 10;
}
if (notBinaryDigit == 0) {
System.out.println(value + " is a Binary Number.");
break; /* breaks out of loop when gets the correct input */
} else {
System.out.println(value + " is not a Binary Number.\n");
}
}
}
}
You just need to use a loop till you get a binary number.
Hope it helps.
import java.util.Scanner;
public class BinaryNumbers {
public static void main(String[] args) {
int value, userValue;
int binaryDigit = 0, notBinaryDigit = 0;
Scanner scan = new Scanner(System.in);
while(true)
{
System.out.println("Please enter positive integers: ");
userValue = scan.nextInt();
value = userValue;
while (userValue > 0)
{
if ((userValue % 10 == 0) || (userValue % 10 == 1))
binaryDigit++;
else
notBinaryDigit++;
userValue = userValue / 10;
}
if (notBinaryDigit == 0)
{
System.out.println(value + " is a Binary Number.");
break;
}
else
System.out.println(value + " is not a Binary Number.");
}
}
}
I am a new human learning to code!
I had a problem with my Scanner, which is that I need it to 'reset' on an invalid character.
My code:
public class Lemonade {
static int m = 150;
private static Scanner scan;
public static void main(String[] args) {
int day = 1;
for(int gameover = m; gameover > 0; day++) {
int Random = (int) (Math.random() * 100);
if(Random <= 25) {
System.out.println("Great Chance!");
System.out.println("--------------------------------");
}
else if(Random <= 50) {
System.out.println("Good Chance!");
System.out.println("--------------------------------");
}
else if(Random <= 75) {
System.out.println("Bad Chance!");
System.out.println("--------------------------------");
}
else if(Random <= 100) {
System.out.println("Awful Chance!");
System.out.println("--------------------------------");
}
int count = 0;
int none = 0;
scan = new Scanner(System.in);
System.out.println("Enter a number between 0 and " + m + "!");
count = scan.nextInt();
if(count >= none && count <= m) {
System.out.println("You entered " + count + "!");
System.out.println("--------------------------------");
day = day + 1;
m = m - count;
System.out.println("Day " + day);
}
else {
System.out.println("Enter a number between 0 and " + m + ".");
count = scan.nextInt();
}
}
}
}
Now is my question how to get this to 'reset' on an invalid character like 'f', as Scanner only accepts numbers.
Thanks for the help!
If I understand you correctly then this is something you're looking for,
InputMismatchException will thrown if user enters invaild characters instead of int. You may use looping until the user enters an integer
import java.util.Scanner;
import java.util.InputMismatchException;
class Example
{
public static void main(String args[])
{
boolean isProcessed = false;
Scanner input = new Scanner(System.in);
int value = 0;
while(!isProcessed)
{
try
{
value = input.nextInt();
//example we will now check for the range 0 - 150
if(value < 0 || value > 150) {
System.out.println("The value entered is either greater than 150 or may be lesser than 0");
}
else isProcessed = true; // If everything is ok, Then stop the loop
}
catch(InputMismatchException e)
{
System.out.print(e);
input.next();
}
}
}
}
If this is not you're looking for please let me know!
import java.util.Scanner;
public class exam2015q2
{
public static void main (String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("Eneter a number");
int num = scan.nextInt();
int num1 = num-1;
int count = 0;
if(num % 2 == 0)
{
count++;
System.out.println(+num+ " Is an even number");
}
else
{
System.out.println(num+" Is an odd number");
}
boolean isPrime = true;
do {
if(num % num1 == 0)
{
isPrime = false;
break;
}
num1--;
}
while(num1 >= 2);
if(isPrime == true)
{
System.out.println(num +" is a prime number as it is only divisible by 1 and " +num);
}
else
{
System.out.println(num +" is NOT a prime number.It is divisible by " +count);
}
}
}
in this i need to output if its not a prime number then we have to output what number it is divisible by but i cant get that to print it out. i need help on this can someone give me suggestions on how to get the number for divisible by
public static void main (String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("Eneter a number");
int divisibleBy;
int num = scan.nextInt();
int num1 = num-1;
int count=0;
if(num%2==0)
{
count++;
System.out.println(+num+ " Is an even number");
}
else
{
System.out.println(num+" Is an odd number");
}
boolean isPrime = true;
do{
if(num%num1==0)
{
divisibleBy = num1;
isPrime = false;
break;
}
num1--;
}
while(num1>=2);
if(isPrime == true)
{
System.out.println(num +" is a prime number as it is only divisible by 1 and " +num);
}
else
{
System.out.println(num +" is NOT a prime number.It is divisible by " +divisibleBy);
}
}
Just change your statement showing the variable the number is divisible by from count to num1, since you determine in your do loop that num can be divided by num1 already.
else{
System.out.println(num +" is NOT a prime number.It is divisible by " +num1);
}
You can get rid of count entirely in your program as it is shown.