i need to use integer division and the remainder operation. eg. if i type 42139 it should look like 4 2 1 3 9. help please!!!
x=52
52%10=2
52-2=50
50/10=5
117%100=17
117-17=100
100/100=1
below is my code so far
# include %26lt;stdio.h%26gt;
int main()
{
int a,b,c,d,e;
printf("enter 5 single digits\n");
scanf("%f%f%f%f%f", %26amp;a,%26amp;b,%26amp;c,%26amp;d,%26amp;e,)
a=%f
}
Write a c program that inputs one five digit number, separtes the number into its individual digits.?
Since you know it is 5 digits long you could:
1. Divide the input by 10000 (notice its 5 digits long as well). The reason being that in computers dividing integers always returns an integer then your result will be a whole # and so e.g., 42139 / 10000 = "4"
2. Subtract: your answer from #1 which is 1st multiplied by 10000 from the original 5-digit input e.g. 42139 - 40000 = 2139
3. Now, divide 2139 by 1000 (notice its 4 digits)...this gives you the answer of: "2"
2. Subtract: your answer from #3 which is 1st multiplied by 1000 from the 4-digit input mentioned in #2 e.g. 2139 - 2000 = 139
3. and so on....
It should be noted that the modulus (remainder operator could be used instead where i was multipying the result with the 10^x in the steps above...it was just merely preference for me)
Reply:Don't want to do your homework, but your code won't work, you need %d instead of %f. But as another poster said, why not scan it as a string and output it as chars?
char ins[80];
int c;
printf("enter a five digit number: ");
scanf("%s", ins); /* this will break if they enter %26gt; 80 */
for(c = 0; c %26lt; 5; c++){
putchar(ins[c]);
putchar(' '); /* that's a space between the quote marks */
}
putchar('\n');
Reply:I would read the number in as a string and then using the substr functions, i would retrieve each digit.
Good luck.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment