Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Java program to find the sum of all even numbers from 1 to 10

int sum = 0;
for (int i = 1; i <= 10; i++) {
// Logic for sum of EVEN
	if (i % 2 == 0) {
		sum = sum + i;
		}
}
System.out.println("Sum of all even from 1 to 10 is: " + sum);
Comment

how to output sum of even numbers in java between two user values

//import classes
import java.util.*;

public class chapter5no9
{
    static Scanner console = new Scanner(System.in);

    public static void main(String[] args)
    {

    //Part A
    int firstNum;
    int secondNum;
    int sumEven;

    System.out.println("Please enter an integer: ");
    firstNum = console.nextInt();

    System.out.println("Please enter another integer less than the first integer: ");
    secondNum = console.nextInt();

    //Part B
    if (firstNum < secondNum)
    {
        System.out.print("Your second number is greater than the first.  So Please re-enter: ");
        secondNum = console.nextInt();
    }
    else
    {
        System.out.print("Odd Numbers: ");
        firstNum++;
        while (firstNum > secondNum)
        {
            if (secondNum % 2 != 0)
            {
                System.out.print(" " + secondNum);
            }
            secondNum++;
        }

        System.out.println();
        System.out.print("Sum of Even Numbers: ");
        firstNum++;
        while (firstNum > secondNum)
        {
            if (secondNum % 2 != 0)
            {
                System.out.print(" " + secondNum);
            }
            secondNum++;
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: Using enum values as string literals 
Java :: bean factory vs application context 
Java :: java insert at index 
Java :: java char array to string 
Java :: java string to integer 
Java :: java swing dialog box 
Java :: find minimum number in java 
Java :: android studio edittext enter pressed 
Java :: java protected private public 
Java :: android studio change button color programmatically 
Java :: spring filter exception handling 
Java :: java hello word 
Java :: java substring after last char 
Java :: java sample code 
Java :: how to find palindrome numbers in java 
Java :: set solid color background android programatically in drawable 
Java :: flink prometheus alert on failed jobs 
Java :: java toggle button get state 
Java :: display two dimension array java 
Java :: how to use while loop in java 
Java :: calculate number of years months and days between two dates in java 
Java :: java array sortieren 
Java :: how to get string from resource id android 
Java :: what is transient in java 
Java :: java rename file 
Java :: mock ParameterizedTypeReference 
Java :: Cannot find a class with the main method. 
Java :: print statement in java 
Java :: from file to array java 
Java :: how to strip spaces in java using split with other delimiters 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =