I am learning C and I am having a little trouble with this program.
The task was to write a program that can determine the largest and smallest integers.
I am using a while loop to accumulate the numbers
the value 0 acts as a "sentinel" value that will stop the program.
The problem is when I enter 0 to stop the program, it will take 0 as the smallest number. This also happens when I do other values like -99 or others. Any help will be greatly appreciated.
//Write a program that reads an integer and determines and prints whether it is od or even
int counter = 0;//counter of variables
int accumulator = 0;//adds all numbers up
int average;//computes average of all the numbers
int largest = 0;
int smallest = 0;
int counting = -99;//value that, when entered, will stop program.
while(numbers != 0)
printf("Please enter a number. Enter 0 to end.\n");
accumulator += numbers;
if(numbers == 0)
if(numbers < smallest && numbers !=0 );
numbers = smallest;
if(numbers % 2 == 0 && numbers != 0)
printf("%d is an even number.\n", numbers);
else if(numbers % 2 != 0 && numbers != 0)
printf("%d is an odd number.\n", numbers);
if(numbers < smallest && numbers != 0)
smallest = numbers;
if(numbers > largest && numbers != 0)
largest = numbers;
average = accumulator/counter;
if(counter > 2)
printf("User made %d entries\n", counter);
printf("User made 1 entry.\n");
printf("Total of all entries: %d\n", accumulator);
printf("Average: %d\n", average);
printf("Largest number: %d\n ", largest);
printf("Smallest number: %d\n ", smallest);