1. Write a program to input a number and check that the number is divisible by 7 or not.
Answers
Explanation:
Rate this video
Ø 5.0 / 3 ratings
Description Divisibility Rules - 7
A number is divisible by 7 if it has a remainder of zero when divided by 7. Examples of numbers which are divisible by 7 are 28, 42, 56, 63, and 98. Divisibility by 7 can be checked by using long division, although this process can be quite time-consuming. Especially when faced with a very large number. Thus, knowledge of divisibility rules for 7 can be very helpful for determining if a number is divisible by 7 or not quickly.
Here are two rules which can be utilized to test divisibility by 7: Rule 1: Remove the last digit, double it, subtract it from the truncated original number and continue doing this until only one digit remains. If this is 0 or 7, then the original number is divisible by 7. For example, to test divisibility of 12264 by 7, we simply perform the following manipulations: 1226 - 8 = 1218 121 - 16 = 105 10 - 10 = 0 Thus, 12264 is divisible by 7.
Rule 2: Take the digits of the number in reverse order, that is, from right to left, multiplying them successively by the digits 1, 3, 2, 6, 4, 5, repeating with this sequence of multipliers as long as necessary. Then add the products. If the resulting sum is divisible by 7, then the original number is divisible by 7. For example, to test divisibility of 12264 by 7, we simply check 4(1) + 6(3) + 2(2) + 2(6) + 1(4) = 4 + 18 + 4 + 12 + 4 = 42, a two-digit number divisible by 7. Hence, 12264 must also be divisible by 7.
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.