sum all numbers in a range javascript
ar sum = 0;
for (var i = Math.min(...arr); i <= Math.max(...arr); i++) {
sum += i;
}
sum all numbers in a range javascript
ar sum = 0;
for (var i = Math.min(...arr); i <= Math.max(...arr); i++) {
sum += i;
}
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]);
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(); }}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us