Computer Science, asked by ritikkkkk, 1 year ago

write a program in java to calculate the sum of even and odd numbers in the range of 1 to 20​

Answers

Answered by suneelgoli
20

Answer:

import java.util.Scanner;

public class SumofEvenOdd1 {

public static void main(String[] args)

{

 int number, i, evenSum = 0, oddSum = 0;

 Scanner sc = new Scanner(System.in);

 System.out.print(" Please Enter any Number : ");

 number = 20;

 for(i = 1; i <= number; i++)

 {

  if(i % 2 == 0)

  {

   evenSum = evenSum + i;

  }

  else

  {

   oddSum = oddSum + i;

  }

 }

 System.out.println("\n The Sum of Even Numbers upto " + number + "  =  " + evenSum);

 System.out.println("\n The Sum of Odd Numbers upto " + number + "  =  " + oddSum);

}

}

Answered by Jasleen0599
1

A program in java to calculate the sum of even and odd numbers in the range of 1 to 20​

import java.util.Scanner;

public class OddEvenSum

{

   public static void main(String args[]) {

     Scanner in = new Scanner(System.in);

       int arr[] = new int[20];

       

       System.out.println("Enter 20 numbers");

       for (int i = 0; i < arr.length; i++) {

           arr[i] = in.nextInt();

       }

     int oddSum = 0, evenSum = 0;

       for (int i = 0; i < arr.length; i++) {

           if (arr[i] % 2 == 0)

               evenSum += arr[i];

           else

               oddSum += arr[i];

       }

       System.out.println("Sum of Odd numbers = " + oddSum);

       System.out.println("Sum of Even numbers = " + evenSum);

   }

}

#SPJ2

Similar questions