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

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep 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. #2
  3. 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
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep 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? :(
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep 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?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep 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".
  10. #6
  11. 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.

    Comments on this post

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

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    1
    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! :)

IMN logo majestic logo threadwatch logo seochat tools logo