Java – How to wrap lines every n repetitions in a loop

How to wrap lines every n repetitions in a loop… here is a solution to the problem.

How to wrap lines every n repetitions in a loop

My code prints every number between the two limits and sums.
How can I print a new line every 10 digits?

for (int i = nedreGrense; i <= øvreGrense; i++) {           
        sum = sum + i;            
}
for (int tallStreng = nedreGrense; tallStreng < øvreGrense; tallStreng++){
    System.out.print(tallStreng+"+");
}
System.out.print(øvreGrense+"="+sum);

Solution

Because your restriction can be any integer,

if nedreGrense can be any integer,
Then after the nedreGrense is increased by 10, its number will not change, only one-tenth of it will change, to check we must use the % operator to give the number of numbers.

So use the condition if( (tallStreng != nedreGrense) &

& ((tallStreng - nedreGrense )%10 == 0)).

for (int i = nedreGrense; i <= øvreGrense; i++)
        sum = sum + i;    
    for (int tallStreng = nedreGrense; tallStreng < øvreGrense; tallStreng++){

if( (tallStreng != nedreGrense) && ((tallStreng - nedreGrense)%10 == 0))
          System.out.println();
        System.out.print(tallStreng+"+");
    }
    System.out.print(øvreGrense+"="+sum);

Related Problems and Solutions