Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Programme to check if number is inside range

Write a program that takes an integer N as input and determines whether it falls within a specific range, such as 1 to 100.

Program in C

C
#include <stdio.h>

int main() {
    int N;
    int lowerLimit = 1;
    int upperLimit = 100;

    // Getting input from the user
    printf("Enter an integer: ");
    scanf("%d", &N);

    // Checking if N falls within the specified range
    if (N >= lowerLimit && N <= upperLimit) {
        printf("%d falls within the range %d to %d.\n", N, lowerLimit, upperLimit);
    } else {
        printf("%d does not fall within the range %d to %d.\n", N, lowerLimit, upperLimit);
    }

    return 0;
}

Explanation:

  1. The program starts by including the necessary header file stdio.h, which provides input/output functions like printf and scanf.
  2. In the main function, we declare the variable N to store the input integer and the variables lowerLimit and upperLimit to represent the range.
  3. The lowerLimit and upperLimit variables are assigned the values of the specific range, which in this case is 1 to 100.
  4. The printf function is used to prompt the user to enter an integer.
  5. The scanf function is used to read the integer entered by the user and store it in the variable N.
  6. We then use an if statement to check if the integer N falls within the specified range. This is done by checking if N is greater than or equal to lowerLimit and less than or equal to upperLimit.
  7. If N falls within the range, we print that it falls within the specified range using the printf function.
  8. If N does not fall within the range, we print that it does not fall within the specified range using the printf function.
  9. Finally, we return 0 to indicate successful execution of the program.

Output:

Output
Enter an integer: 4
4 falls within the range 1 to 100.

Program in Java

Java
import java.util.Scanner;

public class NumberRange {
    public static void main(String[] args) {
        int N;
        int lowerLimit = 1;
        int upperLimit = 100;

        // Getting input from the user
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        N = scanner.nextInt();

        // Checking if N falls within the specified range
        if (N >= lowerLimit && N <= upperLimit) {
            System.out.printf("%d falls within the range %d to %d.\n", N, lowerLimit, upperLimit);
        } else {
            System.out.printf("%d does not fall within the range %d to %d.\n", N, lowerLimit, upperLimit);
        }
    }
}

Explanation:

  1. The program starts by importing the java.util.Scanner class, which allows us to read input from the user.
  2. The NumberRange class is defined, which contains the main method where the program execution begins.
  3. Inside the main method, three integer variables are declared: N to store the user input, lowerLimit to represent the lower limit of the range, and upperLimit to represent the upper limit of the range.
  4. A Scanner object named scanner is created to read input from the user.
  5. The program prompts the user to enter an integer using System.out.print(“Enter an integer: “).
  6. The nextInt() method of the Scanner class is used to read an integer value entered by the user, and the value is assigned to the variable N.
  7. The program checks if the value of N falls within the specified range using an if statement. The condition N >= lowerLimit && N <= upperLimit checks if N is greater than or equal to lowerLimit and less than or equal to upperLimit.
  8. If the condition is true, meaning N falls within the range, the program prints the message “%d falls within the range %d to %d.\n” using System.out.printf(). The placeholders %d are replaced with the corresponding values of N, lowerLimit, and upperLimit.
  9. If the condition is false, meaning N does not fall within the range, the program prints the message “%d does not fall within the range %d to %d.\n” using System.out.printf(). Again, the placeholders %d are replaced with the corresponding values of N, lowerLimit, and upperLimit.
  10. The program execution ends, and the program terminates.
Java Output
Enter an integer: 104
104 does not fall within the range 1 to 100.