Skip to main content

Sum of all integers between two integers

The sum of all integers between two integers refers to the total result of adding up all the whole numbers (integers) that are between two given integers, including the two given integers themselves.

For example, suppose we want to find the sum of all integers between 3 and 7, inclusive. The integers between 3 and 7 are 3, 4, 5, 6, and 7. To find their sum, we simply add them up:

3 + 4 + 5 + 6 + 7 = 25

So the sum of all integers between 3 and 7 is 25.

In general, if we want to find the sum of all integers between two integers a and b (inclusive), where a and b are whole numbers and a ≤ b, we can use the formula:

int n = max - min;
// Calculated sum
int result = ((n + 1) *min) + ((n *(n + 1)) / 2);

min is start number and max is ending number.

Program Solution

/*
    C program for
    Sum of all integers between two integers 
*/
#include <stdio.h>

void sumBetweenAtoB(int min, int max)
{
    if (max < min)
    {
        return;
    }
    /*
        Formula
        resutl = ((n+1) *min) + ((n *(n+1))/2))
        // Here
        min : starting number
        max : last number
        n = max - min
        n indicate number of elements between min to max
        ((n *(n+1))/2) sum of natural number from 1 to n
    */
    int n = max - min;
    // Calculated sum
    int result = ((n + 1) *min) + ((n *(n + 1)) / 2);
    // Display calculated result
    printf("\n Sum of number from (%d..%d) is : %d", min, max, result);
}
int main(int argc, char
    const *argv[])
{
    // Test A
    /*
        min = 4
        max = 10
        [4+5+6+7+8+9+10] = 49
    */
    sumBetweenAtoB(4, 10);
    // Test B
    /*
        min = -5
        max = 6
        [-5+-4+-3+-2+-1+0+1+2+3+4+5+6] = 6
    */
    sumBetweenAtoB(-5, 6);
    // Test C
    /*
        min = 55
        max = 70
        [55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70] = 1000
    */
    sumBetweenAtoB(55, 70);
    return 0;
}

Output

 Sum of number from (4..10) is : 49
 Sum of number from (-5..6) is : 6
 Sum of number from (55..70) is : 1000
/*
    Java program for
    Sum of all integers between two integers 
*/
public class Addition
{
    public void sumBetweenAtoB(int min, int max)
    {
        if (max < min)
        {
            return;
        }
        /*
            Formula
            resutl = ((n+1) *min) + ((n *(n+1))/2))
            // Here
            min : starting number
            max : last number
            n = max - min
            n indicate number of elements between min to max
            ((n *(n+1))/2) sum of natural number from 1 to n
        */
        int n = max - min;
        // Calculated sum
        int result = ((n + 1) * min) + ((n * (n + 1)) / 2);
        // Display calculated result
        System.out.print("\n Sum of number from (" + 
                         min + ".." + max + ") is : " + result );
    }
    public static void main(String[] args)
    {
        Addition task = new Addition();

        // Test A
        /*
            min = 4
            max = 10
            [4+5+6+7+8+9+10] = 49
        */
        task.sumBetweenAtoB(4, 10);
        // Test B
        /*
            min = -5
            max = 6
            [-5+-4+-3+-2+-1+0+1+2+3+4+5+6] = 6
        */
        task.sumBetweenAtoB(-5, 6);
        // Test C
        /*
            min = 55
            max = 70
            [55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70] = 1000
        */
        task.sumBetweenAtoB(55, 70);

    }   
}

Output

 Sum of number from (4..10) is : 49
 Sum of number from (-5..6) is : 6
 Sum of number from (55..70) is : 1000
// Include header file
#include <iostream>

