Java Program to print Odd and Even Numbers from an ArrayWe can print odd and even numbers from an array in java by getting remainder of each element and checking if it is divided by 2 or not. If it is divided by 2, it is even number otherwise it is odd number. Show Output: Odd Numbers:
1
5
3
Even Numbers:
2
6
2
Next TopicJava Programs ← prev next → How to find the sum of even numbers in JavaJune 12, 2021October 12, 2021 0 Comments how to find sum of even numbers in java, sum of even numbers in array java, sum of even numbers using for loop in java, sum of even numbers using while loop in java In this tutorial, we are going to see how to write a Java program to calculate the sum of the even numbers from 1 to N using the For loop and the While loop. Any number divisible by 2 is an even number. The following Java code allows the user to enter a limit value. Then, this Java program finds the sum of the even numbers from 1 to the cutoff value using the If statement and the For loop. Output: Enter a number: 100 The sum of even numbers from 1 to 100 = 2550The following Java code allows the user to enter a limit value. Then, this Java program finds the sum of the even numbers from 1 to the cutoff value using the If statement and the While Loop. import java.util.Scanner; public class Main { public static void main(String[] args) { int n, i = 2, p = 0; Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); n = sc.nextInt(); while(i <= n) { p = p + i; i = i + 2; } System.out.println("The sum of even numbers from 1 to "+n+" = "+p); } }Output: Enter a number: 100 The sum of even numbers from 1 to 100 = 2550
Spread the love |