Computer Science, asked by yamini12343, 4 months ago

Write a program to print sum of even numbers and sum of odd numbers from first 50 natural
numbers using a for loop.​ in java​

Answers

Answered by pawarneha489
4

Answer:

even numbers:-1 to 100

odd numbers:-1,3,5,7,9,11,13,15,17,19

natural numbers:-{1,2,3,4,5,6,7,8,9,10,11,.......}

Answered by lol4389
4

Java Program to find Sum of Even and Odd Numbers

Write a Java Program to find Sum of Even and Odd Numbers using For Loop, and While Loop with example.

TIP : We already explained the logic to check the number is Even or Not in Java Odd or Even Program article.

Java Program to find Sum of Even and Odd Numbers using For Loop

This Java program allows the user to enter the maximum limit value. Next, this Java program calculates the sum of even and odd numbers from 1 to maximum limit value using For Loop and If statement.

// Java Program to find Sum of Even and Odd Numbers using for loop

import java.util.Scanner;

public class SumofEvenOdd1 {

private static Scanner sc;

public static void main(String[] args)

{

int number, i, evenSum = 0, oddSum = 0;

sc = new Scanner(System.in);

System.out.print(" Please Enter any Number : ");

number = sc.nextInt();

for(i = 1; i <= number; i++)

{

if(i % 2 == 0)

{

evenSum = evenSum + i;

}

else

{

oddSum = oddSum + i;

}

}

System.out.println("\n The Sum of Even Numbers upto " + number + " = " + evenSum);

System.out.println("\n The Sum of Odd Numbers upto " + number + " = " + oddSum);

}

}

OUTPUT

Java Program to find Sum of Even and Odd Numbers 1

Similar questions