Computer Science, asked by kaustubhschool115243, 5 hours ago

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

Answered by anindyaadhikari13
4

\textsf{\large{\underline{Solution}:}}

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);

   }

}

\textsf{\large{\underline{Logic}:}}

  1. Initialise even sum = 0 and odd sum = 0.
  2. Iterate a loop in the range - (2,19)
  3. Check if the number is even or not. If true, add the number to even sum or else odd sum.
  4. Display the sum.

See attachment for output.

Attachments:
Answered by SDBhaie
0

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);

}

}

Similar questions