vendredi 21 juin 2019

How to write an even or odd program in 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

Aucun commentaire:

Enregistrer un commentaire