SEARCH

The How-To Geek Forums Have Migrated to Discourse

How-To Geek Forums / Geek Stuff

(Solved) - Need help: C programming

(9 posts)
  • Started 1 year ago by tech27
  • Latest reply from tech27
  • Topic Viewed 381 times

tech27
tech27
Posts: 387

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

Posted 1 year ago
Top
 
Lighthouse
Lighthouse
Posts: 13598

Please remember, that, HTG is a general computer help site. And whilst many members may specialise in certain things, it may be a while before they are online. That's why we recommend specialised sites, C programming in your case.

Posted 1 year ago
Top
 
tech27
tech27
Posts: 387

Yeah it's okay for me. I understand your concern. I will just wait for other members replies here that have some ideas on this one and I will also see my other posted topic on sites that had this specific category. Thanks

Posted 1 year ago
Top
 
Xhi
Xhi
Posts: 6298

At first n is the number typed in.

t=n/1000 gives the number of thousands. Since all variables are integer. ie. if n is 1312 then n/1000 in integer will be 1. Any decimal places are ignored. This will pick One Thousand from the t table.

n=n % 1000 -- The percent is an operator that means "the remainder when t is divided by 1000" ie. if n is 1312 then n % 1000 is 312.

now you need to know how many hundreds

It is done the same way. Since n is now 312

h = n/100 gives h=3 Which will pick Three Hundred from the h table
n = n%100 gives the remainder 12 and n is now 12.

The if statement is just to figure out which of the tables to use for the words for single and double digit numbers.

If the remainder, in our case 12, is greater than 10 and less than 20 you want to use the the first table (r) ie. eleven, twelve, etc. So the value of e and o are set to zero and r is set to the remainder of n%10, in our case 2. This will print Twelve the second value in the r table and nothing else from the other tables (e and o are 0).

The result will be One Thousand Three Hundred Twelve

However the else portion when the value of n is not between 10 and 20 will ignore the r table (r=0) and pick a value from the other two tables.
ex. if the remaining n is 67 (ie. if our number had been 1367 instead of 1312) then e becomes 6 and o becomes 7 taking the value Sixty from the e table and the value Seven from the o table.

The result would be One Thousand Three Hundred Sixty Seven.

Hope this helps.

Accepted Answer · Posted 1 year ago
Top
 
tech27
tech27
Posts: 387

Wow! This is a very concrete answer! Thank you very much Xhi for sharing your knowledge. I really appreciate this a lot because it makes me even easier to understand the whole codes on how it behaves line by line. This really helps me! and now I keep on reading your explanation so that it will really stick on my brain. Thank you very very much!

-mc

Posted 1 year ago
Top
 
tech27
tech27
Posts: 387

Actually C programming is our subject next year together with html&css and networking and yeah I'm studying this programming thing in advance so that it would be alot easier for me, the time I enrolled to this subject. So I could then somewhat understand on preceding activities or problems that I may encounter probably next school year and that would be on the first week on June. So yeah thanks for helping me out!

Posted 1 year ago
Top
 
Xhi
Xhi
Posts: 6298

Glad to be of service.

Posted 1 year ago
Top
 
Lighthouse
Lighthouse
Posts: 13598

Are you using the same C programming environment as your school will ask you to use ?

Posted 1 year ago
Top
 
tech27
tech27
Posts: 387

Yes, I ask my upper-class friends of mine and they're using same environment like the compiler is DevCpp

Posted 1 year ago
Top
 



Topic Closed

This topic has been closed to new replies.

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!