数字の最後の桁が同じかどうかを確認するJavaScriptプログラム

Javascript

例:最後の桁を確認する

/* program to check whether the last digit of three
numbers is same */

// take input
const a = prompt('Enter a first integer: ');
const b = prompt('Enter a second integer: ');
const c = prompt('Enter a third integer: ');

// find the last digit
const result1 = a % 10;
const result2 = b % 10;
const result3 = c % 10;

// compare the last digits
if(result1 == result2 && result1 == result3) {
    console.log(`${a}, ${b} and ${c} have the same last digit.`);
}
else {
    console.log(`${a}, ${b} and ${c} have different last digit.`);
}

出力

Enter a first integer: 8
Enter a second integer: 38
Enter a third integer: 88
8, 38 and 88 have the same last digit.

上記の例では、ユーザーは3つの整数を入力するように求められます。

3つの整数値は変数に格納されます Ab そして c

整数値の最後の桁は、剰余演算子を使用して計算されます %

% 余りの値を与えます。 例えば、 58%10 与える 8

次に、すべての最後の桁がを使用して比較されます if..else ステートメントと論理AND演算子 && オペレーター。



Hope this helps!

Source link

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です