using namespace std;
/*
    C++ program for
    Sum of all integers between two integers 
*/
class Addition
{
	public: void sumBetweenAtoB(int min, int max)
	{
		if (max < min)
		{
			return;
		}
		/*
		    Formula
		    resutl = ((n+1) *min) + ((n *(n+1))/2))
		    // Here
		    min : starting number
		    max : last number
		    n = max - min
		    n indicate number of elements between min to max
		    ((n *(n+1))/2) sum of natural number from 1 to n
		*/
		int n = max - min;
		// Calculated sum
		int result = ((n + 1) *min) + ((n *(n + 1)) / 2);
		// Display calculated result
		cout << "\n Sum of number from (" << min 
             << ".." << max << ") is : " << result;
	}
};
int main()
{
	Addition *task = new Addition();
	// Test A
	/*
	    min = 4
	    max = 10
	    [4+5+6+7+8+9+10] = 49
	*/
	task->sumBetweenAtoB(4, 10);
	// Test B
	/*
	    min = -5
	    max = 6
	    [-5+-4+-3+-2+-1+0+1+2+3+4+5+6] = 6
	*/
	task->sumBetweenAtoB(-5, 6);
	// Test C
	/*
	    min = 55
	    max = 70
	    [55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70] = 1000
	*/
	task->sumBetweenAtoB(55, 70);
	return 0;
}

Output

 Sum of number from (4..10) is : 49
 Sum of number from (-5..6) is : 6
 Sum of number from (55..70) is : 1000
// Include namespace system
using System;
/*
    Csharp program for
    Sum of all integers between two integers 
*/
public class Addition
{
	public void sumBetweenAtoB(int min, int max)
	{
		if (max < min)
		{
			return;
		}
		/*
		    Formula
		    resutl = ((n+1) *min) + ((n *(n+1))/2))
		    // Here
		    min : starting number
		    max : last number
		    n = max - min
		    n indicate number of elements between min to max
		    ((n *(n+1))/2) sum of natural number from 1 to n
		*/
		int n = max - min;
		// Calculated sum
		int result = ((n + 1) * min) + ((n * (n + 1)) / 2);
		// Display calculated result
		Console.Write("\n Sum of number from (" + 
                      min + ".." + max + ") is : " + result);
	}
	public static void Main(String[] args)
	{
		Addition task = new Addition();
		// Test A
		/*
		    min = 4
		    max = 10
		    [4+5+6+7+8+9+10] = 49
		*/
		task.sumBetweenAtoB(4, 10);
		// Test B
		/*
		    min = -5
		    max = 6
		    [-5+-4+-3+-2+-1+0+1+2+3+4+5+6] = 6
		*/
		task.sumBetweenAtoB(-5, 6);
		// Test C
		/*
		    min = 55
		    max = 70
		    [55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70] = 1000
		*/
		task.sumBetweenAtoB(55, 70);
	}
}

Output

 Sum of number from (4..10) is : 49
 Sum of number from (-5..6) is : 6
 Sum of number from (55..70) is : 1000
package main
import "fmt"
/*
    Go program for
    Sum of all integers between two integers 
*/
func sumBetweenAtoB(min, max int) {
	if max < min {
		return
	}
	/*
	    Formula
	    resutl = ((n+1) *min) + ((n *(n+1))/2))
	    // Here
	    min : starting number
	    max : last number
	    n = max - min
	    n indicate number of elements between min to max
	    ((n *(n+1))/2) sum of natural number from 1 to n
	*/
	var n int = max - min
	// Calculated sum
	var result int = ((n + 1) * min) + ((n * (n + 1)) / 2)
	// Display calculated result
	fmt.Print("\n Sum of number from (", min, "..", max, ") is : ", result)
}
func main() {

	// Test A
	/*
	    min = 4
	    max = 10
	    [4+5+6+7+8+9+10] = 49
	*/
	sumBetweenAtoB(4, 10)
	// Test B
	/*
	    min = -5
	    max = 6
	    [-5+-4+-3+-2+-1+0+1+2+3+4+5+6] = 6
	*/
	sumBetweenAtoB(-5, 6)
	// Test C
	/*
	    min = 55
	    max = 70
	    [55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70] = 1000
	*/
	sumBetweenAtoB(55, 70)
}

