简体   繁体   中英

Problem checking numbers between 1 and 100

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.

I have managed to write the following code to check for the to check if the numbers are between 1 to 100

#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)) {
    printf("good");
    printf("Numbers are good");
  } else {
    printf("All numbers must be between 1 to 100");
  }
  return (0);
}

When i run the code i get this output "All numbers must be between 1 to 100" Even if the numbers i enter are between the range of 1-100. i expect the output to be "Numbers are good". Please help.

Your test is wrong, you want (n >= 1) && (n <= 100) not (n <= 1) && (n >= 100)

Also use a loop to manage all the inputs rather duplicating the code, imagine if you have to enter 1000 numbers.

A proposal:

#include <stdio.h>
#define N 10
int main() {
  int isAllOk = 1;
  int nums[N]; /* to save the values to have them available later even not used in your question */
  for (int i = 0; i != N; ++i) {
    printf("\nEnter number %d : ", i + 1);
    if (scanf("%d", & nums[i]) != 1) {
      fprintf(stderr, "invalid input\n");
      return -1;
    }
    isAllOk &= ((nums[i] >= 1) && (nums[i] <= 100));
  }
  puts((isAllOk) ? "Numbers are good" : "All numbers must be between 1 to 100");
  return (0);
}

Compilation and execution:

pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra l.c
pi@raspberrypi:/tmp $ ./a.out
Enter number 1 : 1
Enter number 2 : 2
Enter number 3 : 44
Enter number 4 : 3
Enter number 5 : 3
Enter number 6 : 3
Enter number 7 : 3
Enter number 8 : 3
Enter number 9 : 3
Enter number 10 : 3
Numbers are good
pi@raspberrypi:/tmp $ ./a.out
Enter number 1 : 111111111
Enter number 2 : 23
Enter number 3 : 3
Enter number 4 : 3
Enter number 5 : 3
Enter number 6 : 3
Enter number 7 : 3
Enter number 8 : 3
Enter number 9 : 3
Enter number 10 : 3
All numbers must be between 1 to 100

Note it is also possible to stop immediately when a number is not between 1 and 100 but that seems not compatible with your request.

The condition in the if statement is wrong. It must be x=>1 and x<=100 . And also it doesn't have to separate all scanf and condition.

Your condition is 1<=num<=100 , not 1>=num and 100>=num , so the solution would be to change the if condition, like this:

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)
) {
  printf("Numbers are good");
} else {
  printf("All numbers must be between 1 to 100");
}

(n <= 1) && (n >= 100) is wrong. It should have been (n >= 1) && (n <= 100) . Now you can improve your design with loops, arrays and dedicated functions. This makes your code easier to read/understand and also easily modifiable. Adding another 10 values to the bunch shouldn't mean 10 more (handwritten) checks, and changing the range shouldn't mean changing all 20 checks anew. Here's an example implementation:

#include <stdio.h>
#include <stdbool.h>

bool numb_in_range(int numb, int lower, int upper)
{
  return (numb >= lower) && (numb <= upper);
}

bool arry_in_range(int* arry, size_t sz, int lower, int upper)
{
  for (size_t i = 0; i != sz; ++i)
  {
    if (!numb_in_range(arry[i], lower, upper))
    {
      return false;
    }
  }
  return true;
}

int main(void)
{
  const size_t sz = 10;
  int arry[sz];
  for (size_t i = 0; i != sz; ++i)
  {
    scanf("%d", arry + i);
  }

  if (arry_in_range(arry, 10, 1, 100))
  {
    puts("good");
  }
  else
  {
    puts("bad");
  }

  return 0;
}

You need to revert all comparisons like this:

if ((num1 >= 1 && num2 >= 1 && ... && num10 >= 1) &&
    (num1 <= 100 && num2 <= 100 && ...  && num10 <= 100))

The problem are the comparators, because the condition is wrong.

You accept as good when numbers are lower than 1 and higher than 100 .

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)
) {
  printf("good");
}

如果 N 是大于或等于 1 且小于或等于 100 的数字,我们将其表示为:

if(N >= 1 && N <= 100)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM