Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

ajprincess Group Title

Please help:) write a java program that reads two integers as start and last. within that ranges, it should differentiate ODD values and EVEN values and display the separated list on screen with the sum of ODD and sum of EVEN. the program will be repeated until the termination condition is given by the user(Hint:if both start and last integer are equal then the program will terminate)

  • one year ago
  • one year ago

  • This Question is Closed
  1. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    This is vat I hav done so far. import java.io.*; public class Problem_a { public static void main(String[]args) throws IOException { BufferedReader keyboard=new BufferedReader (new InputStreamReader(System.in)); int a_1;//First term int a_n;//Last term int n;//Number of odd and even numbers available within the range int odd; int even; int sum_1=0;//Sum of odd numbers int sum_2=0;//sum of even numbers System.out.print("Enter the first number:"); a_1=Integer.parseInt(keyboard.readLine()); System.out.print("Enter the last number:"); a_n=Integer.parseInt(keyboard.readLine()); n=((a_n-a_1)/2)+1; if(((a_1%2==1) && (a_n%2==0)) || ((a_1%2==0) && (a_n%2==1))) { for(int i=1;i<=n;i++) { odd=a_1+((i-1)*2); System.out.print(" "+odd+" "); sum_1+=odd; } System.out.print("\n"); System.out.println(sum_1); for(int j=1;j<=n;j++) { even=(a_1+((j-1)*2))+1; System.out.print(" "+even+" "); sum_2+=even; } System.out.print("\n"); System.out.print(sum_2); } else if(a_1%2==1 && a_n%2==1) { for(int i=1;i<=n;i++) { odd=a_1+((i-1)*2); System.out.print(" "+odd+" "); sum_1+=odd; } System.out.print("\n"); System.out.println(sum_1); for(int j=1;j<n;j++) { even=(a_1+((j-1)*2))+1; System.out.print(" "+even+" "); sum_2+=even; } System.out.print("\n"); System.out.print(sum_2); } else { for(int i=1;i<n;i++) { odd=a_1+((i-1)*2); System.out.print(" "+odd+" "); sum_1+=odd; } System.out.print("\n"); System.out.println(sum_1); for(int j=1;j<n;j++) { even=(a_1+((j-1)*2))+1; System.out.print(" "+even+" "); sum_2+=even; } System.out.print("\n"); System.out.print(sum_2); } } }

    • one year ago
  2. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    @Chris2332 please help me:)

    • one year ago
  3. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    @eSpeX please help.

    • one year ago
  4. CliffSedge Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Sorry, computer stuff is out of my range of expertise.

    • one year ago
  5. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    That's k. Thanx a lot for taking a look.:)

    • one year ago
  6. experimentX Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    what are you looking for? be concise ...

    • one year ago
  7. experimentX Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    ah ... my bad. I don't have jdk installed ... hold on for few min.

    • one year ago
  8. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    oh k:)

    • one year ago
  9. experimentX Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    I don't have fast connection ... perhaps it would take more than half hour ... can you summmarize what are you trying to do. I haven't programmed java for quite a long time.

    • one year ago
  10. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I need to display the odd numbers and the even numbers between two numbers and also their sum seperately. When I compile the above program the output comes right. bt i have to include a condition that the program will be repeated until the termination condition is given by the user(Hint:if both start and last integer are equal then the program will terminate)

    • one year ago
  11. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    I hav no idea what that condition will be.

    • one year ago
  12. experimentX Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    why not wrap the whole program in while loop?

    • one year ago
  13. experimentX Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    or ... set the condition true ... inside while loop. use if start == final ... break; inside while loop.

    • one year ago
  14. experimentX Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    hope this part is correct int sum_1=0;//Sum of odd numbers int sum_2=0;//sum of even numbers System.out.print("Enter the first number:"); a_1=Integer.parseInt(keyboard.readLine()); System.out.print("Enter the last number:"); a_n=Integer.parseInt(keyboard.readLine()); n=((a_n-a_1)/2)+1; if(((a_1%2==1) && (a_n%2==0)) || ((a_1%2==0) && (a_n%2==1))) { for(int i=1;i<=n;i++) { odd=a_1+((i-1)*2); System.out.print(" "+odd+" "); sum_1+=odd; } System.out.print("\n"); System.out.println(sum_1); for(int j=1;j<=n;j++) { even=(a_1+((j-1)*2))+1; System.out.print(" "+even+" "); sum_2+=even; } System.out.print("\n"); System.out.print(sum_2); } else if(a_1%2==1 && a_n%2==1) { for(int i=1;i<=n;i++) { odd=a_1+((i-1)*2); System.out.print(" "+odd+" "); sum_1+=odd; } System.out.print("\n"); System.out.println(sum_1); for(int j=1;j<n;j++) { even=(a_1+((j-1)*2))+1; System.out.print(" "+even+" "); sum_2+=even; } System.out.print("\n"); System.out.print(sum_2); } else { for(int i=1;i<n;i++) { odd=a_1+((i-1)*2); System.out.print(" "+odd+" "); sum_1+=odd; } System.out.print("\n"); System.out.println(sum_1); for(int j=1;j<n;j++) { even=(a_1+((j-1)*2))+1; System.out.print(" "+even+" "); sum_2+=even; } System.out.print("\n"); System.out.print(sum_2); }

    • one year ago
  15. experimentX Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    while(true) { // first of all input the integers if start == final break; } rewrite that whole thing inside this loop.

    • one year ago
  16. experimentX Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    gotta go out for a while ... brb in an hour.

    • one year ago
  17. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    ha k. Thanx a lot:)

    • one year ago
  18. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    import java.io.*; public class Problem_a { public static void main(String[]args) throws IOException { BufferedReader keyboard=new BufferedReader (new InputStreamReader(System.in)); int a_1;//First term int a_n;//Last term int n;//Number of odd and even numbers available within the range int odd; int even; int sum_1=0;//Sum of odd numbers int sum_2=0;//sum of even numbers while(true) { System.out.print("Enter the first number:"); a_1=Integer.parseInt(keyboard.readLine()); System.out.print("Enter the last number:"); a_n=Integer.parseInt(keyboard.readLine()); n=((a_n-a_1)/2)+1; if(((a_1%2==1) && (a_n%2==0)) || ((a_1%2==0) && (a_n%2==1))) { for(int i=1;i<=n;i++) { odd=a_1+((i-1)*2); System.out.print(" "+odd+" "); sum_1+=odd; } System.out.print("\n"); System.out.println(sum_1); for(int j=1;j<=n;j++) { even=(a_1+((j-1)*2))+1; System.out.print(" "+even+" "); sum_2+=even; } System.out.print("\n"); System.out.print(sum_2); } else if(a_1%2==1 && a_n%2==1) { for(int i=1;i<=n;i++) { odd=a_1+((i-1)*2); System.out.print(" "+odd+" "); sum_1+=odd; } System.out.print("\n"); System.out.println(sum_1); for(int j=1;j<n;j++) { even=(a_1+((j-1)*2))+1; System.out.print(" "+even+" "); sum_2+=even; } System.out.print("\n"); System.out.print(sum_2); } else { for(int i=1;i<n;i++) { odd=a_1+((i-1)*2); System.out.print(" "+odd+" "); sum_1+=odd; } System.out.print("\n"); System.out.println(sum_1); for(int j=1;j<n;j++) { even=(a_1+((j-1)*2))+1; System.out.print(" "+even+" "); sum_2+=even; } System.out.print("\n"); System.out.print(sum_2); } if (a_1 == a_n) { break; } } } } This is hw it comes right when I include while loop like u said? If I give the same value for first and last it displays the sums of previously done ones. How do I edit it?

    • one year ago
  19. experimentX Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    seems it is working

    • one year ago
  20. experimentX Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    put this thing right after you input it if (a_1 == a_n) { break; }

    • one year ago
  21. experimentX Group Title
    Best Response
    You've already chosen the best response.
    Medals 2

    import java.io.*; public class Test { public static void main(String[]args) throws IOException { BufferedReader keyboard=new BufferedReader (new InputStreamReader(System.in)); int a_1;//First term int a_n;//Last term int n;//Number of odd and even numbers available within the range int odd; int even; int sum_1=0;//Sum of odd numbers int sum_2=0;//sum of even numbers while(true) { System.out.print("Enter the first number:"); a_1=Integer.parseInt(keyboard.readLine()); System.out.print("Enter the last number:"); a_n=Integer.parseInt(keyboard.readLine()); if (a_1 == a_n) { break; } n=((a_n-a_1)/2)+1; if(((a_1%2==1) && (a_n%2==0)) || ((a_1%2==0) && (a_n%2==1))) { for(int i=1;i<=n;i++) { odd=a_1+((i-1)*2); System.out.print(" "+odd+" "); sum_1+=odd; } System.out.print("\n"); System.out.println(sum_1); for(int j=1;j<=n;j++) { even=(a_1+((j-1)*2))+1; System.out.print(" "+even+" "); sum_2+=even; } System.out.print("\n"); System.out.print(sum_2); } else if(a_1%2==1 && a_n%2==1) { for(int i=1;i<=n;i++) { odd=a_1+((i-1)*2); System.out.print(" "+odd+" "); sum_1+=odd; } System.out.print("\n"); System.out.println(sum_1); for(int j=1;j<n;j++) { even=(a_1+((j-1)*2))+1; System.out.print(" "+even+" "); sum_2+=even; } System.out.print("\n"); System.out.print(sum_2); } else { for(int i=1;i<n;i++) { odd=a_1+((i-1)*2); System.out.print(" "+odd+" "); sum_1+=odd; } System.out.print("\n"); System.out.println(sum_1); for(int j=1;j<n;j++) { even=(a_1+((j-1)*2))+1; System.out.print(" "+even+" "); sum_2+=even; } System.out.print("\n"); System.out.print(sum_2); } } } }

    • one year ago
  22. ajprincess Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Ya it works perfectly nw:D. Thank u sooooooooooo much.:D That was a bigg help.

    • one year ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.