JAVA Programming
1. Write a program to input a number and check that number is divisible by 7 or
not.
Answers
Answer:
The given program is written in Java.
import java.util.*;
public class IsDivisible {
public static void main(String args[]) {
System.out.print("Enter a number: ");
int n=(new Scanner(System.in)).nextInt();
if(n%7==0)
System.out.println("Number is divisible by 7.");
else
System.out.println("Number is not divisible by 7.");
}
}
Explanation:
- To find the remainder obtained when a number is divided by another number, the modulus (%) operator is used.
- So, to get the remainder obtained when a number, say n is divided by 7, we can write - n%7
- If number is completely divisible by 7, then n%7 must be equal to zero.
- So, take the number as input, check if n%7==0. If true, number is divisible by 7 else not.
Refer to the attachment for output.
•••♪
Answer:
Program:-
import java.io.*;
public class Divisibility
{
public static void main(String args[ ]) throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(System.in);
int n,num=0;
System.out.println("Enter the number");
n=Integer.parseInt(in.readLine());
num=n;
if(n%7==0)
{
System.out.println(num + " is divisible by 7");
}
else
System.out.println(num + " is not divisible by 7");
}
}