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