Question:
Borland C++ Pascal Help !?
Pirosca
2011-02-17 05:30:04 UTC
Does anyone know how to count the Numerical digits in a number?
Please answer, and explain step by step...and if you can make me some problems do work on.
Three answers:
mystic smeg
2011-02-17 06:23:27 UTC
Like Colanth says,

You can convert a number to a string, then count the number of characters in the string.

Or if you're just counting the characters, just input a string (see below);



na





---- Pascal listing ----



program Digits;



var s: string;

begin

Write('Please enter a number: ');

ReadLn(s);

WriteLn('The number has ',Length(s),' digits.');

{use Str(i,s); to convert from integer to string}

WriteLn('Press enter to continue...');

ReadLn;

end.



---- C listing ----



#include

#include

int main(void) {

//define variables

char s[10] = "";

printf("\nEnter a number: ");

scanf("%s", s);

fflush(stdin);

printf("\nThe number has %d digits.",strlen(s));

printf("\nPress enter to continue... ");

scanf("",s);



return 0;

}
2011-02-17 13:34:55 UTC
In C++ or in Pascal. They two different languages, and the code is totally different.



But ...

Numbers have a value, strings have digits. Convert the number to a string and the length is the number of digits. (Or you could use a switch for the value from 0-99, 100-999, etc., but that's very inelegant.)
?
2011-02-17 13:44:56 UTC
Read the Manuals.

print "hello.world"

Additional Information



Again (as it can't be said enough) RTFM! Please read the manual. I know it is not THAT interesting, but if you encounter problems it is the first place to look.



If you want to start programming in Pascal, I suggest you take a look at your local search engine or surf to www.friends-of-fpc.org (if you are not reading this text there that is ;). Note that most tutorials in the web are on Borland/ Turbo Pascal. If so, try giving the Argument "-So" if you encounter problems compiling TP code.



Again something on Editors/ IDEs etc. There ARE special IDEs for the Free Pascal Compiler. For example "WinFPC". But WinFPC does not seems to get updated anymore and has some nasty bugs. So I would recommend using some kind of Editor until somebody takes the time to code a real Windows IDE.



Well, I guess that's all I have to say about getting started with FPC. I hope everything went well with the installation. Again: if you have some questions, remarks, whatever, conact me at delax@sundancerinc.de. Bye then. Have fun!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...