Answers for "find the average"

C++
1

Compute Average

int a, b, c, d, sum, average;

    cout << "Input the 1st number: ";
    cin >> a;
    cout << "Input the 2nd number: ";
    cin >> b;
    cout << "Input the 3rd number: ";
    cin >> c;
    cout << "Input the 4th number: ";
    cin >> d;

    sum = a+b+c+d;
    average = sum/4;

    cout << "The average is: " << average << endl;
Posted by: Guest on March-04-2022
0

Find Average of Numbers

const average = (...args) => args.reduce((a, b) => a + b) / args.length;

average(1, 2, 3, 4);
// Result: 2.5
Posted by: Guest on January-07-2022
0

find average

const arr = [2, 4, 5, 6];

// Find average function
function fAve(totalVal, arrVal, index, array) {
	const val = totalVal + arrVal;
    
    if (index === array.lengh -1) {
    	return val / array.length;
    }
    
    return val;
}

// Pass function into reduce method
const average = arr.reduce(fAve);

console.log(average);


//node (js file name).
Posted by: Guest on May-03-2022

Code answers related to "find the average"

Browse Popular Code Answers by Language