Skip to main content

Command Palette

Search for a command to run...

Java Programming : Divisible by 3 and 5

Updated
1 min readView as Markdown
Java Programming : Divisible by 3 and 5

Example

Input 
enter the number 15
Output
Divisible by 3 and 5

Required to Know

Use of Variables, Use of different operators like assignment or arithmetic operators, Use of methods like System. out.print(), Use of If-else, and Use of Data types.

Steps

  • Take the number as an input from the user.

  • Check if the number is divisible by 3 and 5. If it is divisible by 3 and 5, print divisible. Otherwise, print it is not divisible.

Condition

if((num%3==0) and (num%5==0)){
    print("divisible")
}
else{
    print("not divisible")
}

Program

import java.util.Scanner;

public class Divisible {

    /**
     * This class checks if a number is divisible by 3 and 5.
     */

    public static void main(String[] args) {

        // Create a Scanner object to read user input.
        Scanner sc = new Scanner(System.in);

        // Prompt the user to enter a number.
        System.out.print("Enter a number: ");
        int a = sc.nextInt();

        // Check if the number is divisible by 3 and 5.
        if (a % 3 == 0 && a % 5 == 0) {

            // The number is divisible by 3 and 5.
            System.out.println("Divisible by 3 and 5");

        } else {

            // The number is not divisible by 3 and 5.
            System.out.println("Not divisible by 3 and 5");

        }

    }
}

More from this blog

TheCodingcoder

60 posts

Empowering the coding community by simplifying complex tasks.