write a java program to calculate the sum of first 50 even and odd numbers
Share
write a java program to calculate the sum of first 50 even and odd numbers
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
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:
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:-
• Output Attached