2014年7月11日 星期五

java程式設計題目---數字處理【拆解位數 ( 100~999 )】

Write a program that reads an integer between 100 and 999, and show the result of the sum, product and difference of all its digits. For example, if an integer is 932, the sum of all its digits is 14, the product of all its digits is 54, and the difference of all its digits is 4.(The equation of the difference is hundred's digit minus ten's digit minus unit's digits.)

撰寫一個程式讀入一個介於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);
  }
}

沒有留言:

張貼留言

Go(Golang)程式語言 設定GCC

下載MSYS2:  https://www.msys2.org/ 安裝 程式開始執行後輸入: pacman -Syu y y 在安裝目錄底下找到msys2.exe,雙擊執行 輸入指令: pacman -Su pacman -S --needed base-devel mingw-...