Java: im using a loop to print the sum of odd numbers, of an int = i,?
-
Right now my program runs as enter a number: 7 1 + 3 + 5 + 7 + = 16 i need it to run as: ex. enter a number: 7 1+3+5+7=7+5+3+1 i can't figure out how to stop the loop from printing the extra "+" sign and then need to print the function in reverse order. would really appreciate some insight. here is my current code import java.io.*; import java.util.*; public class sumofoddshowloop{ public static void main(String[] args){ System.out.println("Enter a number"); Scanner sc=new Scanner(System.in); int input=sc.nextInt(); int i=0; int sum=0; while (i<input){ i++; if(i%2==1){ sum=i+sum; System.out.print(i + " + ");} } System.out.println(" = " + sum); } }
-
Answer:
Well, where you typed your "if" block, I would replace: if ( ( i == input ) && ( i % 2 == 1 ) System.out.print(i); else ( i % 2 == 1 ) System.out.print(i + " + "); This way the last number, if divisible by 2, will print only the number and not the +. For any other number, as long as it is divisible by 2, the + will be printed. EDIT: The above solution by The Phlebob is probably an easier way
Jere Kane at Yahoo! Answers Visit the source
Other answers
The usual way to handle this is to determine whether a + is needed before putting one out. Since there's no way of telling whether a number is the last number in the sequence until it's too late, you have to go the other direction and put out a + before any number that's not the first number in the sequence. Code that and it will solve your problem. As for the reversal, why not just run through the numbers a second time from high to low?
Dont print directly. put as String values.The following coding will guide you. String Output[]=new String(100); int Count=0; within if(i%2) { Output[Count]=i+"+"; Count=Count+1; } After that , Remove the last '+' Sign int lastIdx = Count- 1;//Get the last index String OriginalSt = Output.substring(0, lastIdx-1);// Now Remove the last '+' Sign //Reverse the String String ReverseSt = new StringBuffer(OriginalSt).reverse().toStr… //Now Print System.out.println( OriginalSt +"="+ReverseSt ); i hope it will help you
To stop the extraneous plus sign: The non-hackish way to do this is to test to see if you're on the last iteration. If you are, don't print out the "+" sign, because you'd only want to print it out if you AREN'T on the last iteration. Given that you know the exit condition, it's relatively easy to determine when you're on the last iteration (it'll just be when i == input or i == input-1). The hackish way to do this is to store the output to a string, and then output the string in one go. That is, instead of: while (...) { out.print(...); } Do: String output = ""; while (...) { output += ...; } out.println(output); For the second approach, right before the print statement, you can truncate the string's last two characters (which will be the space and plus sign). In order to print out the numbers in reverse, you'd basically do the same thing, but run the loop the other way (starting from the top and iterating down to 0).
Just add if else condition after sum = i + sum; sum=i+sum; if(i < input) {System.out.print(i + " + ");} else {System.out.print(i);} -Sany
Related Q & A:
- How can I communicate Raspberry Pi and Arduino (in both ways) using a 10-15m distance wires?Best solution by Arduino
- In Visual Studio 2012 or 2013, how can I register a DLL file on Windows 7 64-bit computer using a Custom Action command?Best solution by Stack Overflow
- How do I upload a picture instead of using a generic avatar?Best solution by answers.yahoo.com
- How do i scan something? First time using a scaner and i dont think im doing it right. help. thanks?Best solution by Yahoo! Answers
- How can I play my PS3 using a laptop as a monitor?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.