Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Trying to find the average of 5 numbers C

Joel ArroyoJoel Arroyo Member Posts: 3

I am extremely new to programming and I am trying to find the average of 5 numbers inputted by the user and then output it back out. The only part of my code that is not working properly is calculating the average I keep getting 1 each iteration and I cannot figure out what I am doing wrong. My code is as follows:

include <stdio.h>

int main (void){

long int first,second,third,fourth,fifth;

int sum;

int mean = sum/5;

printf("Please put in five numbers\n");

scanf("%d%d%d%d%d", &first, &second, &third, &fourth, &fifth);

printf("You entered: %d %d %d %d %d\n" , first, second, third, fourth, fifth);

sum = first+second+third+fourth+fifth;

printf("The sum of the numbers you entered is %d\n",
sum);

printf("The average is %d\n",
mean);

return 0;
}

Comments

  • B00B00 Member Posts: 79 ✭✭✭

    You're calculating average before you even have the numbers. In what world this kind of math works?

  • Joel ArroyoJoel Arroyo Member Posts: 3
    edited January 27

    Sorry if I had not clarified these numbers are being input by the user. Is that not what the scanf is? To get input from the user and then store them in the corresponding variables? Otherwise your response is just really rude and not really that insightful. Can you go into more detail of how to actually correct the issue logically?

  • Joel ArroyoJoel Arroyo Member Posts: 3

    I figured it out. Not sure how to mark this as solved but its solved.

  • Shivesh AbhishekShivesh Abhishek Member Posts: 1

    Hey Joel, first let me tell you that you have defined mean not in correct place.
    put --> mean =sum/5; after this line of code:
    sum = first+second+third+fourth+fifth;
    i.e:
    sum = first+second+third+fourth+fifth;
    mean=sum/5;
    C compiles the program with the approach of top to bottom. so it must find 'sum' before 'mean' to calculate correctly .

    and you have the right concept about scanf.

    Your welcome.

Sign In or Register to comment.