Reputation: 29
instructions are: Write a program which reads 10 different integers from the user and finds and prints a) Minimum element b) Sum of all elements and c) Average of elements. Note: do not use arrays to store the user-entered integers.
i did b and c part like this but i can't a there is my code:
#include <stdio.h>
void main() {
int n, i, sum = 0;
double avarage = 0;
int min = 0;
i = 1;
while (i <= 10) {
printf("Enter an integer: ");
scanf ("%d", &n);
sum += n;
if (n < min) {
min = n;
}
++i;
}
printf("Sum = %d \n", sum);
avarage = sum / 10;
printf("Avg = %.2lf \n", avarage);
printf("Min = %d \n", min);
}
this is output of my code:
How can i print Minimum of those.
Upvotes: 0
Views: 40
Reputation: 3066
you min
variable starts with 0, so every number you entered is larger then that.
int min = INT_MAX;
start min
with the largest possible integer will guarantee every number you take as input will be smaller
another approach is the use a flag (like boolean) for the first input, and if so directly put it into min
:
int min = 0;
i = 1;
int my_flag=0;
while (i <= 10) {
printf("Enter an integer: ");
scanf ("%d", &n);
sum += n;
if (n < min) {
min = n;
}
if(my_flag==0){
min=n;
my_flag=1;
}
++i;
}
Upvotes: 2