MatthewS
MatthewS

Reputation: 485

Iteration program

I have written the following code:

#include <stdio.h>

/* max_number.c: outputs the largest number of five numbers entered */

void main(void) {
  int i, num, max;

  for (i = 1; i <= 5; i++) {
    printf("Enter a number: ");
    scanf("%d", &num);

    if (num >= max)
      max = num;
  }
  printf("The maximum number is %d\n", max);
}

When I run the program with any type of data I continually get "The maximum number is 14". Can someone please point me in the direction of what I am doing wrong? Thank you!

Upvotes: 0

Views: 44

Answers (1)

AlexD
AlexD

Reputation: 32566

Variable max is not initialized.

Try

int i, num, max = INT_MIN;

Upvotes: 4

Related Questions