Output sum of even numbers between two integers

10,997

Solution 1

I created the loopCounter variable to handle the required iterations without changing the values inputted by the user. The following changes have been made to your code.

Part A: added a while loop to validate user input. Also changed logic in if statement.

Part B: used one loop to print odd numbers and total even numbers

   //Part A
   int firstNum;
   int secondNum;
   int sumEven=0;

   System.out.println("Please enter an integer: ");
   firstNum = input.nextInt();

   System.out.println("Please enter another integer less than the first integer: ");
   secondNum = input.nextInt();

   //Part B

   //validate input in a loop
   while(true)
   {
     if (firstNum > secondNum)
     {
         System.out.print("Your second number is larger than the first.  So Please re-enter: ");
         secondNum = input.nextInt();
     }
     else
     {
         break;
     }
   }

       System.out.print("Odd Numbers: ");
       int loopCounter=firstNum;
       while(loopCounter<secondNum)
       {
         if (loopCounter%2!=0)
         {
             System.out.print(" " + loopCounter);
         }//end if
         else
         {
             sumEven+=loopCounter;
         }//end else
         loopCounter++;
       }

       System.out.println();
       System.out.print("Sum of Even Numbers: ");    
       System.out.print(sumEven);

   }

Solution 2

I would separate the two concerns of checking input and calculating the result. Here's how I would calculate it:

int sum = IntStream.rangeClosed(firstNum, secondNum).filter(i -> i % 2 == 0).sum();
Share:
10,997
daveskylark
Author by

daveskylark

...

Updated on June 17, 2022

Comments

  • daveskylark
    daveskylark almost 2 years

    I am working on a simple JAVA question in one of my college courses. I am stumped on this one program. I will display what I have so far and give the question I have to answer. I also looked at a similar question on StackOverflow, BUT it isn't the same problem so it DIDN'T help. The program I need to write is:

    Write a program that uses 'while' loops to perform the following steps:

    a.) Prompt the user to input two integers: 'firstNum' and 'secondNum' (firstNum must be less than secondNum)

    b.) Output all the odd numbers between 'firstNum' and 'secondNum' inclusive.

    c.) Output the sum of all the even numbers between 'firstNum' and 'secondNum' inclusive.

    This is what I have so far... (I still need to calculate the even numbers and sum them up)

    //import classes
    import java.util.*;
    
    public class chapter5no9
    {
        static Scanner console = new Scanner(System.in);
    
        public static void main(String[] args)
        {
    
        //Part A
        int firstNum;
        int secondNum;
        int sumEven;
    
        System.out.println("Please enter an integer: ");
        firstNum = console.nextInt();
    
        System.out.println("Please enter another integer less than the first integer: ");
        secondNum = console.nextInt();
    
        //Part B
        if (firstNum < secondNum)
        {
            System.out.print("Your second number is greater than the first.  So Please re-enter: ");
            secondNum = console.nextInt();
        }
        else
        {
            System.out.print("Odd Numbers: ");
            firstNum++;
            while (firstNum > secondNum)
            {
                if (secondNum % 2 != 0)
                {
                    System.out.print(" " + secondNum);
                }
                secondNum++;
            }
    
            System.out.println();
            System.out.print("Sum of Even Numbers: ");
            firstNum++;
            while (firstNum > secondNum)
            {
                if (secondNum % 2 != 0)
                {
                    System.out.print(" " + secondNum);
                }
                secondNum++;
            }
        }
    }
    

    }