⇒ Write a program using for loop to calculate the sum of first 50 even and odd integers.
Answers
Answer:
Program:
public class Sum_EvenOdd
{
public static void main(String args[ ])
{
int a=0,s1=0,s2=0;
for(a=1;a<=50;a++);
{
if(a%2==0)
s1=s1+a;
else
s2=s2+a;
}
System.out.println("The sum of first 50 even numbers="+s1);
System.out.println("The sum of first 50 odd numbers="s2);
}
}
/*
Project Type: Brainly Answer
Date Created: 09-02-2021
Date Edited Last Time: ---NIL---
Question Link: https://brainly.in/question/34894165
Question: Write a program using for loop to calculate
the sum of first 50 even and odd integers.
Program Created By: atrs7391
Programming Language: Java
Language version (When program created or last edited): jdk-15.0.2
*/
package Brainly_Answers;
public class Sum_of_First_50_Odd_and_Even_Numbers {
public static void main(String[] args) {
int se = 0, so = 0;
// variable declaration as integers and initialising them 0
// here se means sum of even numbers and so means sum of odd numbers
for (int i = 1; i <= 100 ; i++)
// Iterating for loop from values of 1 to 100 as i
{
if (i%2==0)
// checking if i is even number
{
se = i+se;
// if i is even then adding i to se
}
else
// if i is not even then it's obvious that i is odd
{
so = i+so;
// because i is odd, so adding i to so
}
}
System.out.println("Sum of first 50 even numbers = "+se);
System.out.println("Sum of first 50 odd numbers = "+so);
// printing the sum of first 50 even and odd numbers respectively
}
}
Please read this:
Why did you reported my answer! Just check it ones, its's a correct program and then report it and not by just giving it a glance.