package Alternate;
import javax.swing.JOptionPane;
public class Add {
public static void main(String[] args){
int sum = 0;
for(int i = 1; i <= 100; i ) {
sum = sum i;
}
JOptionPane.showMessageDialog(null, "Sum : " sum);
}
}
Here is what I have so far. Only one for loop is allowed and 3 variables.
It must be alternately from number 1-100 (add 10 numbers, skip 10 numbers, add 10 numbers, skip 10 numbers again, until number 100) . The sum total should be 2275.
CodePudding user response:
You can skip i
by 11 when i % 10 == 0
.
Demo:
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i ) {
sum = sum i;
if (i % 10 == 0)
i = 11;
}
JOptionPane.showMessageDialog(null, "Sum : " sum);
}
}