首頁  >  文章  >  後端開發  >  在C程式中列印給定數字的個位數的倍數

在C程式中列印給定數字的個位數的倍數

王林
王林轉載
2023-09-06 11:17:151203瀏覽

在C程式中列印給定數字的個位數的倍數

輸入數字N,取出給定數字的個位數並顯示該數字的倍數。

輸入− N=326

輸出− 個位元數為6,其倍數為2 且3

注意 − 任何數字的個位數都可以透過計算 來取得數字

例如- 如果給你一個數字N 並且你需要找到它的個位數字

你可以使用N 它將返回你的數字個位數字N

演算法

START
Step 1 -> Declare start variables num, num2 and i
Step 2 -> input number num
Step 3 -> store num%10 in num2 to fetch unit digit
Step 4 -> print num2
Step 5 -> Loop For i=2 and i<=num2/2 and ++i
   IF num2%i=0\
      Print i
   End IF
Step 6 -> End For Loop
STOP

範例

#include<stdio.h>
int main() {
   int num,num2,i;
   printf("</p><p>enter a number");
   scanf("%d" , &num);
   num2=num%10;    //storing unit digit in num2
   printf("</p><p> unit digit of %d is: %d",num,num2);
      for(i=2;i<=num2/2;++i) {    //loop till half of unit digit
      if(num2%i==0) { //calculate multiples
         printf("</p><p> multiple of %d is : %d ",num2,i);
      }
   }
return 0;
}

輸出

如果我們執行上面的程式,那麼它將產生以下輸出

enter a number329
unit digit of 329 is: 9
multiple of 9 is : 3
#

以上是在C程式中列印給定數字的個位數的倍數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除