Given two integers L and R, the task is to find the sum of all even numbers in range L and R.
Examples:
Input: L = 2, R = 5 Output: 6 2 + 4 = 6 Input: L = 3, R = 8 Output: 18
Method-1: Iterate from L to R and sum all the even numbers in that range.
Method-2: Find the sum all the natural numbers from L to R and subtract the sum of odd natural numbers in range L to R from it.
Method-3:
- Find the sum of all even numbers up to R i.e. No. of even numbers up to R will be R/2.
- Find the sum of all even numbers up to L-1 i.e. No. of even numbers up to L-1 will be (L-1)/2.
- Then subtract sumUptoL from sumuptoR.
Sum of all even numbers up to any N will be:
R*(R+1) where R = N/2
Below is the implementation of the above approach:
C++
// C++ program to print the sum// of all even numbers in range L and R#include <bits/stdc++.h>using namespace std;// Function to return the sum of// all natural numbersint sumNatural(int n){ int sum = (n * (n + 1)); return sum;}// Function to return sum// of even numbers in range L and Rint sumEven(int l, int r){ return sumNatural(r/2) - sumNatural((l-1) / 2);}// Driver Codeint main(){ int l = 2, r = 5; cout << "Sum of Natural numbers from L to R is " << sumEven(l, r); return 0;} |
Java
// Java program to print the sum // of all even numbers in range L and R import java.io.*;class GFG { // Function to return the sum of // all natural numbers static int sumNatural(int n) { int sum = (n * (n + 1)); return sum; } // Function to return sum // of even numbers in range L and R static int sumEven(int l, int r) { return sumNatural(r/2) - sumNatural((l-1) / 2); } // Driver Code public static void main (String[] args) { int l = 2, r = 5; System.out.println ("Sum of Natural numbers from L to R is "+ sumEven(l, r)); }} |
Python3
# Python 3 program to print the sum # of all even numbers in range L and R # Function to return the sum # of all natural numbers def sumNatural(n): sum = (n * (n + 1)) return int(sum)# Function to return sum # of even numbers in range L and R def sumEven(l, r): return (sumNatural(int(r / 2)) - sumNatural(int((l - 1) / 2)))# Driver Code l, r = 2, 5print("Sum of Natural numbers", "from L to R is", sumEven(l, r)) # This code is contributed # by 29AjayKumar |
C#
// C# program to print the sum // of all even numbers in range L and Rusing System;public class GFG{ // Function to return the sum of // all natural numbers static int sumNatural(int n) { int sum = (n * (n + 1)); return sum; } // Function to return sum // of even numbers in range L and R static int sumEven(int l, int r) { return sumNatural(r/2) - sumNatural((l-1) / 2); } // Driver Code static public void Main (){ int l = 2, r = 5; Console.WriteLine("Sum of Natural numbers from L to R is "+ sumEven(l, r)); }} |
PHP
<?php// PHP program to print the sum of// all even numbers in range L and R// Function to return the sum of// all natural numbersfunction sumNatural($n){ $sum = ($n * ($n + 1)); return $sum;}// Function to return sum of// even numbers in range L and Rfunction sumEven($l, $r){ return sumNatural((int)($r / 2)) - sumNatural((int)(($l - 1) / 2));}// Driver Code$l = 2; $r = 5;echo "Sum of Natural numbers " . "from L to R is " . sumEven($l, $r);// This code is contributed // by Akanksha Rai?> |
Javascript
<script>// Javascript program to print the sum // of all even numbers in range L and R// Function to return the sum of // all natural numbers function sumNatural(n) { let sum = Math.floor(n * (n + 1)); return sum; } // Function to return sum // of even numbers in range L and R function sumEven(l, r) { return sumNatural(Math.floor(r/2)) - sumNatural(Math.floor(l-1) / 2); } // driver program let l = 2, r = 5; document.write ("Sum of Natural numbers from L to R is "+ sumEven(l, r)); </script> |
Sum of Natural numbers from L to R is 6
Time Complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
