每日编程练习(打卡第二天)

这是每日编程练习的第二天,涵盖了判断回文数、计算数字各位之和、打印图形以及求平方根等基础编程题,旨在提升编程技能。
摘要由CSDN通过智能技术生成

1.判断回文数:

import java.util.Scanner;

public class HuiWenZhengShu {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个数字:");
        int num = sc.nextInt();
        if(isPaildrome(num)){
            System.out.println("是回文数");
        }else{
            System.out.println("不是回文数");
        }
    }
    public static boolean isPildrome(int num){
        return reverse(num) == num;
    }
    public static int reverse(int sum){
        int sum = 0;
        while(true){
            sum = sum * 10 + num % 10;
            num /= 10;
            if(num == 0){
                return sum;
            }
        }
    }
}

2.求数字各项和:

import java.util.Scanner;

public class ZiChuan01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.pritln
        int num = sc.nextInt();
        int sum = sumDigtal(num);
        System.out.println(num);
    }
    public static int sumDigtal(int n){
        int sum = 0;
        while(true){
            sum += n % 10;
            n /= 10;
            if(n == 0){
                return sum;
            }
        }
    }
}

3.打印图形:

import java.util.Scanner;

public class PrintTuXing {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入行数:");
        displayPaattern(scanner.nextInt());
    }

    private static void displayPaattern(int line) {
        for (int i = 1; i <= line; i++){
            for (int k = 1; k <= line - i; k++){
                System.out.print("  ");
            }
            for (int j = i; j >= 1; j--){
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

4.平方根:

public class PingFangGen {
    public static void main(String[] args) {
        System.out.println(sqrt(9));
    }
    public static double sqrt(long n){
        double lastGuess = 1;
        double nextGuess = (lastGuess + n / lastGuess) / 2;
        while (true){
            if (Math.abs(nextGuess - lastGuess) < 0.0000001){
                return nextGuess;
            }
            lastGuess = nextGuess;
            nextGuess = (lastGuess + n / lastGuess) / 2;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值