撰寫一個程式讀入一個介於100 - 999的數字,然後顯示它的每位數和、積和差。例如932的每位數和是14、每位數積是54、每位數差是4。(百位數減十位數再減個位數)
import java.util.Scanner; public class C1 { public static void main(String[] args) { Scanner input =new Scanner (System.in); //創建並提示使用者輸入數字 System.out.println("please enter a number between 100 and 999:"); int a = input.nextInt(); //計算答案 int b = a / 100 ; int c = ( a - 100 * b ) / 10; int d = a - 100 * b - 10 * c ; int e = b + c + d; int f = b * c * d; int g = b - c - d; //印出結果 System.out.println("The sum is:" + e); System.out.println("The product is:" + f); System.out.println("The difference is:" + g); } }
沒有留言:
張貼留言