您好,欢迎访问一九零五行业门户网

Java程序,用于检查一个数字是否可以被5整除

在数学中,5 的整除规则规定,如果数字以 0 或 5 结尾,则它可以被 5 整除。还有另一种方法来确定 5 的整除规则,如果余数为 0,则返回该数字能被 5 整除。mod(%)运算符通常在编程中用于整除。
让我们举一个例子。
给定的数字是 525,该数字以 5 结尾,并且可以被 5 整除。
给定的数字是 7050,该数字以 0 结尾,并且可以被 5 整除。
给定的数字是 678,该数字不以 0 和 5 结尾,并且不能被 5 整除。
在本文中,我们将解决该数字是否能被 5 整除的问题。
算法以下步骤是
我们将使用 java.util.* 包来获取原始数据类型的用户输入。
从主类开始,初始化整型变量来存储输入的整数。
然后使用 mod(%) 运算符将整数变量除以 5。
最后,打印结果。
示例 1在此程序中,我们将使用 if-else 语句来检查数字是否能被 5 整除。
import java.util.*;public class check_divisiblity_of_five { public static void main(string[] args) { int number =590; if (number % 5 == 0) system.out.println(number+ \tis the number is divisible by 5); else system.out.println(number+ \tthe number is not divisible by 5); }}
输出590 is the number is divisible by 5
示例 2在此程序中,我们将获取一个输入变量,并使用“!=”运算符在 if 语句的条件下检查它。如果带有 mod(%) 运算符的整型变量不等于 0,则会导致该数字能被 5 整除。
import java.util.*;public class check_divisible_five { public static void main(string[] args) { int n=109; if(n % 5 != 0) { system.out.println(n+\t is the number not divisible by 5); } else { system.out.println(n+\t is the number divisible by 5); } }}
输出109 is the number not divisible by 5
示例 3在这个程序中,我们设置了50-100之间的数字范围,并检查该数字是否能被5整除。
import java.util.scanner;public class divisible_by_five { public static void main(string []args) { for(int n = 50; n <= 100; n++){ if(n % 5 == 0) { system.out.println(the number is divisible by 5 :\t+ n); } } }}
输出the number is divisible by 5 : 50the number is divisible by 5 : 55the number is divisible by 5 : 60the number is divisible by 5 : 65the number is divisible by 5 : 70the number is divisible by 5 : 75the number is divisible by 5 : 80the number is divisible by 5 : 85the number is divisible by 5 : 90the number is divisible by 5 : 95the number is divisible by 5 : 100
结论我们探索了与 5 整除相关的所有三个示例。然后使用 mod(%) 运算符检查整除规则的条件。一旦数字可以整除,它就会打印结果,否则不会。参考这个程序我们也能知道其他数的整除规则。
以上就是java程序,用于检查一个数字是否可以被5整除的详细内容。
其它类似信息

推荐信息