So how to I write a program in turbo pascal, that takes a non-negative integer as input and writes out each of the digits on a separate line. I know how to use div and mod to separate the digits, it's just the fact that it can be an unlimited amount of digits. Here is what I got so far:

program example (input, output); var num, n, c: integer; begin write('Enter an Integer:'); readln(num); n:= num; repeat n:= n div 10; if (n > 0) then c:= c + 1; until(n<= 0); writeln(c); end.Now all this tells me is the amount of digits there is in the number. If the user puts in 1234 the output would be 4. Any help?

✔ Best Answer

Alright, since teach coughed up the answer, I'll go ahead and give you mine, for what it's worth:

program int (input, output);

var c,x,n,this: integer;

begin

write ('enter int: ');

readln (n);

x:=10000;

c:=0;

repeat

this:=n div x;

//all c does is act as a flag that the first non-zero number has been generated.

c:=c+this;

n:=n mod x;

x:=x div 10;

if c>0 then writeln(this);

until x=0;

end.As for the instructor's code, (or, the code you posted last), well, (ahem), I don't understand how it could possibly come close to functioning. There's appears to be an unterminated for-loop with no "next", and I have no clue what that z-900 subtraction does. Here's what I managed to dredge out of that (I tried to assign roughly the same roles to the variables, but there are differences):

program example2 (input, output);

var z,x,n,f: integer;

begin

write ('enter int: ');

readln (x);

//make a backup copy of "x", since it gets destroyed

n:=x;

z:=1;

repeat

x:=x div 10;

//this (mult) saves having to use a for-loop based on c to build z.

if x>0 then z:=z*10;

until x<=0;

repeat

f:=n div z;

n:=n mod z;

z:=z div 10;

writeln(f);

until z=0;

end.

Well, did the professor specify the order of the numbers (first to last, or last to first).

If he did not, then you can just put them out "opposite":

var

n: longint;

this: integer;

begin

write ('enter integer: ');

readln (n);

repeat

this:=n mod 10;

// technically, you could as easlily use: n:=n div 10;

n:=(n-this) div 10;

writeln(this);

until n=0;

end.If it turns out he wants them in major to minor order (reverse of this), let me know (or use your existing code to tell you what power of ten to start with, and work backwards)

Why to accept the typed number as an integer when it is manipulated as a numeric string? The following code fits the request while a check on numeric input should be added.

program example (input, output); var num: string; i: integer; begin write('Enter a positive number>'); readln(num); writeln; for i:= 1 to Length(num) do writeln(num[i]); end.

Well, she wants it in order. For example, if the user put in 1234 the output should be:

1

2

3

4We did not discuss longint, so I don't know what that is. Basically, I think we are just using loops and div and mod in this problem.

It's a "bigger number" (more digits). integer is confined to 32767 down to -32767 or something like that. With that in mind, instead of taking the number's measure, I would just start with 10000 and work down, only writing after a non-zero value has been encountered (so you don't get: 0 0 0 1 0 when user enters "10".) This may not be what instructor has in mind, so be warned. this is just the way I would work it.

Okay, this is what my teacher gave me as the answer. I guess she doesn't know how to solve it herself because the program doesn't work! Well my previous code is the same, she just added a couple things: * program example2 (input, output); Begin write('enter an integer:'); readln(x); n:=x; repeat n:= n div 10; if (n>0) then c:= c+1; until(x<=0); for y:= 1 to c do z:= z *10; for y:= 1 to c do begin f:= n div z p:= n mod z; z:= z - 900; writeln(f); end; writeln(p); end.Like I said this doesn't work at all. Is there an easier or shorter way to do this?

Alright, since teach coughed up the answer, I'll go ahead and give you mine, for what it's worth:

program int (input, output);

var c,x,n,this: integer;

begin

write ('enter int: ');

readln (n);

x:=10000;

c:=0;

repeat

this:=n div x;

//all c does is act as a flag that the first non-zero number has been generated.

c:=c+this;

n:=n mod x;

x:=x div 10;

if c>0 then writeln(this);

until x=0;

end.As for the instructor's code, (or, the code you posted last), well, (ahem), I don't understand how it could possibly come close to functioning. There's appears to be an unterminated for-loop with no "next", and I have no clue what that z-900 subtraction does. Here's what I managed to dredge out of that (I tried to assign roughly the same roles to the variables, but there are differences):

program example2 (input, output);

var z,x,n,f: integer;

begin

write ('enter int: ');

readln (x);

//make a backup copy of "x", since it gets destroyed

n:=x;

z:=1;

repeat

x:=x div 10;

//this (mult) saves having to use a for-loop based on c to build z.

if x>0 then z:=z*10;

until x<=0;

repeat

f:=n div z;

n:=n mod z;

z:=z div 10;

writeln(f);

until z=0;

end.

Thank you very much, my teacher isn't really skilled in pascal. She's more Java and C++. She trys though! Anywho, for the last program, how would I display the numbers on separate lines?

(?)

writeln doesn't work for that?

teach [enter]

enter int: _ [10]

1

0ps: if shes nice looking, hook me up! I'll help her with her homework ;-)

(just joking! just joking!)

I don't know Pascal, so I'm going to focus more on the logic than providing example code. The easiest way to do this is what IVOsuggests, and just treat it like the string it is. Pascal probably even has a function to convert an integer to a string, so there goes any challenge.If this must be done though math no matter what, this can be broken up into two parts.

1) Find the number of digits. The non-string method is to use a base-10 logarithm. Most languages have a log() function, but a quick search suggests Pascal does not, so you'll have to make do with ln(). The equation looks something like this:numberOfDigits = (ln(someNumber) / ln(10)) + 12) Display the digits. This is broken into two parts. Take your number, divide it by 10 to the power of(numberOfDigits - 1), and show it. Then take the modulus of the above, save it as your number, decrease the numberOfDigits by one, and repeat until your numberOfDigits < 0.

If your programming teacher can't handle part 2, your teacher is bad at the job of teaching programming.

Hi R: I assumed, since this was "classwork"/"homework"/excercise, that the string method was verboten, although it is the best method for real-world application. That's why I "plussed" Ivo's contribution, but i figured the instructor wanted to demo the fundamentals of division and modulo. Also, I am not Pascal proficient, even though that was my first computer language (1978!, using punch-cards no less! We had to wait hours for our programs to run, then got the syntax errors, Lol!) I was trying to figure out the function or operator to give "number of power-of-10" for a number and was stumped. Once again, I learn. Thanks!

Thanks. I've been learning pascal for a month or two, so i guess i'm a beginner. Your pretty good at this by the way, it seems. Anyway, thanks again!

Ask Your Question

Weekly Poll

Do you think Microsoft can save the Surface Book lineup?

Discuss in The Lounge

Poll History