6
Moyenne des nombres positifs saisis au clavier
- #include <stdio.h>
- main() {
- int n, count = 0, sum = 0;
- printf( "Enter a series of integers (end with 0):\n" );
- for ( ;; ) {
- printf("%d: ", count+1);
- scanf("%d", &n );
- if ( n == 0 )
- break;
- if ( n < 0 )
- continue;
- sum += n;
- ++count;
- }
- printf("The mean of the %d positive numbers is %f\n", count, (float)sum/count);
- }
$ gcc -o mean mean.c
$ ./mean
Enter a series of integers (end with 0):
1: 9
2: 3
3: -3
3: 6
4: 0
The mean of the 3 positive numbers is 6.000000
Commentaires