Algorithm


Problem Name: beecrowd | 1154

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1154

 

 

Adapted by Neilor Tonin, URI Brazil

Timelimit: 1

Write an algorithm to read an undeterminated number of data, each containing an individual's age. The final data, which will not enter in the calculations, contains the value of a negative age. Calculate and print the average age of this group of individuals.

 

Input

 

The input contains an undetermined number of integers. The input will be stop when a negative value is read.

 

Output

 

The output contains a value corresponding to the average age of individuals.

The average should be printed with two digits after the decimal point.

 

 

 

Input Sample Output Sample

34
56
44
23
-2

39.25

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include<bits/stdc++.h>
#define FIXED_FLOAT(x) std::fixed << std::setprecision(2) << (x)
using namespace std;

int main(){
    int sum = 0;
    int cnt = 0;
    while(1){
        int n;
        cin >> n;
        if (n  <  0){
            break;
        }
        sum += n;
        cnt += 1;
    }
    cout << FIXED_FLOAT(((1.0) * sum) / cnt) << endl;
    return 0;
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
34 56 44 23 -2

Output

x
+
cmd
39.25

#2 Code Example with Java Programming

Code - Java Programming


import java.io.IOException;
import java.util.Scanner;

public class Main{
    public static void main(String[] args) throws IOException{
        Scanner input = new Scanner(System.in);
        int sum = 0;
        int cnt = 0;
        while(true){
            int n = input.nextInt();
            if (n  <  0){
                break;
            }
            sum += n;
            cnt += 1;
        }
        System.out.printf("%.2f\n", (((1.0) *sum) / cnt));
    }
}


Copy The Code & Try With Live Editor

Input

x
+
cmd
34 56 44 23 -2

Output

x
+
cmd
39.25

#3 Code Example with Javascript Programming

Code - Javascript Programming


var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');

var sum = 0;
var cnt = 0;

while(true){
    var n = parseInt(lines.shift());
    if (n  <  0){
        break;
    }
    sum += n;
    cnt += 1;
}

console.log((sum / cnt).toFixed(2));
Copy The Code & Try With Live Editor

Input

x
+
cmd
34 56 44 23 -2

Output

x
+
cmd
39.25

#4 Code Example with Python Programming

Code - Python Programming


sum = 0
cnt = 0

while(True):
    n = int(input())
    if (n < 0):
        break
    sum += n
    cnt += 1

print("{:.2f}".format(sum / cnt))
Copy The Code & Try With Live Editor

Input

x
+
cmd
34 56 44 23 -2

Output

x
+
cmd
39.25
Advertisements

Demonstration


Previous
#1153 Beecrowd Online Judge Solution 1153 Simple Factorial Solution in C++, Java, Js and Python
Next
#1155 Beecrowd Online Judge Solution 1155 S Sequence Solution in C++, Java, Js and Python