Plz Help with C (Do - While) Loop to find largest number

Hi, I'm having some trouble with trying to get the largest number in my do - while loop...not sure if this is the way to go...anyway this is what I have done so far & it's in C...the way it is at the moment it only outputs the last number you enter as the largest number;


#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,m,i,max;
n=0;
m=0;
max=m;
do
{
	printf("Enter the numbers:");
	/*loops the program so that user can enter values*/
	for(i=0;i<=m;i++)
	scanf("%d",&m);
	/*determines if it is the largest number entered if not then it ignores it*/
	if(m>max)
	max=m;
	printf("The Largest Number is %d",m);
	printf("Number of values enetered were: %d\n",i);
}	

/*loops until user enters a value below 0 or a negative value*/
while(m>= 0);

return 0;

}
Last edited on
If there are no braces enclosing the statements after a for(), only the first statement after it is executed more than one time.

1
2
3
4
5
6
7
8
9
10
    printf("Enter the numbers:");
    /*loops the program so that user can enter values*/
    for(i=0;i<=m;i++)
    {
        scanf("%d",&m);
        if(m>max)
            max=m;
    }
    printf("The Largest Number is %d",m);
    printf("Number of values enetered were: %d\n",i);
thanks for the help , but it's still having the same output as before only that a new 'enter values' prompt is being printed for every entry
Great, thanks man...i got it working but it keeps a running a tally of the largest number entered. All I really did was what you suggested & change the m to a max in 'printf("The Largest Number is %d\t",max);'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
do
{
	
        /*loops the program so that user can enter values*/
	printf("Enter the numbers:");
	for(i=0;i<=m;i++)
	{
	/*determines if it is the largest number entered if not then it ignores it*/
	scanf("%d",&m);
	if(m>max)
	max=m;
	}
	printf("The Largest Number is %d\t",max);
	printf("Number of values entered were: %d\n",i);
	
	
	
}	
	
/*loops until user enters a value below 0 or a negative value*/
while(m>= 0);

return 0;

}
So, you really didn't want another loop to begin with.

1
2
3
4
5
6
7
8
9
10
11
12
printf( "Enter the numbers:") ;

do
{
    scanf("%d", &m) ;

    if ( m > max )
        max = m ;

} while ( m>=0 ) ;

printf("The Largest Number is %d\t", max) ;

Topic archived. No new replies allowed.