Output

 Sum of number from (4..10) is : 49
 Sum of number from (-5..6) is : 6
 Sum of number from (55..70) is : 1000
// Rust program for
// Sum of all integers between two integers 
fn main(){
	// Test A
	/*
	    min = 4
	    max = 10
	    [4+5+6+7+8+9+10] = 49
	*/
	sum_between_ato_b(4, 10);
	// Test B
	/*
	    min = -5
	    max = 6
	    [-5+-4+-3+-2+-1+0+1+2+3+4+5+6] = 6
	*/
	sum_between_ato_b(-5, 6);
	// Test C
	/*
	    min = 55
	    max = 70
	    [55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70] = 1000
	*/
	sum_between_ato_b(55, 70);
}
fn sum_between_ato_b(min: i32, max: i32) {
	if max < min {
		return;
	}
	/*
	    Formula
	    resutl = ((n+1) *min) + ((n *(n+1))/2))
	    // Here
	    min : starting number
	    max : last number
	    n = max - min
	    n indicate number of elements between min to max
	    ((n *(n+1))/2) sum of natural number from 1 to n
	*/
	let n: i32 = max - min;
	// Calculated sum
	let result: i32 = ((n + 1) * min) + ((n * (n + 1)) / 2);
	// Display calculated result
	print!("\n Sum of number from ({}..{}) is : {}", min, max, result);
}

Output

 Sum of number from (4..10) is : 49
 Sum of number from (-5..6) is : 6
 Sum of number from (55..70) is : 1000
<?php
/*
    Php program for
    Sum of all integers between two integers 
*/
class Addition
{
	public	function sumBetweenAtoB($min, $max)
	{
		if ($max < $min)
		{
			return;
		}
		/*
		    Formula
		    resutl = ((n+1) *min) + ((n *(n+1))/2))
		    // Here
		    min : starting number
		    max : last number
		    n = max - min
		    n indicate number of elements between min to max
		    ((n *(n+1))/2) sum of natural number from 1 to n
		*/
		$n = $max - $min;
		// Calculated sum
		$result = (($n + 1) * $min) + ((int)(($n * ($n + 1)) / 2));
		// Display calculated result
		echo("\n Sum of number from (".$min.
			"..".$max.
			") is : ".$result);
	}
}

function main()
{
	$task = new Addition();
	// Test A
	/*
	    min = 4
	    max = 10
	    [4+5+6+7+8+9+10] = 49
	*/
	$task->sumBetweenAtoB(4, 10);
	// Test B
	/*
	    min = -5
	    max = 6
	    [-5+-4+-3+-2+-1+0+1+2+3+4+5+6] = 6
	*/
	$task->sumBetweenAtoB(-5, 6);
	// Test C
	/*
	    min = 55
	    max = 70
	    [55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70] = 1000
	*/
	$task->sumBetweenAtoB(55, 70);
}
main();

Output

 Sum of number from (4..10) is : 49
 Sum of number from (-5..6) is : 6
 Sum of number from (55..70) is : 1000
/*
    Node JS program for
    Sum of all integers between two integers 
*/
class Addition
{
	sumBetweenAtoB(min, max)
	{
		if (max < min)
		{
			return;
		}
		/*
		    Formula
		    resutl = ((n+1) *min) + ((n *(n+1))/2))
		    // Here
		    min : starting number
		    max : last number
		    n = max - min
		    n indicate number of elements between min to max
		    ((n *(n+1))/2) sum of natural number from 1 to n
		*/
		var n = max - min;
		// Calculated sum
		var result = ((n + 1) * min) + (parseInt((n * (n + 1)) / 2));
		// Display calculated result
		process.stdout.write("\n Sum of number from (" + 
                             min + ".." + max + ") is : " + result);
	}
}

