Question:
Write any java program.
Your Program must include loops, decision structures, input and output, comments.
ANSWER :
CODE :
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt(); //input the number
int sum_even=0, sum_odd = 0;
for(int i=1; i<=n; i++) //loop
{
if(i % 2 == 0) //decision structure
{
sum_even += i;
}
else if( i % 2 == 1)
{
sum_odd += i;
}
}
System.out.println("Sum of even
numbers = "+sum_even); //output for sum of even
System.out.println("Sum of odd
numbers = "+sum_odd); // output for sum of odd
}
}
OUTPUT :
Get Answers For Free
Most questions answered within 1 hours.