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

  1. #1
    Registered User
    Join Date
    Sep 2015

    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:

    #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;
        printf("Write a number: \n");
        scanf(" %d",&tal);
        if (tal>max) max=tal;
        if (tal<min) min=tal;
        summa= summa+tal;
        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!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2012
    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 subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-13-2015, 11:54 PM
  2. how can i make average/maximum/minimum score to an output file?
    By catastrophe in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2013, 11:43 PM
  3. Finding maximum and minimum number in 2D array
    By wonderwall in forum C Programming
    Replies: 4
    Last Post: 10-23-2011, 10:21 PM
  4. Minimum/maximum number problem. Please help!
    By PYROMANIAC702 in forum C Programming
    Replies: 43
    Last Post: 07-15-2011, 12:28 AM
  5. Replies: 2
    Last Post: 10-31-2002, 07:27 PM