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:
- The program starts by including the necessary header file stdio.h, which provides input/output functions like printf and scanf.
- In the main function, we declare the variable N to store the input integer and the variables lowerLimit and upperLimit to represent the range.
- The lowerLimit and upperLimit variables are assigned the values of the specific range, which in this case is 1 to 100.
- The printf function is used to prompt the user to enter an integer.
- The scanf function is used to read the integer entered by the user and store it in the variable N.
- 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.
- If N falls within the range, we print that it falls within the specified range using the printf function.
- If N does not fall within the range, we print that it does not fall within the specified range using the printf function.
- 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:
- The program starts by importing the java.util.Scanner class, which allows us to read input from the user.
- The NumberRange class is defined, which contains the main method where the program execution begins.
- 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.
- A Scanner object named scanner is created to read input from the user.
- The program prompts the user to enter an integer using System.out.print(“Enter an integer: “).
- 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.
- 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.
- 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.
- 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.
- The program execution ends, and the program terminates.
Java Output
Enter an integer: 104
104 does not fall within the range 1 to 100.