How to write an even or odd program in java? - java

My instructions are "Write a program that prompts the user for a number, then counts up (a ‘for’ loop) from one to that number and prints whether that loop number is even or odd (which will require an ‘if-else’ structure inside the loop)." So it needs to list:
1 is odd
2 is even
3 is odd...
public class AssmtEvenOrOddJulianP {
public static void main(String[] args) {
//variable
int num = 0;
//input
System.out.print("\nEnter a number less than 100: ");
num = Expo.enterInt();
//output
for (int i = 1; i <= num; i++)
if ((num % 2) == 0)
System.out.print("\n" + i + " Is Even");
else if ((num % 2) >= 0)
System.out.print("\n" + i + " Is Odd");
Right now if I input 3 it will print:
1 is odd
2 is odd
3 is odd

Minor mistake:
You should calculate the remainder of i by 2, not num by 2.
Always wrap for and if/else blocks in curly braces:
for (int i = 1; i <= num; i++) {
if ((i % 2) == 0) {
System.out.print("\n" + i + " Is Even");
} else if ((num % 2) >= 0) {
System.out.print("\n" + i + " Is Odd");
}
}
Avoid using redundant parantheses:
for (int i = 1; i <= num; i++) {
if (i % 2 == 0) {
System.out.print("\n" + i + " Is Even");
} else if (num % 2 >= 0) {
System.out.print("\n" + i + " Is Odd");
}
}
The else if condition has a minor bug that is "unreachable" right now, but could cause pain in the future
num % 2 >= 0 should be i % 2 < 0 || i % 2 > 0
The else if condition can be simplified to else:
for (int i = 1; i <= num; i++) {
if (i % 2 == 0) {
System.out.print("\n" + i + " Is Even");
} else {
System.out.print("\n" + i + " Is Odd");
}
}
Final result:
With some other minor improvements:
public class EvenOdd {
public static void main(String[] args) {
// input
System.out.print("\nEnter a number less than 100: ");
// variable
int num = Expo.enterInt();
System.out.println();
// output
for (int i = 1; i <= num; i++) {
if (i % 2 == 0) {
System.out.println(i + " Is Even");
} else {
System.out.println(i + " Is Odd");
}
}
}
}

The Following program will help you . for odd and Even number we need to divide by 2 and if number is divisible by 2 then number is Even Number (in this case reminder will be 0) and if the reminder is 1 then its Odd Number
public class EvenAndOddNumber {
public static void main(String[] args) {
System.out.println("Enter the number");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
for (int i = 1; i <= num; i++) {
if (i % 2 == 0) {
System.out.println(i + " is even number");
} else {
System.out.println(i + " is odd number");
}
}
}
}

Related

Issue with Prime/Composite Java program [duplicate]

This question already has answers here:
Computing prime numbers up to N integers
(6 answers)
Calculating and printing the nth prime number
(11 answers)
Closed last year.
I am new to Java and am currently trying to create a program that will determine if the entered number is prime or composite. I've figured out most of it, but there's one part that has me stumped.
I want the code to print a message stating either "the number needs to be greater than 1" if a 0 or a negative number is entered, or "one is neither prime nor composite" if a 1 is entered.
Here's my code:
public class Main {
public static void main(String[] args) {
int num;
boolean prime = true;
String answer;
Scanner keyboard = new Scanner(System.in);
do {
System.out.print("Please enter a number ==> ");
num = keyboard.nextInt();
if (num < 1)
System.out.println("The number entered needs to be greater than one.");
else if (num == 1)
System.out.println("One is neither a prime nor composite number.");
else {
for (int ii = 2; ii <= num/2; ii++) {
if (num % ii == 0) {
prime = false;
break;
}
}
}
if (prime == true)
System.out.println("The integer " + num + " is prime.");
else {
System.out.println("The integer " + num + " is composite.");
}
System.out.print("Would you like to go again? (yes/no) ");
answer = keyboard.next();
System.out.print("\n");
} while (answer.equals("yes"));
}
}
My issue is that when I run the code and enter a 0, 1, or negative number, the code prints the correct message but adds "The integer "0, 1, -#" is prime."
I don't want it to do so, and only want it to print the corresponding message. What am I missing?
This code is executing no matter the value of num:
if (prime == true)
System.out.println("The integer " + num + " is prime.");
else {
System.out.println("The integer " + num + " is composite.");
}
To fix you can just move this inside the else statement
if (num < 1)
System.out.println("The number entered needs to be greater than one.");
else if (num == 1)
System.out.println("One is neither a prime nor composite number.");
else {
for (int ii = 2; ii <= num/2; ii++) {
if (num % ii == 0) {
prime = false;
break;
}
}
if (prime == true)
System.out.println("The integer " + num + " is prime.");
else {
System.out.println("The integer " + num + " is composite.");
}
}
Just move the print statements inside else to fix the logic.
Also, some more improvements are possible to exclude even numbers except 2, and look for the primes until a square root of num is achieved.
if (num < 1)
System.out.println("The number entered needs to be greater than one.");
else if (num == 1)
System.out.println("One is neither a prime nor composite number.");
else {
prime = num % 2 == 1 || num == 2;
for (int ii = 3; prime && ii * ii <= num; ii += 2) {
if (num % ii == 0) {
prime = false;
}
}
if (prime)
System.out.println("The integer " + num + " is prime.");
else {
System.out.println("The integer " + num + " is composite.");
}
}
The reason it is printing the statements is because the if piece of code will always run in case the value is less than 1 ( as you have already initialized prime boolean to be true ). A fix to this to to move the last if statement inside the else part.
public class Main {
public static void main(String[] args) {
int num;
boolean prime = true;
String answer;
Scanner keyboard = new Scanner(System.in);
do {
System.out.print("Please enter a number ==> ");
num = keyboard.nextInt();
if (num < 1)
System.out.println("The number entered needs to be greater than one.");
else if (num == 1)
System.out.println("One is neither a prime nor composite number.");
else {
for (int ii = 2; ii <= num / 2; ii++) {
if (num % ii == 0) {
prime = false;
break;
}
}
if (prime == true)
System.out.println("The integer " + num + " is prime.");
else {
System.out.println("The integer " + num + " is composite.");
}
}
System.out.print("Would you like to go again? (yes/no) ");
answer = keyboard.next();
System.out.print("\n");
} while (answer.equals("yes"));
}
}
then it does not give the additional print statement as earlier.
Please enter a number ==> 1
One is neither a prime nor composite number.
Would you like to go again? (yes/no)

How possible to display the 2 digit sum of the inputted number (prime numbers)

It will only accept a non-negative even number (higher than 2) and extract the 2 prime numbers. Here is the sample output:
Enter the number: 20
The twin prime numbers of 20 are: 17 and 3 / 13 and 7
(17 + 3 = 20 and 13 + 7 = 20 also) But 17,3,13, and 7 is a prime number. I don't know how to do it, please, thankyou!
But here is my output, my output is just to print all twin prime between 3-20 (that is inputted number)
run:
Enter the number: 20
The twin prime numbers of 20 are:
3 / 5
5 / 7
11 / 13
17 / 19
I don't know how to do the specific problem.
Here is my code btw:
import java.util.Scanner;
class Prime {
boolean prime(int a) {
int count = 0;
for (int x = 1; x <= a; x++) {
if (a % x == 0) {
count++;
}
}
switch (count) {
case 2:
return true;
default:
return false;
}
}
public static void main(String args[]) {
Prime twin = new Prime();
Scanner sc = new Scanner(System.in);
int higherThan2 = 3;
System.out.print("Enter the number: ");
int number = sc.nextInt();
if (higherThan2 >= number) {
System.out.println("Must be higher than 3!");
} else {
System.out.println("\nThe twin prime numbers of " + number + " are: ");
for (int i = higherThan2; i <= (number - 2); i++) {
if (twin.prime(i) == true && twin.prime(i + 2) == true) {
System.out.print(i + " / " + (i + 2) + "\n");
}
}
}
}
}
You can try this. It works, but I don't think it's the best
System.out.println("\nThe twin prime numbers of " + number + " are: ");
for(int i = higherThan2; i < number; i++)
for(int j = number; j > higherThan2; j--)
if(twin.prime(i) && twin.prime(j) && i + j == number)
System.out.print(i + " / " + j + "\n");

How to make hollow box with increasing and decreasing number at the side?

I have attempted a java program that includes hollow in the middle and numbers decrease and increase at the sides based on user's input.
Let say the user's input is 4, so the output should be like this:-
1 2 3 4
2 3
3 2
4 3 2 1
and i managed to do it until its like this:-
1 2 3 4
2
3
4 3 2 1
by using this code:
import java.util.*;
public class Exercise2 {
public static void main(String args[])
{
System.out.print("Enter an integer >0 and <21: ");
Scanner input = new Scanner (System.in);
int num = input.nextInt();
for (int i = 0; i < num; i++)
{
System.out.print((i+1) + " ");
}
System.out.println();
for (int j = 1; j < num-1; j++)
{
System.out.println((j+1) + " ");
}
// for (int j = num-1; j > 1; j--)
// {
// System.out.print(j + " ");
// }
for (int j = num; j > 0; j--)
{
System.out.print((j) + " ");
}
System.out.println();
if (num > 21 || num < 0)
{
System.out.println("Wrong number range");
}
}
}
I dont know how to do the other side of it to become 4 3 2 1 downwards. I tried uncommenting the commented codes and it gave 3 2 which is what i wanted but not in a straight line. I dont know where my error is. And i dont how to insert blank space (System.out.print(" ")) so that the hollow is actually something printed, not just blank.
Here's one way of doing it:
for (int i = 0; i < num; ++i) {
for (int j = 0; j < num; ++j) {
if (i == 0) {
System.out.print((j + 1) + " ");
} else if (i == num-1) {
System.out.print((num - j) + " ");
} else if (j == 0) {
System.out.print((i + 1) + " ");
} else if (j == num-1) {
System.out.print((num - i) + " ");
} else {
System.out.print(" ");
}
}
System.out.println();
}

How do I terminate this loop?

"Only one while loop should be used to determine all even and odd numbers between 50 and 100."
public class EvenOdd {
public static void main(String args[]) {
int x = 50;
int y = 50;
int i = 0;
int j = 0;
int n = 0;
System.out.print("Even numbers between 50 and 100: ");
while((i != x) || (j != y)) {
n++;
System.out.print(i + x + ", ");
i += 2;
if(i != x)
continue;
System.out.println("100");
System.out.print("\nOdd numbers between 50 and 100: ");
System.out.print((j+1) + y + ", ");
j += 2;
if(j != y)
continue;
}
}
}
The evens print fine but the odds continue on forever. This may be a dumb question, but I'm having the biggest brainfart right now, and I would really appreciate help on this.
Simply try this and let me know. It's based on your code, with just a few minor adjustments:
public class Teste4 {
public static void main(String args[]) {
int x = 50;
int y = 50;
int i = 0;
int j = 1;
int n = 0;
System.out.print("Even numbers between 50 and 100: ");
while((i < x) || (j < y)) {
if(i < x){
System.out.print(i + x + ", ");
i += 2;
continue;
}else if(i == x){
System.out.println("100");
i++;
}
if(j == 1){
System.out.print("\nOdd numbers between 50 and 100: ");
}
System.out.print(j + y + ", ");
j += 2;
}
}
}
Perhaps you should try this
int even_counter = 50;
int odd_counter=51;
System.out.println("Even numbers between 50 and 100: ");
while((even_counter < 100 ) || (odd_counter < 100)){
if(even_counter < 100){
System.out.print(even_counter+ " ");
even_counter+=2;
continue;
}
if(odd_counter < 100){
if(odd_counter == 51){
System.out.println("\nOdd numbers between 50 and 100: ");
}
System.out.print(odd_counter+ " ");
odd_counter+=2;
}
}
I'm sure this is an assignment but i'll add my 2 cents since it's solved. This one will give you an array.
final int EVEN = 0, ODD = 1;
int low = 50, high = 100, current = low;
int[][] numbers = new int[2][];
numbers[EVEN] = new int[((high - low) / 2) + ((high - low) % 2)];
numbers[ODD] = new int[((high - low) / 2)];
while(current < high){
numbers[current % 2][(current - low) / 2] = current++;
}
System.out.println("EVEN" + Arrays.toString(numbers[EVEN]));
System.out.println("ODD " + Arrays.toString(numbers[ODD]));
Alternative approach ,
int current = 50 , end = 100 ;
String odd , even ;
while(current <= 100){
if (current % 2 == 0)
even = even.concat ("," + current);
else
odd = odd.concat("," + current);
current++;
}
System.out.println("Even no are : " + even);
System.out.println("Odd no are : " + odd);
I dont have a compiler now . I think this should be right :) .

Putting numbers in specific ranges giving out weird output

I'm trying to put my generated numbers into a specific range but it keeps giving me this output:
200 Generated numbers, 10 per line:
99 Numbers from 0-20: 1
Numbers from 20-40: 0
Numbers from 40-60: 0
Numbers from 60-80: 0
Numbers from 80-100: 0
18 Numbers from 0-20: 2
Numbers from 20-40: 0
Numbers from 40-60: 0
Numbers from 60-80: 0
Numbers from 80-100: 0
56 Numbers from 0-20: 3
Here is my current code:
import java.util.Random;
public class RandomStats2 {
public static void main(String[] args) {
Random random = new Random ();
int[] num = new int [5];
System.out.println("200 Generated numbers, 10 per line: ");
int numbersOnThisLine = 0;
for(int i = 0; i < 200; i++){
int nextNumber = random.nextInt(101);
System.out.print(nextNumber + " ");
numbersOnThisLine++;
if (numbersOnThisLine == 10){
System.out.print("\n");
numbersOnThisLine = 0;
}
if(i <=21) num[0]++;
else if(i <=41) num[1]++;
else if(i <=61) num[2]++;
else if(i <=81) num[3]++;
else if(i <=100) num[4]++;
System.out.println("Numbers from 0-20: " + num[0]);
System.out.println("Numbers from 20-40: " + num[1]);
System.out.println("Numbers from 40-60: " + num[2]);
System.out.println("Numbers from 60-80: " + num[3]);
System.out.println("Numbers from 80-100: " + num[4]);
}
}
}
How can I fix this? Also, how would I set a range like 41-60 in the else if statements?
You want to check you nextNumber so I think you want this:
if(nextNumber <21){ num[0]++;}
else if(nextNumber <41){ num[1]++;}
else if(nextNumber <61){ num[2]++;}
else if(nextNumber <81){ num[3]++;}
else if(nextNumber <101){ num[4]++;}
Also, use these {} whenever you are working with if-else.
Second, print after the loop ended.
Are you really supposed to do all of that inside of the for loop? I am not sure of what you are trying to accomplish, but a wild suggestion is to print it out outside of the loop.
Right now you are printing what you have found for every random integer.
Looking at your code, it looks like you want to print out the ranges every 10th iteration. If so, move your print statements inside the if statement as below.
import java.util.Random;
public class RandomStats2 {
public static void main(String[] args) {
Random random = new Random ();
int[] num = new int [5];
System.out.println("200 Generated numbers, 10 per line: ");
int numbersOnThisLine = 0;
for(int i = 0; i < 200; i++){
int nextNumber = random.nextInt(101);
System.out.print(nextNumber + " ");
numbersOnThisLine++;
if (numbersOnThisLine == 10){
System.out.print("\n");
numbersOnThisLine = 0;
System.out.println("Numbers from 0-20: " + num[0]);
System.out.println("Numbers from 20-40: " + num[1]);
System.out.println("Numbers from 40-60: " + num[2]);
System.out.println("Numbers from 60-80: " + num[3]);
System.out.println("Numbers from 80-100: " + num[4]);
}
if(i <=21) num[0]++;
else if(i <=41) num[1]++;
else if(i <=61) num[2]++;
else if(i <=81) num[3]++;
else if(i <=100) num[4]++;
}
}
}
your statistics needs to be out of the for loop like this:
import java.util.Random;
public class RandomStats2 {
public static void main(String[] args) {
Random random = new Random ();
int[] num = new int [5];
System.out.println("200 Generated numbers, 10 per line: ");
int numbersOnThisLine = 0;
for(int i = 0; i < 200; i++){
int nextNumber = random.nextInt(101);
System.out.print(nextNumber + " ");
numbersOnThisLine++;
if (numbersOnThisLine == 10){
System.out.print("\n");
numbersOnThisLine = 0;
}
if(i <=21) num[0]++;
else if(i <=41) num[1]++;
else if(i <=61) num[2]++;
else if(i <=81) num[3]++;
else if(i <=100) num[4]++;
}
System.out.println("Numbers from 0-20: " + num[0]);
System.out.println("Numbers from 20-40: " + num[1]);
System.out.println("Numbers from 40-60: " + num[2]);
System.out.println("Numbers from 60-80: " + num[3]);
System.out.println("Numbers from 80-100: " + num[4]);
}
}
Also for the if statements i think you meant to use nextNumberand not i, since i is you loop counter
Moving the calls to System.out.println() outside of your for loop will resolve the issue with the repeated output. Also, your current if and else if statements are not checking for the right values. Try this:
if (nextNumber <= 20) num[0]++;
else if (nextNumber <= 40) num[1]++;
else if (nextNumber <= 60) num[2]++;
else if (nextNumber <= 80) num[3]++;
else if (nextNumber <= 100) num[4]++;

Categories

Resources