Betran Gustama
Betran Gustama

Reputation: 25

How to Reverse the order?

How to reverse the number from 1-10 to 10-1

How to reverse the order of the loop and sum the even number only.

So it would look like this:

10+8+6+4+2=30

10+8+6+4=28

10+8+6=24

10+8=18

10=10

My code:


package javaapplication4;

public class NewClass7 {
   public static void main(String[] args) {
  int i=10,a; 
        for(int j=i; j>=1; j--) { 
            for(a=1; a<=i; a++)
                System.out.print(a + " + ");
            int n = 0;
           for(a = 1; a<=i; a++) { 
               n = n + a;
           }
           System.out.print(" = "+ n);
           System.out.println();
           i--; 
       } 
    } 
} 

Upvotes: 0

Views: 153

Answers (5)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79580

Shouldn't you solve it in a simple way like the one shown below?

public class Main {
    public static void main(String[] args) {
        // Define limits
        final int MAX = 10, MIN = 2;

        for (int i = MIN; i <= MAX; i += 2) {
            int sum = 0;
            for (int j = MAX; j >= i; j -= 2) {
                sum += j;
                // After getting decremented by 2 in each iteration, if the value of `j` has
                // become equal to that of `i`, print the value of `j` followed by '=' and the
                // value of `sum`; otherwise print the value of `j` followed by '+'
                System.out.print(j == i ? (j + "=" + sum) : j + "+");
            }
            System.out.println();
        }
    }
}

Output:

10+8+6+4+2=30
10+8+6+4=28
10+8+6=24
10+8=18
10=10

Upvotes: 1

Vyom Yadav
Vyom Yadav

Reputation: 145

I got the code for you for the Problem you asked me if we have to do 5+3+1=9 and so on. mind that this code will only work for odd numbers, for even numbers we would have to make a separate code. Hope it helps don't forget to tick the mark.

Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int p = n;
    int sum = 0;
    if (n % 2 != 0)
        for (int j = 1; j <= p; j = j + 2) {
            for (int i = n; i <= p && i > 0; i = i - 2) {
                System.out.print(i);
                if (i == 1) {
                    System.out.print("=");
                } else {
                    System.out.print("+");
                    sum += i;
                }
            }
            System.out.println(sum + 1);
            sum = 0;
            n = n - 2;
        }

Upvotes: 0

Andreas
Andreas

Reputation: 159215

Instead of counting up using for (int i = 1; i <= max, i++), count down using for (int i = max; i >= 1, i--).

Instead of counting up/down by 1 using i++/i--, count in steps of 2 using i += 2/i -= 2.

Upvotes: 0

Yukino
Yukino

Reputation: 57

I don't know whether I understand your question.

To my understand, you want a way to reserve the result order, you did it and want to know another way to reserve it. Here is my solution.

To reserve the result not need to reserve the order of compute. You can reserve the order of store them. For example, you can use the stack.like this:

int n = 10;
int sum = 0;
String str = "";
LinkedList<String> result = new LinkedList<>();
str = n + "";
sum += n;
result.push(str + "=" + sum);
for (int i = n - 2; i > 0; i-=2) {
    sum += i;
    str =str + "+" + i;
    result.push(str + "=" + sum);
}
for (String res : result) {
    System.out.println(res);
}

So use the data structure, you can use the result before, and then use one for loop to solve it. However, there's still some space for improvement. For example, you can try to use StringBuffer to replace String, or you can use array to replace LinkedList. Improve ourselves step by step. That's the fun of coding.

Upvotes: 0

rzwitserloot
rzwitserloot

Reputation: 103863

if x is a (non-floating-point) number, x % 2 == 0 returns true only if x is even. % is the modulo operator, basically: 'remainder': %2 means: Divide by 2, throw away the result, but what is the remainder? For even numbers, the remainder is always 0, which is why that works.

to reverse a loop, well, it's already in your code: for (int j=i; j>=1; j--) {} is a reversed loop, which starts with j at whatever i is (which is always 10 in that code), loops through it, then j is 9, loops through it, and it keeps going.. the last time it loops is when j is 1, as, when j is 0, j>=1 is no longer true.

If you can't finish the assignment with this information, you need to start over or ask for help from your TAs.

Upvotes: 0

Related Questions