function main()
{
	var task = new Addition();
	// Test A
	/*
	    min = 4
	    max = 10
	    [4+5+6+7+8+9+10] = 49
	*/
	task.sumBetweenAtoB(4, 10);
	// Test B
	/*
	    min = -5
	    max = 6
	    [-5+-4+-3+-2+-1+0+1+2+3+4+5+6] = 6
	*/
	task.sumBetweenAtoB(-5, 6);
	// Test C
	/*
	    min = 55
	    max = 70
	    [55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70] = 1000
	*/
	task.sumBetweenAtoB(55, 70);
}
main();

Output

 Sum of number from (4..10) is : 49
 Sum of number from (-5..6) is : 6
 Sum of number from (55..70) is : 1000
#    Python 3 program for
#    Sum of all integers between two integers 
class Addition :
	def sumBetweenAtoB(self, min, max) :
		if (max < min) :
			return
		
		#    Formula
		#    resutl = ((n+1) *min) + ((n *(n+1))/2))
		#    // Here
		#    min : starting number
		#    max : last number
		#    n = max - min
		#    n indicate number of elements between min to max
		#    ((n *(n+1))/2) sum of natural number from 1 to n
		n = max - min
		#  Calculated sum
		result = ((n + 1) * min) + (int((n * (n + 1)) / 2))
		#  Display calculated result
		print("\n Sum of number from (", 
              min ,"..", max ,") is : ", result, end = "")
	

def main() :
	task = Addition()
	#  Test A
	#    min = 4
	#    max = 10
	#    [4+5+6+7+8+9+10] = 49
	task.sumBetweenAtoB(4, 10)
	#  Test B
	#    min = -5
	#    max = 6
	#    [-5+-4+-3+-2+-1+0+1+2+3+4+5+6] = 6
	task.sumBetweenAtoB(-5, 6)
	#  Test C
	#    min = 55
	#    max = 70
	#    [55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70] = 1000
	task.sumBetweenAtoB(55, 70)

if __name__ == "__main__": main()

Output

 Sum of number from ( 4 .. 10 ) is :  49
 Sum of number from ( -5 .. 6 ) is :  6
 Sum of number from ( 55 .. 70 ) is :  1000
#    Ruby program for
#    Sum of all integers between two integers 
class Addition 
	def sumBetweenAtoB(min, max) 
		if (max < min) 
			return
		end

		#    Formula
		#    resutl = ((n+1) *min) + ((n *(n+1))/2))
		#    // Here
		#    min : starting number
		#    max : last number
		#    n = max - min
		#    n indicate number of elements between min to max
		#    ((n *(n+1))/2) sum of natural number from 1 to n
		n = max - min
		#  Calculated sum
		result = ((n + 1) * min) + ((n * (n + 1)) / 2)
		#  Display calculated result
		print("\n Sum of number from (", 
              min ,"..", max ,") is : ", result)
	end

end

def main() 
	task = Addition.new()
	#  Test A
	#    min = 4
	#    max = 10
	#    [4+5+6+7+8+9+10] = 49
	task.sumBetweenAtoB(4, 10)
	#  Test B
	#    min = -5
	#    max = 6
	#    [-5+-4+-3+-2+-1+0+1+2+3+4+5+6] = 6
	task.sumBetweenAtoB(-5, 6)
	#  Test C
	#    min = 55
	#    max = 70
	#    [55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70] = 1000
	task.sumBetweenAtoB(55, 70)
end

main()

Output

 Sum of number from (4..10) is : 49
 Sum of number from (-5..6) is : 6
 Sum of number from (55..70) is : 1000
