Thread: Program to find the largest and lowest value

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    1

    Question Program to find the largest and lowest value

    I am trying to write a C program that can accept 10 numbers between 1 and 100. If values outside the range are entered an error message should be displayed and allow the user to enter a correct value. It should then find the highest value entered and lowest value entered.

    Here is what i have tried so far but i get the output "All numbers must be between 1 to 100". Kindly assist.

    Here is my code:

    Code:
    #include "stdio.h"
    
    
    int main() {
       int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10;
     
       printf("\nEnter the first number : ");
       scanf("%d", &num1);
     
       printf("\nEnter the second number : ");
       scanf("%d", &num2);
       
          printf("\nEnter the third number : ");
       scanf("%d", &num3);
     
       printf("\nEnter the fourth number : ");
       scanf("%d", &num4);
      
       printf("\nEnter the fifth number : ");
       scanf("%d", &num5);
     
       printf("\nEnter the sixth number : ");
       scanf("%d", &num6);
       
          printf("\nEnter the seventh number : ");
       scanf("%d", &num7);
     
       printf("\nEnter the eighth number : ");
       scanf("%d", &num8);
          
          printf("\nEnter the nineth number : ");
       scanf("%d", &num9);
     
       printf("\nEnter the tenth number : ");
       scanf("%d", &num10);
       
       if ((num1 < 1 &&  num2 < 1 &&  num3 < 1 &&  num4 < 1 &&  num5 < 1 &&  num6 < 1 &&  num7 < 1 &&  num8 < 1 &&  num9 < 1 &&  num10 < 1) &&
       (num1 > 100 &&  num2 > 100 &&  num3 > 100 &&  num4 > 100 &&  num5 > 100 &&  num6 > 100 &&  num7 > 100 &&  num8 > 100 &&  num9 > 100 &&  num10 > 100)){
           
           if( num1>=num2 && num1>=num3 && num1>=num3 && num1>=num4 && num1>=num5 && num1>=num6 && num1>=num7 && num1>=num8 && num1>=num9 && num1>=num10)
           printf("%d.2lf is the largest number.", num1);
           else if( num1<=num2 && num1<=num3 && num1<=num3 && num1<=num4 && num1<=num5 && num1<=num6 && num1<=num7 && num1<=num8 && num1<=num9 && num1<=num10)
           printf("%d.2lf is the smallest number.", num1);
           
           else if( num2>=num1 && num2>=num3 && num2>=num3 && num2>=num4 && num2>=num5 && num2>=num6 && num2>=num7 && num2>=num8 && num2>=num9 && num2>=num10)
           printf("%d.2lf is the largest number.", num2);
           else if( num2<=num1 && num2<=num3 && num2<=num3 && num2<=num4 && num2<=num5 && num2<=num6 && num2<=num7 && num2<=num8 && num2<=num9 && num2<=num10)
           printf("%d.2lf is the largest number.", num2);
           
           else if( num3>=num1 && num3>=num2 && num3>=num4 && num3>=num5 && num3>=num6 && num3>=num7 && num3>=num8 && num3>=num9 && num3>=num10)
           printf("%d.2lf is the largest number.", num3);
           else if( num3<=num1 && num3<=num2 && num3<=num4 && num3<=num5 && num3<=num6 && num3<=num7 && num3<=num8 && num3<=num9 && num3<=num10)
           printf("%d.2lf is the largest number.", num3);
           
           else if( num4>=num1 && num4>=num2 && num4>=num3 && num4>=num5 && num4>=num6 && num4>=num7 && num4>=num8 && num4>=num9 && num4>=num10)
           printf("%d.2lf is the largest number.", num4);
           else if( num4<=num1 && num4<=num2 && num4<=num3 && num4<=num5 && num4<=num6 && num4<=num7 && num4<=num8 && num4<=num9 && num4<=num10)
           printf("%d.2lf is the largest number.", num4);
           
           else if( num5>=num1 && num5>=num2 && num5>=num3 && num5>=num4 && num5>=num6 && num5>=num7 && num5>=num8 && num5>=num9 && num5>=num10)
           printf("%d.2lf is the largest number.", num5);
           else if( num5<=num1 && num5<=num2 && num5<=num3 && num5<=num4 && num5<=num6 && num5<=num7 && num5<=num8 && num5<=num9 && num5<=num10)
           printf("%d.2lf is the largest number.", num5);
           
           else if( num6>=num1 && num6>=num2 && num6>=num3 && num6>=num4 && num6>=num5 && num6>=num7 && num6>=num8 && num6>=num9 && num6>=num10)
           printf("%d.2lf is the largest number.", num6);
           else if( num6<=num1 && num6<=num2 && num6<=num3 && num6<=num4 && num6<=num5 && num6<=num7 && num6<=num8 && num6<=num9 && num6<=num10)
           printf("%d.2lf is the largest number.", num6);
           
           else if( num7>=num1 && num7>=num2 && num7>=num3 && num7>=num4 && num7>=num5 && num7>=num6 && num7>=num8 && num7>=num9 && num7>=num10)
           printf("%d.2lf is the largest number.", num7);
           else if( num7<=num1 && num7<=num2 && num7<=num3 && num7<=num4 && num7<=num5 && num7<=num6 && num7<=num8 && num7<=num9 && num7<=num10)
           printf("%d.2lf is the largest number.", num7);
           
           else if( num8>=num1 && num8>=num2 && num8>=num3 && num8>=num4 && num8>=num5 && num8>=num6 && num8>=num7  && num8>=num9 && num8>=num10)
           printf("%d.2lf is the largest number.", num8);
           else if( num8<=num1 && num8<=num2 && num8<=num3 && num8<=num4 && num8<=num5 && num8<=num6 && num8<=num7  && num8<=num9 && num8<=num10)
           printf("%d.2lf is the largest number.", num8);
           
           else if( num9>=num1 && num9>=num2 && num9>=num3 && num9>=num4 && num9>=num5 && num9>=num6 && num9>=num7 && num9>=num8 && num9>=num10)
           printf("%d.2lf is the largest number.", num9);
           else if( num9<=num1 && num9<=num2 && num9<=num3 && num9<=num4 && num9<=num5 && num9<=num6 && num9<=num7 && num9<=num8 && num9<=num10)
           printf("%d.2lf is the largest number.", num9);
           
           else if( num10>=num1 && num10>=num2 && num10>=num3 && num10>=num4 && num10>=num5 && num10>=num6 && num10>=num7 && num10>=num8 && num10>=num9)
           printf("%d.2lf is the largest number.", num10);
           else( num10<=num1 && num10<=num2 && num10<=num3 && num10<=num4 && num10<=num5 && num10<=num6 && num10<=num7 && num10<=num8 && num10<=num9);
           printf("%d.2lf is the largest number.", num10);
           
       }else{
            printf("All numbers must be between 1 to 100");
       }
     
       return (0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10;
    Start with

    int num[10];

    Then replace your copy/paste of input with
    Code:
    for ( i = 0 ; i < 10 ; i++ ) {
       printf("\nEnter the %d number : ",i+1);
       scanf("%d", num[i]);
    }
    You can then do a similar loop to find min/max within the array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    6
    Quote Originally Posted by Salem View Post
    > int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10;
    Start with

    int num[10];

    Then replace your copy/paste of input with
    Code:
    for ( i = 0 ; i < 10 ; i++ ) {
       printf("\nEnter the %d number : ",i+1);
       scanf("%d", num[i]);
    }
    You can then do a similar loop to find min/max within the array.
    Can you explain a little more ?

  4. #4
    Registered User
    Join Date
    Mar 2019
    Posts
    6
    Code:
    #include <stdio.h>
    int main()
    {
    	int i, sayi[5], max = 0;
    
    
    	for(i = 0; i < 5; i++)
    	{
    		printf("Lutfen %d. sayiyi girin.\n", i);
    		scanf("%d", &sayi[i]);
    
    
    		if(sayi[i] >= max)
    		{
    			max = sayi[i];
    		}
    		else
    		{
    			max = max;
    		}
    	}
    printf("max sayi : %d\n", max);
    
    
    	return 0;
    }

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by Mavi View Post
    Can you explain a little more ?
    This might help

    For, While and Do While Loops in C - Cprogramming.com

    https://www.cprogramming.com/tutorial/c/lesson8.html
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Mar 2019
    Posts
    6
    Thank you bro.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 02-15-2016, 02:01 PM
  2. Replies: 7
    Last Post: 02-27-2013, 01:43 PM
  3. Replies: 10
    Last Post: 02-01-2013, 08:13 PM
  4. Replies: 3
    Last Post: 01-30-2013, 09:30 AM
  5. Program to find largest of 3 numbers
    By stuartbaggs in forum C Programming
    Replies: 9
    Last Post: 10-11-2012, 04:50 AM

Tags for this Thread