write a java program to calculate the sum of first 50 even and odd numbers
Answers
Answered by
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:
- START.
- Initialise even_sum=0, odd_sum=0
- Loop through first 50 even and odd numbers.
- Add the even numbers in even_sum and odd numbers in odd_sum.
- Display the values of even_sum and odd_sum.
- STOP.
Attachments:
Answered by
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
English,
1 month ago
Math,
1 month ago
Social Sciences,
3 months ago
Math,
3 months ago
History,
10 months ago