How To Sum The Even Numbers Using Loop

Viewed 393

I need help on this code I seem to have a problem regarding on summing the even numbers, what I want to happen is that the even numbers will be outputted and at the same time there will be an output where all the even numbers are summed within the inputted range of the user. I am just a beginner at coding and I hope some people can help me.

import java.util.*;
public class Loop
{
    //Start
    public static void main(String args[]) 
    {
        
        Scanner console = new Scanner(System.in) ;
        System.out.println("Enter Start Number") ;
        int start =console.nextInt();
        System.out.println("Enter End Number") ;
        int end =console.nextInt();
        
        int sum = 0;
        System.out.println("The even numbers between "+start+" and "+end+" are the following: ");
        for (int r = start; r <= end; r++)
        {
        //if number%2 == 0 it means its an even number
        if(r % 2 == 0)
        {
        System.out.println(r);
        }
        }
    }
}
8 Answers

A concise way using Java streams. Just putting it here so that you know that there is another way to do this:

int sum = IntStream.range(start, 1 + end).filter(num -> 0 == num % 2).peek(System.out::println).sum();
System.out.println(sum);

You can use with sum+=r; and get the sum :

    public static void main(String args[]) 
    {
        
        Scanner console = new Scanner(System.in) ;
        System.out.println("Enter Start Number") ;
        int start =console.nextInt();
        System.out.println("Enter End Number") ;
        int end =console.nextInt();
        
        int sum = 0;
        System.out.println("The even numbers between "+start+" and "+end+" are the following: ");
        for (int r = start; r <= end; r++)
        {
        //if number%2 == 0 it means its an even number
        if(r % 2 == 0)
        {
            sum += r;
        System.out.println(r);
        }
        }
        System.out.println("The sum of even numbers between :"+start +" - "+end +" is : "+sum);
    }
}

Output :

Enter Start Number
10
Enter End Number
20
The even numbers between 10 and 20 are the following: 
10
12
14
16
18
20
The sum of even numbers between :10 - 20 is : 90

you are almost there

   int sum = 0;
    System.out.println("The even numbers between "+start+" and "+end+" are the following: ");
    for (int r = start; r <= end; r++)
    {
    if(r % 2 == 0)
    {
    sum = sum + r;
    System.out.println(r);
    }
    }
    System.out.println("the sum : "+sum);

When you print an even number, also add it to sum and print the value of sum after the loop.

for (int r = start; r <= end; r++) {
    //if number%2 == 0 it means its an even number
    if(r % 2 == 0) {
        System.out.println(r);
        sum += r;
    }
}

System.out.println("Sum = " + sum);

I do not understand why are you incrementing r by one, instead do this:

if (r % 2 == 1)  // if `r` is odd
    r++;

for (; r <= end; r+=2) {
    System.out.println(r);
    sum += r;
}

System.out.println("Sum = " + sum);

You can simplify by a lot! By creating a temporary variable t

  1. If start is odd, initialise t = start + 1, otherwise t = start
  2. Use a formula to calculate sum between two numbers
  3. Also, increment the loop variable by 2 instead of 1
public static void main(String args[]) {
    Scanner console = new Scanner(System.in);
    System.out.println("Enter Start Number");
    int start = console.nextInt();
    System.out.println("Enter End Number");
    int end = console.nextInt();
    System.out.println("The even numbers between " + start + " and " + end + " are the following:");

    int t = start % 2 == 1 ? start + 1: start;
    int sum = ((t + end) / 2) * ((end - t + 2) / 2);

    while(t <= end) {
        System.out.println(t);
        t += 2;
    }
    
    System.out.println("The sum of even numbers between : " + start + " - " + end + " is : " + sum);
}

Note: If we make the use of bit-wise operators we can make replace the following

  1. int t = start % 2 == 1 ? start + 1: start; by int t = start + (start & 1);

  2. int sum = ((t + end) / 2) * ((end - t + 2) / 2); by int sum = ((t + end) >> 1) * ((end - t + 2) >> 1);

Final Solution

public static void main(String args[]) {
    Scanner console = new Scanner(System.in);
    System.out.println("Enter Start Number");
    int start = console.nextInt();
    System.out.println("Enter End Number");
    int end = console.nextInt();
    System.out.println("The even numbers between " + start + " and " + end + " are the following:");

    int t = start + (start & 1);
    int sum = ((t + end) >> 1) * ((end - t + 2) >> 1);
    while(t <= end) {
        System.out.println(t);
        t += 2;
    }
    System.out.println("The sum of even numbers between : " + start + " - " + end + " is : " + sum);
}
int sum = 0;
for(int i=start;i<=end;i++){
   if(i%2==0){
      sum += i;
   }
}
System.out.println(sum);
/**
 * C program to print sum of all even numbers between 1 to n
 */

#include <stdio.h>

int main()
{
    int i, n, sum=0;

    /* Input upper limit from user */
    printf("Enter upper limit: ");
    scanf("%d", &n);

    for(i=2; i<=n; i+=2)
    {
        /* Add current even number to sum */
        sum += i;
    }

    printf("Sum of all even number between 1 to %d = %d", n, sum);

    return 0;
}

Output:- Enter upper limit: 10 Sum of all even number between 1 to 10 = 30

Related