Computer Science, asked by poojarajeevan2006, 3 months ago

write a java program to calculate the sum of first 50 even and odd numbers​

Answers

Answered by anindyaadhikari13
25

Answer:

This is the required Java program for the question.

public class Main {  

 public static void main(String args[]) {

   int even=0,odd=0;

   for(int i=1;i<=100;i++)  {

     if(i%2==0)

       even+=i;

     else

       odd+=i;

   }

   System.out.println("Sum of 50 even numbers: "+even);

   System.out.println("Sum of 50 odd numbers: "+odd);

 }

}

Algorithm:

  1. START.
  2. Initialise even_sum=0, odd_sum=0
  3. Loop through first 50 even and odd numbers.
  4. Add the even numbers in even_sum and odd numbers in odd_sum.
  5. Display the values of even_sum and odd_sum.
  6. STOP.
Attachments:
Answered by BrainlyProgrammer
22

Answer:

//Program to calculate sum of first 50 even and odd numbers

package Programmer;

public class Sum{

public static void main (String ar[]){

int k=0, j=0;

for(int I=0;I<=100;I++){

k+=(I%2==0)?I:0;

j+=(I%2!=0)?I:0;

}

System.out.println("Even:"+k+"\nOdd:"+j);

}

}

Variable Description:-

  • k:- even sum
  • j:- odd sum
  • I:- loop variable

• Output Attached

Attachments:
Similar questions