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

Program to determine divisible by both 5 and 7

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:

  1. The program begins 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.
  3. The printf function is used to prompt the user to enter an integer.
  4. The scanf function is used to read the integer entered by the user and store it in the variable N.
  5. 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).
  6. If N is divisible by both 5 and 7, we print that it is divisible by both 5 and 7 using the printf function.
  7. 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.
  8. 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:

  1. In this Java code, the Scanner class is used to read input from the user.
  2. The program starts by importing the necessary classes, including Scanner.
  3. The DivisibilityChecker class is defined, which contains the main method where the program execution begins.
  4. Inside the main method, an integer variable N is declared to store the integer entered by the user.
  5. A Scanner object named scanner is created to read input from the user.
  6. The program prompts the user to enter an integer using the System.out.print() method.
  7. 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.
  8. 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.
  9. 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.