Write a program that takes an integer N as input and determines whether it is divisible by both 5 and 7.
Program in C
C
#include <stdio.h>
int main() {
int N;
// Getting input from the user
printf("Enter an integer: ");
scanf("%d", &N);
// Checking if N is divisible by both 5 and 7
if (N % 5 == 0 && N % 7 == 0) {
printf("%d is divisible by both 5 and 7.\n", N);
} else {
printf("%d is not divisible by both 5 and 7.\n", N);
}
return 0;
}
Explanation:
- The program begins 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.
- 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 is divisible by both 5 and 7. This is done by checking if the remainder of dividing N by 5 is 0 (N % 5 == 0) and the remainder of dividing N by 7 is also 0 (N % 7 == 0).
- If N is divisible by both 5 and 7, we print that it is divisible by both 5 and 7 using the printf function.
- If N is not divisible by both 5 and 7, we print that it is not divisible by both 5 and 7 using the printf function.
- Finally, we return 0 to indicate successful execution of the program.
Output:
Output
Enter an integer: 70
70 is divisible by both 5 and 7.
Program in Java
Java
import java.util.Scanner;
public class DivisibilityChecker {
public static void main(String[] args) {
int N;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
N = scanner.nextInt();
if (N % 5 == 0 && N % 7 == 0) {
System.out.println(N + " is divisible by both 5 and 7.");
} else {
System.out.println(N + " is not divisible by both 5 and 7.");
}
scanner.close();
}
}
Explanation:
- In this Java code, the Scanner class is used to read input from the user.
- The program starts by importing the necessary classes, including Scanner.
- The DivisibilityChecker class is defined, which contains the main method where the program execution begins.
- Inside the main method, an integer variable N is declared to store the integer entered by the user.
- A Scanner object named scanner is created to read input from the user.
- The program prompts the user to enter an integer using the System.out.print() method.
- The nextInt() method of the Scanner class is used to read the integer value entered by the user and assign it to the N variable.
- The program then checks if the integer N is divisible by both 5 and 7 using the if statement:
- If N is divisible by both 5 and 7 (i.e., the remainders of N divided by 5 and 7 are both 0), the program prints that N is divisible by both 5 and 7 using the System.out.println() method.
- If N is not divisible by both 5 and 7, the program prints that N is not divisible by both 5 and 7.
- Finally, the scanner.close() method is called to release any resources associated with the Scanner object and close the input stream.
Java
Enter an integer: 40
40 is not divisible by both 5 and 7.