## ajprincess 3 years ago 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)

1. ajprincess

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); } } }

2. ajprincess

3. ajprincess

4. CliffSedge

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

5. ajprincess

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

6. experimentX

what are you looking for? be concise ...

7. experimentX

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

8. ajprincess

oh k:)

9. experimentX

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.

10. ajprincess

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)

11. ajprincess

I hav no idea what that condition will be.

12. experimentX

why not wrap the whole program in while loop?

13. experimentX

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

14. experimentX

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); }

15. experimentX

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

16. experimentX

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

17. ajprincess

ha k. Thanx a lot:)

18. ajprincess

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?

19. experimentX

seems it is working

20. experimentX

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

21. experimentX

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); } } } }

22. ajprincess

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

Find more explanations on OpenStudy