Hello!

So I have some programming basics in secondary school and I'm learning Pascal. But I have a tiny little problem. I need to make a program that counts together digits in all numbers from 1 to 1000. For example, 22 - 2+2=4 23 - 2+3=5 But I need to count all the results together too. I know I have to use functions like for x:=1 to 1000 do a MOD b a DIV b but I can't figure it out. :(

11 years ago*

Comment has been collapsed.

what you really need is not MOD or DIV... just -> out := IntToStr(x)
and now in out you have all your digits which you can add :]

11 years ago
Permalink

Comment has been collapsed.

Well, my school is using FreePascal which doesn't have all smart functions and stuff so we have to pull out some nifty stuff with simpliest functions. So this one really doesn't help much.:/

11 years ago
Permalink

Comment has been collapsed.

OK, I just re-red what you are trying to do. So you want to sum the numerical digits in a number.
This means you have to extract the last digit and to add it to a sum number that has the initial value set to 0.

So you do something like this:
n=2013
s=0
s=s+n MOD 10
n=n DIV 10
and you do this until n=0 (2013/10=201;201/10=20;20/10=2;2/10=0 - last digit to add is the first in the number aka 2 and it will always be lower than 10 so your DIV will always be 0) - this is your condition.

If you want to add more numbers and to sum all digits in one huge number, create another var, let's say global_sum=0 and when you finish with the first number ask the user to add another number and then you do global_sum = global_sum+s.
Can't find a Pascal version now but if you want I'll post a C++ version for reference.

In C++ we use "while" statement for this: while(n!=0){instructions_here}

11 years ago
Permalink

Comment has been collapsed.

Been some time since I did PASCAL but Runaurufu seems to have a point.
I'm surprised they still teach Pascal. (they do it here too, I just thought we are "special").

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 2 years ago.

11 years ago
Permalink

Comment has been collapsed.

That's what I keep hearing but it's not too practical.
Not saying it isn't but I prefer a more practical approach. I felt more motivated while learning C++ & C#.
But I guess it's just me.

11 years ago
Permalink

Comment has been collapsed.

yep learning language which is used only in schools and in some programs remembering late 90'... great idea ;]
it is neither robust, nor programmer friendly... not mentioning those modern and powerful IDEs.

11 years ago
Permalink

Comment has been collapsed.

:)) I remember back in highschool when I did C++ we used to use Turbo C++ 4 or something like that, that run in a DOS fullscreen interface.
I got pissed off and start using DEV C++ at home and installed one at school because I was too lazy changing the code to run on Borland and got a 2 (guess this counts as F- or something) for initiative :))

11 years ago
Permalink

Comment has been collapsed.

OMG... Borland... I remember when I first time used winforms on VS and this feeling of simplicity in comparison to Borland way of handling forms was so staggering that I simply felt in love with VS :x Sometimes I think that this easy usage of forms is only reason why I didn't choose java as profession.

11 years ago
Permalink

Comment has been collapsed.

Exactly. For me, VS is the best IDE out there and the form editor in VS simply rocks. I had the same feeling when I first tried VS. It was 2k3 or something like that. The first .NET version.
I tried using NetBeans editor for Java and I was like...WTF :))

11 years ago
Permalink

Comment has been collapsed.

It really depends on the purpose of learning programming.
If you're trying to learn about application development, then I agree things like C++/# would be great. But if its more for learning about algorithms and problem solving, I feel like python and especially pascal has no overhead code you have to deal with.

11 years ago
Permalink

Comment has been collapsed.

C++ is evil... Very usefull, but also hard language in some cases.

11 years ago
Permalink

Comment has been collapsed.

We've been taught pascal when we were on highschool and it was pretty hard to grasp immediately for some.
I remember making a fortune back them coding their assignments. Looking back now I would say python would be the best language to teach for entry level programmers followed by C

11 years ago
Permalink

Comment has been collapsed.

Python is nice and simple also later it has lot of more modern things like objects...

C is good for understanding what goes under lot of stuff, but it's not really too simple to start with...

11 years ago
Permalink

Comment has been collapsed.

It never is. I still have some issues with pointers lol :)) I mixed em up quite often.

11 years ago
Permalink

Comment has been collapsed.

Deleted

This comment was deleted 2 years ago.

11 years ago
Permalink

Comment has been collapsed.

I HATE PASCAL

11 years ago
Permalink

Comment has been collapsed.

google it

11 years ago
Permalink

Comment has been collapsed.

I have no idea what you are trying to do..

Do you mean add each number together, like
1+2=3, 2+3=5, 3+5=8

all the way to 1000?

11 years ago
Permalink

Comment has been collapsed.

I think its:
1 = 1
...
11 = 1+1 = 2
1000 = 1 = 1
And then sum those.

11 years ago
Permalink

Comment has been collapsed.

I'm not sure but here's some sloppy pascalling from me, not sure if it will even compile.

But this is the gist of what i thought you are trying to do

http://pastebin.com/pvKA4h65

11 years ago
Permalink

Comment has been collapsed.

Hmm, never done pascal, lately C...

Now to think most optimal ways of doing this ;D
There probably is some algorithms for this...

11 years ago
Permalink

Comment has been collapsed.

this is the rough pseudo code but

for x = 1:1000

sumdigits[x] = 0 (this is an array to store your answers)

while x <> 0

sumdigits[x] = sumdigits + x mod 10

x = x mod 10

end while

end for

11 years ago
Permalink

Comment has been collapsed.

For just 1 to 1000:

I know it's bad solution ;D

11 years ago
Permalink

Comment has been collapsed.

Why does schools still use Pascal? It's outdated.
Python is much better alternative.

11 years ago
Permalink

Comment has been collapsed.

You can laugh if you want but they teach Pascal in high schools and universities for us. HAHA.

11 years ago
Permalink

Comment has been collapsed.

Pascal is old bro, nobody's gonna hire you because you know Pascal. Learn Java or something.

11 years ago
Permalink

Comment has been collapsed.

I have to learn it in school. I know php, html and CSS but I have no choice when it comes to Pascal - I just have to learn it because I don't want to get 2 (I think it's F or F- in USA).

11 years ago
Permalink

Comment has been collapsed.

Fast code:

var i, count:integer; n1,n2,n3:integer;
begin
for i:=1 to 999 do
begin
n1:=i div 100;
n2:=(i mod 100) div 10;
n3:=(i mod 100) mod 10;
count:=count + n1 + n2 + n3;
end;
count:= count + 1; //last number is for 1000 (1+0+0+0 = 1)
writeln('Total count= ', count);
end.

11 years ago
Permalink

Comment has been collapsed.

Oh wow thanks! I got my own solution too but I'll check your out.
This is my solution:
program summa;
uses crt;
var i, x, a, b :longint;
begin
clrscr;
writeln('Skaitļu no 1 līdz 1000 ciparu summa ir:');
i:=1+2+3+4+5+6+7+8+9;
x:=i10;
a:=x
10;
b:=a*3+1;
writeln(b);
readln;
end.

11 years ago
Permalink

Comment has been collapsed.

Closed 11 years ago by ExDec.