首页  >  文章  >  后端开发  >  在C程序中打印给定数字的个位数的倍数

在C程序中打印给定数字的个位数的倍数

王林
王林转载
2023-09-06 11:17:151202浏览

在C程序中打印给定数字的个位数的倍数

输入数字N,取出给定数字的个位数并显示该数字的倍数。

输入− N=326

输出− 个位数为 6,其倍数为 2 和 3

注意 − 任何数字的个位数都可以通过计算 %10 来获取数字

例如 - 如果给你一个数字 N 并且你需要找到它的个位数字

你可以使用 N%10 它将返回你的数字个位数字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删除