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 :: jsonnode change field value 
Java :: HOW TO SUPRESS sonar warning in java code 
Java :: character classes in regex 
Java :: java localdate zoneid example 
Java :: java enum get first element 
Java :: android display drawable in imageview 
Java :: in a junit test how test if empty list is returned 
Java :: data to string format java 
Java :: java max from 3 parameters 
Java :: java klasse 
Java :: how to collect objective in java 
Java :: stream sum java 
Java :: print hashtable in java 
Java :: how to iterate hashmap in java 
Java :: android java toast 
Java :: RealmConfiguration.Builder.allowWritesOnUiThread(true) 
Java :: assert system.out.println 
Java :: java split string on two or more spaces except for words in quotes 
Java :: jackson object to string 
Java :: java sort 2d array 
Java :: how to declare list of object in java as constant 
Java :: gitignore java 
Java :: java stream filter 
Java :: find object with same attribute java stream 
Java :: android java convert double to 2 decimal places 
Java :: change button text when clicked android studio 
Java :: how to make an activity default in android studio 
Java :: entre clavier java 
Java :: how to count an replace substring string in java 
Java :: spigot how to kill player 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =