# Sum of all integers between two integers

Here given code implementation process.

``````/*
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 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)
{

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

}
}``````

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

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

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

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

#### 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``````

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 