1383 Shares

Can someone help me get started on this Java program?

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 Answer

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

Sydne 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


Sydne 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 :)
Sydne 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 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

Rebecca Originally Answered: Java Program about Vowels just need minor help?
I just rearranged the while loop Here's the modified code: public class CountVowelsAndConsonents { public static void main(String[] args) { while(true){ Scanner input = new Scanner(System.in); System.out.print(" Please input a string: "); String str = input.nextLine(); if(str.equals("e")) break; else countVowelConsCount(str); } } public static void countVowelConsCount(String s){ String vowels = "aeiou"; int vowelCount=0; int consCount=0; char str[] = s.toLowerCase().toCharArray(); for(char c1:str){ if(vowels.indexOf(c1)!=-1) { vowelCount++; } else{ consCount++; } } System.out.println("Total Amount of Vowels: "+vowelCount); System.out.println("Total Amount of Consonants: "+consCount); } }
Rebecca Originally Answered: Java Program about Vowels just need minor help?
i don't think of it concerns in java yet you will possibly often want a tail-recursive function (merely nutrients for theory): function countVowels(String s, int numVowels)   IF length(s) == 0     return numVowels   ELSE     IF first char is vowel       countVowels(relax(s), numVowels + a million)     ELSE       countVowels(relax(s), numVowels) In my pseudocode, "relax()" returns each and every thing after the 1st character interior the string (in Java it quite is something like substring(a million,s.length())). the belief is that once the recursive call to countVowels returns we don't could do any further artwork. a smart compiler can convert this into very useful merchandise code.

If you have your own answer to the question java case string, then you can write your own version, using the form below for an extended answer.