1383 Shares

# Can someone help me get started on this Java program?

Topic: Java case string
July 21, 2019 / By Albin
Question: The program is as follows: Write a Java program that reads an integer value from the user and displays: 1. The sum of all even integers between 1 and the input value, both inclusive. 2. The sum of all odd integers between 1 and the input value, both inclusive Example: User enters 7. Sum of even integers = 2+4+6 = 12 Sum of odd integers = 1+3+5+7=16 I think I'm supposed to use a "for looping repetition structure" or am I wrong? I have an idea for displaying the even numbers, it should be something like this right?: for (i = 0; i < *user entered integer*; i = i +2) System.out.print(i + " "); I have no idea what to do for displaying the odd numbers, and I'm not even sure what I would need to write to get the sums of the even and odd numbers. Can someone help, I'm really lost with the whole thing? Thank you guys for your help. This is the code for the program: import java.util.*; public class evenOdd { static Scanner console = new Scanner(System.in); public static void main(String[] args) { int inputNum; int sumEven = 0; int sumOdd = 0; int i; System.out.println("Enter a number"); inputNum = console.nextInt(); for(i = 1; i <= inputNum; i++) { if (i % 2 == 0) { sumEven += i; } else { sumOdd += i; } } System.out.print("The sum of the even integers = "); for (i = 1; i <= inputNum; i++) { if (i % 2 == 0) System.out.print(i + " + "); } System.out.println("= " + sumEven); System.out.print("The sum of the odd integers = "); for (i = 1; i <= inputNum; i++) { if (i % 2 != 0) System.out.print(i + " + "); } System.out.println("= " + sumOdd); } } This is the result after the program is executed: Enter a number 7 The sum of the even integers = 2 + 4 + 6 + = 12 The sum of the odd integers = 1 + 3 + 5 + 7 + = 16 Is there a way to get rid of the extra "+" after 6 and 7?

## Best Answers: Can someone help me get started on this Java program?

Sydne | 8 days ago
EDIT: http://pastebin.com/f13483554 ======================================... Here's the code. import java.io.*; public class OddEven { public static void main(String args[])throws IOException { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); int n,i,oddSum=0,evenSum=0; char a='\b'; System.out.println("Enter the number: "); n=Integer.parseInt(in.readLine()); for(i=1;i<=n;i++){ if(i%2==0){ evenSum+=i; } else{ oddSum+=i; } } System.out.print("Sum of even integers = "); for(i=1;i<=n;i++){ if(i%2==0) System.out.print(i+" + "); } System.out.print(a); System.out.print(a); System.out.print("="+evenSum); System.out.println(); System.out.print("Sum of odd integers = "); for(i=1;i<=n;i++){ if(i%2!=0) System.out.print(i+" + "); } System.out.print(a); System.out.print(a); System.out.print("="+oddSum); } } Here's the code, in case some part of the code gets truncated on Yahoo Answers.http://pastebin.com/f78e46c5a Hope this helps. Let me know if you have any queries. Have fun :)
👍 224 | 👎 8
Did you like the answer? Can someone help me get started on this Java program? Share with your friends

We found more questions related to the topic: Java case string

Originally Answered: Can someone help me get started on this Java program?
EDIT: http://pastebin.com/f13483554 ======================================... Here's the code. import java.io.*; public class OddEven { public static void main(String args[])throws IOException { BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); int n,i,oddSum=0,evenSum=0; char a='\b'; System.out.println("Enter the number: "); n=Integer.parseInt(in.readLine()); for(i=1;i<=n;i++){ if(i%2==0){ evenSum+=i; } else{ oddSum+=i; } } System.out.print("Sum of even integers = "); for(i=1;i<=n;i++){ if(i%2==0) System.out.print(i+" + "); } System.out.print(a); System.out.print(a); System.out.print("="+evenSum); System.out.println(); System.out.print("Sum of odd integers = "); for(i=1;i<=n;i++){ if(i%2!=0) System.out.print(i+" + "); } System.out.print(a); System.out.print(a); System.out.print("="+oddSum); } } Here's the code, in case some part of the code gets truncated on Yahoo Answers.http://pastebin.com/f78e46c5a Hope this helps. Let me know if you have any queries. Have fun :)
Originally Answered: Can someone help me get started on this Java program?
int evens = 0; int odds = 0; int input = 15; for ( int i = 1; i <= input; i++ ) { ..... if ( i % 2 == 0 ) { .......... evens++; ..... } else { .......... odds++; ..... } } System.out.println( "Evens: " + evens + ", Odds: " + odds );

Rebecca
int evens = 0; int odds = 0; int input = 15; for ( int i = 1; i <= input; i++ ) { ..... if ( i % 2 == 0 ) { .......... evens++; ..... } else { .......... odds++; ..... } } System.out.println( "Evens: " + evens + ", Odds: " + odds );
👍 90 | 👎 7