Ask your own question, for FREE!
Computer Science 14 Online
OpenStudy (ajprincess):

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)

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

OpenStudy (ajprincess):

@Chris2332 please help me:)

OpenStudy (ajprincess):

@eSpeX please help.

OpenStudy (anonymous):

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

OpenStudy (ajprincess):

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

OpenStudy (experimentx):

what are you looking for? be concise ...

OpenStudy (experimentx):

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

OpenStudy (ajprincess):

oh k:)

OpenStudy (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.

OpenStudy (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)

OpenStudy (ajprincess):

I hav no idea what that condition will be.

OpenStudy (experimentx):

why not wrap the whole program in while loop?

OpenStudy (experimentx):

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

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

OpenStudy (experimentx):

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

OpenStudy (experimentx):

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

OpenStudy (ajprincess):

ha k. Thanx a lot:)

OpenStudy (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?

OpenStudy (experimentx):

seems it is working

OpenStudy (experimentx):

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

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

OpenStudy (ajprincess):

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

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!