Write a program find out and Print the following:
1. Sum of all even numbers between 1 to 20;
2. Sum of all odd number between 1 to 20.
Answers
The given problem is solved using language - Java.
public class Brainly{
public static void main(String args[]){
int odd=0,even=0,i;
for(i=2;i<20;i++){
if(i%2==0)
even+=i;
else
odd+=i;
}
System.out.println("Sum of even numbers between 1 and 20 is: "+even);
System.out.println("Sum of odd numbers between 1 and 20 is: "+odd);
}
}
- Initialise even sum = 0 and odd sum = 0.
- Iterate a loop in the range - (2,19)
- Check if the number is even or not. If true, add the number to even sum or else odd sum.
- Display the sum.
See attachment for output.
Answer:
The given problem is solved using language - Java.
public class Brainly{
public static void main(String args[]){
int odd=0,even=0,i;
for(i=2;i<20;i++){
if(i%2==0)
even+=i;
else
odd+=i;
}
System.out.println("Sum of even numbers between 1 and 20 is: "+even);
System.out.println("Sum of odd numbers between 1 and 20 is: "+odd);
}
}