Mike Chang
Mike Chang

Reputation: 19

Write a program that reads a set of integers and prints the sum of the even and odd integers

This is what I have so far. I am supposed to write this code with a For loop and if/else statement, but /i am stuck on how to do it properly. It would be nice if someone can tell me how to properly use a For loop and if/else statement together instead of giving the answer:

import java.util.*;

public class SumEvenOdd
{
    public static void main(String []args)
    {
        Scanner keyboard= new Scanner(System.in);

        int counter;
        int i= 0;
        int num=0;
        int sumOdd= 0;
        int sumEven= 0;

        System.out.println("Enter integers other then Zero: ");
        num=keyboard.nextInt();

        System.out.println("The numbers you entered are: ");

        for (i =num; i !=0; i=i)
                {
                    if (i % 2 == 0)
                        sumEven = sumEven + i;
                    else
                        sumOdd = sumOdd + i;
                    i = keyboard.nextInt();
                }
        System.out.println("Even sum: " + sumEven);
        System.out.println("Odd sum: " + sumOdd);
    }
}

Upvotes: 0

Views: 27507

Answers (5)

Nishkarsh Thakur
Nishkarsh Thakur

Reputation: 1

import java.util.Scanner;

public class Loop {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

// printing the sum of even and odd number input from the user

    int evensum = 0 ;
    int oddsum = 0 ;
    System.out.println("Enter the number:");
    
    for(int n1= sc.nextInt(); n1>0; n1=sc.nextInt()) {
        
        if(n1 % 2 == 0) {
            evensum+=n1 ;   // evensum = evensum + n1 ;
        }
        else {
            oddsum+=n1 ;    // oddsum = oddsum + n1 ;
        }
        
        System.out.println("Sum of even number is :"+evensum);
        System.out.println("Sum of odd number is :"+oddsum);
        
 // asking for continuing y or n?  

System.out.println("Do you want to continue ? yes = 1 or no = 0");
        
        int choice = sc.nextInt();
        if(choice==1) {
            System.out.println("Enter the number :");
        }
        else {
            System.out.println("End");
            break;
        }
    }
    

    System.out.println("Sum of even number is :"+evensum);
    System.out.println("Sum of odd number is :"+oddsum);
    

}

}

Upvotes: 0

AtharvATAR
AtharvATAR

Reputation: 11

If you want the answer in while loops

import java.util.*;

/*
EXPLANATION
Question: write a program that reads a set of integers and tells the sum of the even and odd numbers
First: Initialize 4 variables(all integers)
Second: Take input and print title
Third: Take a while loop that will run when i is smaller than a
Fourth: take inputs of the numbers and check for condition od or even and add the numbers
Fifth: Break the while loop if input = 
*/

public class EvenOddSum
{
    public static void main(String[]args)
    {
        //initializing variables
        int InputNums = 0, OddNums = 0, EvenNums = 0, loopingVar = 0, PrintAmount;

        //initializing scanner class
        Scanner scanner = new Scanner(System.in);

        //Input using Scanner
        System.out.print("How many numbers you want to input: ");
        PrintAmount = scanner.nextInt();

        //Loop to execute if PrintAmount is bigger than or equal to The loop Variable
        while(loopingVar <= PrintAmount)
        {
            //increase Loop Variable by 1 if it is smaller than PrintAmount
            loopingVar++;

            //The input which will be sorted into odd or even
            System.out.print("Please input a number : ");
            InputNums = scanner.nextInt();

            //Conditional statements to Sort Input into Odd or even
            if (InputNums % 2 == 0)
            {
                //store input numbers into OddNums var if it is not divisible by 2
                OddNums = OddNums + InputNums;
            }
            else
            {
                //store input numbers into EvenNums var if it is divisible by 2
                EvenNums = EvenNums + InputNums;
            }

            if(loopingVar == PrintAmount)
            {
                //If the loop variable is equal to the print amount the break will end the loop
                break;
            }
        }

        //if the condition is true the sums are printed and the code is stopped
        if (loopingVar == PrintAmount)
        {
            System.out.println("Sum of even numbers is : " + OddNums);
            System.out.println("Sum of odd numbers is : " + EvenNums);
            System.exit(0);
        }

        //if InputNums is smaller than 0 there has been some error in the code
        if (InputNums < 0)
        {
            System.out.print("Invalid input");
            System.exit(0);
        }
    }
}

Upvotes: 1

james
james

Reputation: 23

    Scanner input = new Scanner(System.in);
    int num;
    int i;
    int x = 0;
    int y = 0;
    System.out.print("How many numbers you want to input: ");
    i = input.nextInt();
    for (;;) {
        i--;
        System.out.print("Please input a number : ");
        num = input.nextInt();
        if (num % 2 == 0) {
            x = x + num;
        } else {
            y = y + num;
        }
        if (num < 0) {
            System.out.print("Inivald input");
            System.exit(0);
        }
        if (i == 0) {
            System.out.println("Sum of even numbers is : " + x);
            System.out.println("Sum of odd numbers is : " + y);
            System.exit(0);
        }
    }

Upvotes: 0

bwegs
bwegs

Reputation: 3767

Your loop never executes because your loop condition is false to begin with:

for (i =num; i !=0; i=i) // i already equals 0 so i != 0 equates to false

You also aren't incrementing or decrementing with i=i so even if your condition was true you'd be stuck in an infinite loop. Use i++ to increment the value of i by 1 in each iteration of your for loop.

Also, you're only taking in one number from the user. One simple way of handling this would be to first ask the user how many numbers they want to enter first, then use that input to loop that many times asking for the numbers to sum. For example:

System.out.println("How many numbers do you want to enter? ");
num=keyboard.nextInt();

int[] addThese = new int[num]; // create an array of size num to store numbers


for(int i = 0; i < num; i++) {
    System.out.print(": ");
    addThese[i] = keyboard.nextInt();
}

// now use your for loop to iterate over addThese[] and find your sums
...

EDIT

You've confused yourself (and me) with your print statements and lack thereof. Your program runs fine but I don't think you're realizing it because of this.

Add something like this inside your loop so you know it's waiting for input:

if (i % 2 == 0)
    sumEven = sumEven + i;
else
    sumOdd = sumOdd + i;

System.out.print(": ");  // <-- let the user know you're expecting more input   
i = keyboard.nextInt();

You can use an array like I used above to store the user input so you actually can tell the user what numbers they entered.

Upvotes: 3

Rami Del Toro
Rami Del Toro

Reputation: 1150

In your application you do not need a for loop as you are breaking the loop as long you dont enter 0.

for loops is used to iterate through collections (for each loop) or iteratively increment a counter till it satisfies the break(classic for loop).

A do while(i!=0) loop would be more appropriate in your scenario.

Upvotes: 1

Related Questions