Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 28th, 2013, 08:54 AM
 arman.khandaker
Contributing User

Join Date: Jun 2013
Posts: 116
Time spent in forums: 16 h 51 m 33 sec
Reputation Power: 1
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
June 28th, 2013, 09:06 AM
Registered User

Join Date: May 2013
Posts: 15
Time spent in forums: 5 h 34 m 18 sec
Reputation 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
June 28th, 2013, 09:59 AM
 arman.khandaker
Contributing User

Join Date: Jun 2013
Posts: 116
Time spent in forums: 16 h 51 m 33 sec
Reputation Power: 1
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
June 28th, 2013, 10:16 AM
 arman.khandaker
Contributing User

Join Date: Jun 2013
Posts: 116
Time spent in forums: 16 h 51 m 33 sec
Reputation Power: 1
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
June 28th, 2013, 10:30 AM
 arman.khandaker
Contributing User

Join Date: Jun 2013
Posts: 116
Time spent in forums: 16 h 51 m 33 sec
Reputation Power: 1
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
June 28th, 2013, 12:38 PM
Registered User

Join Date: May 2013
Posts: 15
Time spent in forums: 5 h 34 m 18 sec
Reputation 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
June 28th, 2013, 12:50 PM
 arman.khandaker
Contributing User

Join Date: Jun 2013
Posts: 116
Time spent in forums: 16 h 51 m 33 sec
Reputation Power: 1
Quote:
 Originally Posted by raadys 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!

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Using a loop to find the largest number provided