Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

get sum in range

# From user input, using lambda
# pass string directly to lambda
print((lambda s: sum(range(int(s[0]), int(s[1])+1)))(input().split()))
# or, convert string to list then pass into lambda
print((lambda s: sum(range(s[0], s[1]+1)))(list(map(int, input().split()))))
# input: 1 100
# output: 5050
# Or, simply, from variables
a, b = 1, 100
print(sum(range(a, b+1)))
Comment

Sum All Numbers in a Range

const sumAll = arr => {
   let maxNumber = Math.max(...arr)
   let minNumber = Math.min(...arr)
   let numbersArray = []

   for(let i = minNumber; i <= maxNumber; i++){
     numbersArray.push(i)
   }
   return numbersArray.reduce((acc, cur) => acc + cur, 0)
}

sumAll([1, 4]);

// With love @kouqhar
Comment

sum in range

//Java program to print the sum of numbers in a given rangeimport java.util.Scanner;public class sum_of_numbers_in_range{		public static void main(String[] args)	{		//scanner class declaration		Scanner sc = new Scanner(System.in);		//input from user		System.out.print("Enter starting number : ");						int start = sc.nextInt();		System.out.print("Enter ending number : ");						int end = sc.nextInt();		//declare a variable to store sum		int sum = 0;		//loop to add n natural numbers		for(int i = start ; i <= end ; i++)		sum=sum+i;		//display the sum		System.out.print("Sum of numbers in the range from "+start+" to "+end+" is "+sum);		//closing scanner class(not compulsory, but good practice)		sc.close();														}}
Comment

get range sum

// get sum from x to y
// initialization of variables
int x = 1, y = 100, sum = 0;
// loop from x to y and add up the current index i
for(int i = x; i = y; i++) sum += i;
std::cout << sum;
Comment

PREVIOUS NEXT
Code Example
Python :: get a slice of string in python 
Python :: how to read excel file in python 
Python :: how to reset index after dropping rows pandas 
Python :: python program to solve quadratic equation 
Python :: how to change avatar of a bot using discord.py 
Python :: copy from folder to folder python 
Python :: Python Removing Directory or File 
Python :: list comprehension if else 
Python :: discord py bot example 
Python :: python scatter plot legend 
Python :: what value do we get from NULL database python 
Python :: django signup view 
Python :: regex findall 
Python :: csv library python convert dict to csv 
Python :: randomforestregressor in sklearn 
Python :: python remove spaces 
Python :: python get last element of iterator 
Python :: how to get username with userid discord.py 
Python :: python remove consecutive spaces 
Python :: count decimal number python 
Python :: time addition in python 
Python :: jupyter markdown new line 
Python :: dynamic array python numpy 
Python :: wordle python 
Python :: pandas merge on index column 
Python :: numpy arrauy to df 
Python :: how to add header in csv file in python 
Python :: Simple Scatter Plot in matplotlib 
Python :: file searching in python 
Python :: pytest multi thread 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =