I just started learning C programming and currently I'm on 'statement condition' topic which is "If's and Switch case". So I encountered this example program which a user input a number and will have an output as a worded value of the number.
Example:
Input:1234
Output: One thousand two hundred thirty four
So as the subject of my topic says, I'am really confuse on the operational part I mean the calculation part of the code...
Here's the source code:
Quote:
#include <stdio.h>
main()
{
int n,r,t,h,e,o;
printf("\n Enter number: ");
scanf("%d", &n);
t=n/1000;
n=n%1000;
h=n/100;
n=n%100;
if ((n>10) && (n<20))
{
e=0;
o=0;
r=n%10;
}
else
{
e=n/10;
n=n%10;
o=n;
}
switch(t){
case 1: printf(" One thousand");break;
case 2: printf(" Two thousand");break;
case 3: printf(" Three thousand");break;
}
switch(h){
case 1: printf(" One hundred");break;
case 2: printf(" Two hundred");break;
case 3: printf(" Three hundred");break;
case 4: printf(" Four hundred");break;
case 5: printf(" Five hundred");break;
case 6: printf(" Six hundred");break;
case 7: printf(" Seven hundred");break;
case 8: printf(" Eight hundred");break;
case 9: printf(" Nine hundred");break;
}
switch(r){
case 1: printf(" Eleven");break;
case 2: printf(" Tweleve");break;
case 3: printf(" Thirteen");break;
case 4: printf(" Fourteen");break;
case 5: printf(" Fiftenn");break;
case 6: printf(" Sixteeb");break;
case 7: printf(" Seventeen");break;
case 8: printf(" Eighteen");break;
case 9: printf(" Nineteen");break;
}
switch(e){
case 1: printf(" Ten");break;
case 2: printf(" Twenty");break;
case 3: printf(" Thirty");break;
case 4: printf(" Forty");break;
case 5: printf(" Fifty");break;
case 6: printf(" Sixty");break;
case 7: printf(" Seventy");break;
case 8: printf(" Eighty");break;
case 9: printf(" Ninety");break;
}
switch(o){
case 1: printf(" One");break;
case 2: printf(" Two");break;
case 3: printf(" Three");break;
case 4: printf(" Four");break;
case 5: printf(" Five");break;
case 6: printf(" Six");break;
case 7: printf(" Seven");break;
case 8: printf(" Eight");break;
case 9: printf(" Nine");break;
}
getch();
}
So here's the part where I get really confuse, so please guys spare me some of your knowledge and help me understand these lines of codes operates or how do they worked? I wanted this to be understood by me before proceeding on the next topic.. Please explain these lines to me.
Quote:
t=n/1000;
n=n%1000;
h=n/100;
n=n%100;
if ((n>10) && (n<20))
{
e=0;
o=0;
r=n%10;
}
else
{
e=n/10;
n=n%10;
o=n;
}
So in the codes there's "t=n/1000" I somewhat understand this line, so when I user input "1000" so it will fall along on the thousandth place which would result 1 and outputs "One thousand" but then the remaining codes on that operation is where I can't understand like how does the "% modulus" works on that code and the remaining lines? Please really help me guys please explain how the operation/calculations works?
Though I just posted this problem also on other forum site, but hmm... kind of a hassle because admin will approve first my post/replies their and it was three days and still my topic wasn't approve yet, I don't as of now didn't check my email if it was approve or not. AnywaysI hope someone here will also help me on this one.
Thanks.
-mc
