java基础编程题
1.斐波拉契数列。
2.判断一个数是否是质数或者素数。
3.判断一个数字是否是回文数字。
4.求一个数的阶乘。
5.判断一个数是否是Armstrong Number。
定义: A positive number is called armstrong number if it is equal to the sum of cubes of its digits for example 0, 1, 153, 370, 371, 407 etc.
java代码实现
/**
* @author: create by liubh
* @email lbhbinhao@gmail.com
* @date:2020/4/21
*/
public class Algothrim {
public static void main(String[] args) {
System.out.println(judgeArmstrongNum(154));
}
/**
* 求fibonacci数列
* 测试方法
* for (int i=0;i<10;i++){
* System.out.println(fibonacci(i));
* }
* @param n
* @return
*/
public static int fibonacci(int n){
if(n==0){
return 0;
}else if(n==1){
return 1;
}
else return fibonacci(n-1)+fibonacci(n-2);
}
/**
* 判断一个数是否为质数
* @param n
* @return false表示不是 true表示是
*/
public static boolean judgePrimeNumber(int n){
if (n==0||n==1){
return false;
}
else {
for (int i=2;i<=n/2;i++){
if (n%i==0){
return false;
}
}
}
return true;
}
/**
* 判断一个数字是否是否是回文数字
* 4554 这种
* @param num
* @return 是返回true 不是返回false
*/
public static boolean judgePalindrome(int num){
int remainder,sum=0;
int temp = num;
while(num>0){
remainder = num%10;
sum=(sum*10)+remainder;
num=num/10;
}
if (sum==temp){
return true;
}
else {
return false;
}
}
/**
* 求阶乘
* @param num
* @return
*/
public static int factorial(int num){
if (num==0){
return 1;
}
if (num<0){
return (num*factorial(num+1));
}
else
return (num*factorial(num-1));
}
/**
* judge a Armstrong number
*
*/
public static boolean judgeArmstrongNum(int num){
int tem = num;
int c = 0;
while(num>0){
int a = num%10;
num = num/10;
c += a*a*a;
}
if (tem == c){
return true;
}else {
return false;
}
}
}