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}
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").
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.
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 :))
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.
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 :))
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.
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
Comment has been collapsed.
It never is. I still have some issues with pointers lol :)) I mixed em up quite often.
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
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.
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:=x10;
b:=a*3+1;
writeln(b);
readln;
end.
Comment has been collapsed.
365 Comments - Last post 1 hour ago by dailylhama
5 Comments - Last post 1 hour ago by Steamgifty
47,146 Comments - Last post 2 hours ago by FranckCastle
19 Comments - Last post 4 hours ago by juryman00
39 Comments - Last post 11 hours ago by Massulan
50 Comments - Last post 11 hours ago by wigglenose
27 Comments - Last post 12 hours ago by Foxhack
2,524 Comments - Last post 10 minutes ago by krol7
746 Comments - Last post 35 minutes ago by krol7
19 Comments - Last post 44 minutes ago by m0r1arty
10 Comments - Last post 1 hour ago by herbesdeprovence
61 Comments - Last post 1 hour ago by SergeD
34 Comments - Last post 1 hour ago by squall831
15 Comments - Last post 1 hour ago by antidaz
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. :(
Comment has been collapsed.