Friday, July 31, 2009

It's about C program... please help.?

#include%26lt;stdio.h%26gt;


void pw(long,char[]);


char *one[]={" "," one"," two"," three"," four"," five"," six"," seven"," eight"," Nine"," ten"," eleven"," twelve"," thirteen"," fourteen"," fifteen"," sixteen"," seventeen"," eighteen"," nineteen"};


char *ten[]={" "," "," twenty"," thirty"," forty"," fifty"," sixty"," seventy"," eighty"," ninety"};





void main()


{


long n;


clrscr();


printf("Enter the no: ");


scanf("%9ld",%26amp;n);


if(n%26lt;=0)


printf("Enter numbers greater than 0");


else


{


pw(((n/1000)%100),"thousand");


pw(((n/100)%10),"hundred");


pw((n%100)," ");


}


getch();


}





void pw(long n,char ch[])


{


(n%26gt;19)?printf("%s %s ",ten[n/10],one[n%10]):printf("%s ",one[n]);


if(n)printf("%s ",ch);


}


==========


Output is:


Enter the no.: 25


twenty five


=========


can someone explain the code:


pw(((n/1000)%100),"thousand");


pw(((n/100)%10),"hundred");


pw((n%100)," ");





--what's the use of that code? i mean the use of %100, /100, etc? thanks

It's about C program... please help.?
The % is modulus and the / is division.





If the input is 6543 for instance, then:


pw(((n/1000)%100),"thousand");


(1) 6543 / 1000 = 6


(2) 6 % 100 = 6


The first value passed to pw function is 6 for 6 thousand.





pw(((n/100)%10),"hundred");


(3) 6543 / 100 = 65


(4) 65 % 10 = 5


The first value is 5 for 5 hundred.





pw((n%100)," ");


(5) 6543 % 100 = 43


The first value is 43 for 43 (ones).





Now, a brief definition of modulus.


Modulus of a number is the remainder after dividing the number by another number. For example,





If you the first number is 198, and the you want to find the 10 modulo of 198, represented 198 % 10, you do the following procedure.





(10) get the input number (i) // 98


(11) get the modulo number (m) // 10


(12) divide (i) by (m) // 198 / 10, [edit] 98 if 198 / 100 ( 198 % 100) [/edit]


(13) if the number remaining is greater than m


(13.1) then goto (11)


(13.2) else the number remaining is the modulus // 8





Sorry about the two similar names, modulo number and modulus.
Reply:the three lines of code do the following:





-if the number is greater than 1000 it puts the suffix "thousand"


-if the number is greater than 100 it puts the suffix "hundred"


-if the number is greater than 1000 it just states the output of the function "pw"





-the function pw tests the numer and transforms it into text :)


-the modulo operator (%) returns the numer that is left after substracting the most multplid product of the other number (i am ambiguous); let me explain:


47%5=47-45=2


(i subsracted 45 becouse it is the biggest number that divides exactly to 5 and is smaller than 47)
Reply:% is the symbol for the modulus operator. The modulus of 2 numbers is the remainder of 1 number repeatedly subtracted from the other.





Eg. 8 mod 7 = 1


4 mod 1 = 0


33 mod 30 = 3





The code is trying to work out how many thousands, hundreds and tens are in a given number.


No comments:

Post a Comment