### Thread: Using a loop to find the largest number provided

1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Location
Posts
116
Rep Power
2

#### Using a loop to find the largest number provided

So I've been able to go this far:
Code:
```#include <stdio.h>

int main(void)
{
int i;
float n;

printf("Enter a number: ");
scanf("%f", &n);

for (i = 0; i < n; i++) {
printf("Enter a number: ");
scanf("%f", &n);
if (n == 0 || n < 0)
printf("The largest number entered was: %f", n);
}

return 0;
}```
The last printf is where I am stuck. What do I replace 'n' with? Btw, the program shows the largest number if 0 or a negative number is provided as input.
2. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Posts
15
Rep Power
0
Code:
```int n,m,i,max;

printf("How many numbers(n) you going to enter:");
scanf("%d",&n);
printf("Enter the numbers:");
scanf("%d",&m);
max=m;
for(i=2;i<=n;i++)
{
scanf("%d",&m);
if(m>max)
max=m;
}
printf("The Largest Number is %d",max);```

use like this
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Location
Posts
116
Rep Power
2
So here's an edited version:
Code:
```#include <stdio.h>

int main(void)
{
int i;
float n, max, m;

printf("Enter a number: ");
scanf("%f", &n);
max = n;

while (n > 0) {
printf("Enter a number: ");
scanf("%f", &n);
if (n > max);
max = n;
break;
}
printf("The largest number entered was: %.0f", max);

return 0;
}```
But now the loop exits after providing only two inputs. Any thoughts? :(
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Location
Posts
116
Rep Power
2
Here's a modification:
Code:
```#include <stdio.h>

int main(void)
{
int i;
float n, max, m;

printf("Enter a number: ");
scanf("%f", &n);
max = n;

do {
printf("Enter a number: ");
scanf("%f", &n);
if (n > max);
max = n;
} while (n > 0);

printf("The largest number entered was: %.0f", max);

return 0;
}```
Now I get to enter as much inputs as I want, but after I enter a number less than or equal to zero, the output is always zero. Any idea why?
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Location
Posts
116
Rep Power
2
I figured it out. Here's the working code:
Code:
```#include <stdio.h>

int main(void)
{
int i;
float n, max, m;

printf("Enter a number: ");
scanf("%f", &n);
max = n;

do {
printf("Enter a number: ");
scanf("%f", &n);
if (n > max) {
max = n; }
} while (n > 0);

printf("The largest number entered was: %.0f", max);

return 0;
}```
I just needed to put curly braces around "max = n".
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2013
Posts
15
Rep Power
0
Its not due to curely braces, it due to ; at the end of if statement in the previous code...

not to be like this if (n > max) ;

if(n>max)
max=n;

and

if(n>max){
max=n;
}

both are same when you are having just one statement.

• arman.khandaker agrees
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Location
Posts
116
Rep Power
2
Its not due to curely braces, it due to ; at the end of if statement in the previous code...

not to be like this if (n > max) ;

if(n>max)
max=n;

and

if(n>max){
max=n;
}

both are same when you are having just one statement.
Ahh I see. Thanks man! :)