New guy here and I'm stuck on this code I cant get to work right. I want to input any amount of numbers and I want the output to be the largest number along with how many times the largest number was input. I'm just off a bit but I can't for the life of me figure it out.

ok this is the new and revised version but it STILL messes up the count of the largest number

``````#include<stdio.h>
#include<stdlib.h>

int main()
{

int num;
int largest= 0;
int count= 0;

while(num>0)
{

printf("Enter a number 0 to end\n");
scanf("%d" ,&num);
if (num >= largest) {
largest = num;
count++;
}

}

printf("The largest number is %d and it was entered %d times\n" ,largest ,count);

system ("pause");

return 0;

}``````

## All 10 Replies

Theres a problem with our count variable, you end up incremented everytime regarrdless if its the largest or not, and you never reset it to 0.

``````if (num > largest){
largest = num;
count=1;}
if(num == largest) count++;``````

Change this :

``````if (num > largest)
largest = num;
count++;``````

to this :

``````if (num >= largest) {
largest = num;
count++;
}``````

And I think it should work.

still no go on all attempts. I appreciate all the help though.

Whats the problem exactly? The code, other then the if portion seems to be correct.

Whats the problem exactly? The code, other then the if portion seems to be correct.

the computer outputs the wrong amount for the times the largest number was entered

Just to be clear, is this the code your running?

``````#include<stdio.h>
#include<stdlib.h>

int main()
{

int num;
int largest= 0;
int count= 0;

while(num>0)
{

printf("Enter a number 0 to end\n");
scanf("%d" ,&num);
if (num > largest){
largest = num;
count=1;}
if(num == largest) count++;

}

printf("The largest number is %d and it was entered %d times\n" ,largest ,count);

system ("pause");

return 0;

}``````

Just to be clear, is this the code your running?

``````#include<stdio.h>
#include<stdlib.h>

int main()
{

int num;
int largest= 0;
int count= 0;

while(num>0)
{

printf("Enter a number 0 to end\n");
scanf("%d" ,&num);
if (num > largest){
largest = num;
count=1;}
if(num == largest) count++;

}

printf("The largest number is %d and it was entered %d times\n" ,largest ,count);

system ("pause");

return 0;

}``````

yes

How much is the count off by? And im starting to notice a few more problems. I assume that all numbers are positive?

``````#include<stdio.h>
#include<stdlib.h>

int main()
{
int num;
int largest=0;
int count= 1;
while(num>0)
{
printf("Enter a number 0 to end\n");
scanf("%d" ,&num);
if (num > largest){
largest = num;
count=1;}
if(num == largest) count++;
}
printf("The largest number is %d and it was entered %d times\n" ,largest ,count);
system ("pause");
return 0;
}``````

How much is the count off by? And im starting to notice a few more problems. I assume that all numbers are positive?

``````#include<stdio.h>
#include<stdlib.h>

int main()
{
int num;
int largest=0;
int count= 1;
while(num>0)
{
printf("Enter a number 0 to end\n");
scanf("%d" ,&num);
if (num > largest){
largest = num;
count=1;}
if(num == largest) count++;
}
printf("The largest number is %d and it was entered %d times\n" ,largest ,count);
system ("pause");
return 0;
}``````

idk why, but it was off once and no more. problem solved. thanks all

So my newest version worked for you? If it did, its because the count started at 0, meaning that if the first number you put ended up being the largest, the count would be 1 less then it should.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.