c - Program to separate digits of a number using asterisk -
i building program ask user input 5 digit positive number. display each of digits separated asterisk. example, if user inputs 51408
, output 5*1*4*0*8
.
but, can't seem logic of finding separate digits (of number). how should it?
now, code:
#include <stdio.h> int main() { int mnumber, num5, num4, num3, num2, num1; printf("input number:"); scanf ("%d", &mnumber); //these ones have trouble can't seem logic// num5 = (mnumber/10000) / 1; num4 = (mnumber/1000) / 1; num3 = (mnumber/100) / 1; num2 = (mnumber/10) / 1; num1 = (mnumber/1) / 1; printf("=%d", num5); printf("*%d", num4); printf("*%d", num3); printf("*%d", num2); printf("*%d", num1); return 0; }
to solve problem using approach, have remove significant part of number, once have processed it. example:
num5 = mnumber/10000; mnumber -= num5*10000; num4 = mnumber/1000; mnumber -= num4*1000; num3 = mnumber/100; mnumber -= num3*100; num2 = mnumber/10; mnumber -= num2*10; num1 = mnumber;
however, think cleaner convert string , print out each character of string, alternating asterisk, @clcto demonstrates.
Comments
Post a Comment