In: Computer Science
Help, please!
a)
int sum = 0;
for(int i =2;i<=100;i+=2) {
sum+=i;
}
b)
int sum = 0;
for (int i = 1; i <= 100; i += 1) {
sum += i *
i;
}
c)
Scanner scan = new Scanner(System.in);
int sum = 0;
// Take input
int a = scan.nextInt();
int b = scan.nextInt();
// Iterate loop from a to b
for (int i = a; i <= b; i++)
{
// If number is
not divisible by 2 means it's odd
// Add it to
sum
if (i % 2 != 0)
{
sum += i;
}
}
a)
This loop will run 10 times from 1 to 10
b)
This loop will run 10 times from 0 to 9
c)
This loop will run 10 times from 10 to 1
d)
This loop will run 21 times from -10, -9,-8......,-1,0,1,2.....,10
e)
This loop will run 11 times from -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10