9、判斷一個數最多能被幾個9整除

編程題目:

9.判斷一個數最多能被幾個9整除。

示例代碼:

package program.calculation.exercise09;

import java.util.Scanner;

/**
 * 9.判斷一個數最多能被幾個9整除。
 */

public class NineDivide {
    public static void main(String[] args) {

        System.out.println("請輸入一個數:");
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();

        countNine(num);

    }

    //計數
    private static void countNine(int num) {

        int count = 0;
        int number = num;
        while (number >= 9) {
            number /= 9;
            count++;
        }
        System.out.println(num+"最多能被"+count+"個9除");

    }
}

結果顯示:

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章