Home > Back-end >  how to add all the numbers in a columns using for loop in java?
how to add all the numbers in a columns using for loop in java?

Time:11-30

I'm confused about the for loops in java i have trouble adding all the numbers in my columns like i want to put the sum of all squares in my total same as the cube like 4 16 36 64 100 = 220 same with the cube but I can't add them this is my work as for now

public class Main {
    public static void main(String[] args) {

        int ctr = 1;
        int number = 2, square = 2, cube;
        System.out.println("\nNumber\tSquare\tcube\n");

        for (int n = 2; ctr <= 5; ctr  ){

            number = ctr * n;
            square = number * number;
            cube = square * number;

            System.out.println(number   "\t"   square   "\t"   cube);
        }
        System.out.println("\nTotal\t");
    }
}

CodePudding user response:

The loop in java has 3 parts

1-> initialization (from where the loop should start)

2-> condition (when should loop stop its execution)

3-> iteration (how many steps should initializer takes)

int sum=0;
for(int i=0; i<4; i  ) {
   System.out.println(i);
   // if want to add 0 1 2 3 4
   sum  = i; // sum=sum   i
}

hope this will help you work with loops in future

CodePudding user response:

The for loop needs a bit of work.

A for loop has three parts to it:

  • initialization
  • condition
  • what to execute after each iteration

So to get the number, its square and its cube for all numbers from 2 to 5 included, you can write

System.out.println("\nNumber\tSquare\tcube\n");
for (int n = 2; n <= 5; n  ) {
    int sq = n * n;
    int cube = sq * n;
    System.out.println(n   "\t"   sq   "\t"   cube);
}

What happens is the following:

  • initialization: in the first block, int n = 2 is the integer variable n, which is initialized to 2. - condition: next, in the second block (the condition), check if n is less than or equal to 5. If it is, enter into the body of the loop
  • block of code in loop: define an integer for the square of n, which is n * n; define an integer for the cube of n in the same way; print the values
  • end of loop: execute whatever is in the execution block, in this case n to increment n.

Building on this, if you need the sum of everything, or the sum of all cubes, or the sum of all squares, you can add the variables as appropriate above the loop:

int sumN = 0;
int sumSq = 0;
int sumCube = 0;
System.out.println("\nNumber\tSquare\tcube\n");
for (int n = 2; n <= 5; n  ) {
    int sq = n * n;
    int cube = sq * n;
    sumN  = n;
    sumSq  = sq;
    sumCube  = cube;
    System.out.println(n   "\t"   sq   "\t"   cube);
}
System.out.println("sumN\t"   sumN);
System.out.println("sumSq\t"   sumSq);
System.out.println("sumCube\t"   sumCube);
// or the sum of everything, adding these three sums, if you need it

CodePudding user response:

I am not sure I 100% understand what you are asking. Here is a version of what I think you are asking with perfect squares only. You can use the same ideas to do the perfect cubes. Comments are in the code.

public class Main {
    public static void main(String[] args) {
        int totalSquare = 0;//Start the sum of the perfect squares at zero!

        System.out.println("\nNumber\tSquare\tTotal\n");

        for (int i = 2; i <= 5; i  ){//loop from 2 to 5
           int currentSquare = i * i;//get the square of i.
           totalSquare = totalSquare   currentSquare;//add the square of i to the totalSquare variable.        
           System.out.println(i   "\t"   currentSquare   "\t"   totalSquare);
        }
        System.out.println("\nTotal:\t"   totalSquare);
    }
}

Output:

Number  Square  Total

2       4       4
3       9       13
4       16      29
5       25      54

Perfect Square Total:   54

CodePudding user response:

public static void main(String[] args) {

        int ctr = 1;
        int number = 2, square = 2, cube;
        System.out.println("\n\tNumber\tSquare\tcube\n");
        int numberTotal=0, squareTotal=0, cubeTotal=0;
        for (int n = 2; ctr <= 5; ctr  ){

            number = ctr * n;
            numberTotal =number;
            
            square = number * number;
            squareTotal =square;
            
            cube = square * number;
            cubeTotal =cube;

            System.out.println("\t" number   "\t"   square   "\t"   cube);
        }
        System.out.println("\nTotal\t" numberTotal   "\t"   squareTotal   "\t"   cubeTotal);
        
    }

OUTPUT:

    Number  Square  cube

    2   4   8
    4   16  64
    6   36  216
    8   64  512
    10  100 1000

Total   30  220 1800
  •  Tags:  
  • java
  • Related