# Thread: C program that tells Minimum number, Maximum Number, Sum And Average

1. ## C program that tells Minimum number, Maximum Number, Sum And Average

So the task is to write a program that repeatedly ask for a positive number, Negative numbers will make the input to stop.
After that the program will tell you minimum number, maximum number, sum of the numbers and average of the numbers. You also need to be able to start over again without closing the program.
Im not allowed to store the value in arrays. The result is supposed to be counted over time after every new number the user input. The negative number is not supposed to be in the result.

Here is the code so far:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
int summa = 0, antal, max =-0, min= INT_MAX;
int tal, k;
float medel;
int again;
start:
do
{
printf("Write a number: \n");
scanf(" %d",&tal);
if (tal>max) max=tal;
if (tal<min) min=tal;
summa= summa+tal;
antal++;
}
while (tal >=0);
medel=(float) summa/antal;
printf("Sum of the numbers: %d \n", summa);
printf("Highest number: %d \n", max);
printf("Lowest number: %d \n", min);
printf("Average number: %f \n", medel);
{
printf("If u wanna play again press 1 if not press 2 \n");
scanf(" %d", &again);
if (again==1){
goto start;
}
}
}
My problem is that the negative number is in the result and the average number is complitly wrong.

I need some pointers on how to fix the problem!
Thanks

2. A few comments:
• Please get rid of the goto. You should be using a loop statement here instead of implementing a loop using goto and a label.
• Indent your code properly.
• You should reset your variables at the start of the loop that implements "start over again without closing the program".
• Your inner loop should both check the return value of scanf and check that the value read is non-negative (or non-positive: the instructions are not clear what to do with 0).

3. Initialize antal with 0. I think goto should not be used. All the printf statements can be in the do while loop.

Popular pages Recent additions