/*
    Scala program for
    Sum of all integers between two integers 
*/
class Addition()
{
	def sumBetweenAtoB(min: Int, max: Int): Unit = {
		if (max < min)
		{
			return;
		}
		/*
		    Formula
		    resutl = ((n+1) *min) + ((n *(n+1))/2))
		    // Here
		    min : starting number
		    max : last number
		    n = max - min
		    n indicate number of elements between min to max
		    ((n *(n+1))/2) sum of natural number from 1 to n
		*/
		var n: Int = max - min;
		// Calculated sum
		var result: Int = ((n + 1) * min) + ((n * (n + 1)) / 2);
		// Display calculated result
		print("\n Sum of number from (" + 
              min + ".." + max + ") is : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Addition = new Addition();
		// Test A
		/*
		    min = 4
		    max = 10
		    [4+5+6+7+8+9+10] = 49
		*/
		task.sumBetweenAtoB(4, 10);
		// Test B
		/*
		    min = -5
		    max = 6
		    [-5+-4+-3+-2+-1+0+1+2+3+4+5+6] = 6
		*/
		task.sumBetweenAtoB(-5, 6);
		// Test C
		/*
		    min = 55
		    max = 70
		    [55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70] = 1000
		*/
		task.sumBetweenAtoB(55, 70);
	}
}

Output

 Sum of number from (4..10) is : 49
 Sum of number from (-5..6) is : 6
 Sum of number from (55..70) is : 1000
/*
    Swift 4 program for
    Sum of all integers between two integers 
*/
class Addition
{
	func sumBetweenAtoB(_ min: Int, _ max: Int)
	{
		if (max < min)
		{
			return;
		}
		/*
		    Formula
		    resutl = ((n+1) *min) + ((n *(n+1))/2))
		    // Here
		    min : starting number
		    max : last number
		    n = max - min
		    n indicate number of elements between min to max
		    ((n *(n+1))/2) sum of natural number from 1 to n
		*/
		let n: Int = max - min;
		// Calculated sum
		let result: Int = ((n + 1) * min) + ((n * (n + 1)) / 2);
		// Display calculated result
		print("\n Sum of number from (", 
              min ,"..", max ,") is : ", 
              result, terminator: "");
	}
}
func main()
{
	let task: Addition = Addition();
	// Test A
	/*
	    min = 4
	    max = 10
	    [4+5+6+7+8+9+10] = 49
	*/
	task.sumBetweenAtoB(4, 10);
	// Test B
	/*
	    min = -5
	    max = 6
	    [-5+-4+-3+-2+-1+0+1+2+3+4+5+6] = 6
	*/
	task.sumBetweenAtoB(-5, 6);
	// Test C
	/*
	    min = 55
	    max = 70
	    [55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70] = 1000
	*/
	task.sumBetweenAtoB(55, 70);
}
main();

Output

 Sum of number from ( 4 .. 10 ) is :  49
 Sum of number from ( -5 .. 6 ) is :  6
 Sum of number from ( 55 .. 70 ) is :  1000
/*
    Kotlin program for
    Sum of all integers between two integers 
*/
class Addition
{
	fun sumBetweenAtoB(min: Int, max: Int): Unit
	{
		if (max < min)
		{
			return;
		}
		/*
		    Formula
		    resutl = ((n+1) *min) + ((n *(n+1))/2))
		    // Here
		    min : starting number
		    max : last number
		    n = max - min
		    n indicate number of elements between min to max
		    ((n *(n+1))/2) sum of natural number from 1 to n
		*/
		val n: Int = max - min;
		// Calculated sum
		val result: Int = ((n + 1) * min) + ((n * (n + 1)) / 2);
		// Display calculated result
		print("\n Sum of number from (" + 
              min + ".." + max + ") is : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Addition = Addition();
	// Test A
	/*
	    min = 4
	    max = 10
	    [4+5+6+7+8+9+10] = 49
	*/
	task.sumBetweenAtoB(4, 10);
	// Test B
	/*
	    min = -5
	    max = 6
	    [-5+-4+-3+-2+-1+0+1+2+3+4+5+6] = 6
	*/
	task.sumBetweenAtoB(-5, 6);
	// Test C
	/*
	    min = 55
	    max = 70
	    [55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70] = 1000
	*/
	task.sumBetweenAtoB(55, 70);
}

Output

 Sum of number from (4..10) is : 49
 Sum of number from (-5..6) is : 6
 Sum of number from (55..70) is : 1000




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment