Write a program to compulsory Marks : 10 calculate and print the sum of odd numbers and sum of even numbers for the first n natural numbers. The integer n is to be entered by the user.
Attachments:
Answers
Answered by
7
Answer:
import java.util.*;
public class Question26
{
static void main()
{
Scanner sc=new Scanner(System.in);
int i,n,s1=0,s2=0;
System.out.println("Enter a number:");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(i%2==0)
s1=s1+i;
else
s2=s2+i;
}
System.out.println("Sum of even numbers="+s1);
System.out.println("Sum of odd numbers="+s2);
}
}
Similar questions