Board index » delphi » C++ Translation to Pascal

C++ Translation to Pascal

Can anyone help me translate this code written in C++ into Pascal:

#include <iostream.h>

void main()

{
int sum = 0;
int x;
cout<< "Enter a number: ";

cin>> x;

for (int i = 1; i <= x; ++i)

sum = sum + i;

cout <<sum  <<endl;

Quote
}

 

Re:C++ Translation to Pascal


In article <eC0H6.41194$RF1.3501...@bgtnsc06-news.ops.worldnet.att.net>,

Quote
Joseph Lazarus wrote:
> Can anyone help me translate this code written in C++ into Pascal:

> #include <iostream.h>

> void main()

> {
> int sum = 0;
> int x;
> cout<< "Enter a number: ";

> cin>> x;

> for (int i = 1; i <= x; ++i)

> sum = sum + i;

> cout <<sum  <<endl;

> }

{untested:}
Program somesum;

var sum,x,i : longint;

begin
 sum:=0;
 writeln('Enter a number: '); read(x);
 for i:=1 to x do
  inc(sum,i);
 writeln(sum);
end.

Re:C++ Translation to Pascal


Joseph Lazarus schreef:

Quote

> Can anyone help me translate this code written in C++ into Pascal:

> #include <iostream.h>

> void main()

> {
> int sum = 0;
> int x;
> cout<< "Enter a number: ";

> cin>> x;

> for (int i = 1; i <= x; ++i)

> sum = sum + i;

> cout <<sum  <<endl;

> }

Yes.

program void;
var sum,i,x:longint;
begin
  sum:=0;
  write('Enter a number: ');
  read(x);
  i:=1;
  while (i<=x) do
  begin
    sum:=sum+i;
    i:=i+1
  end;
  writeln(sum)
end.

Huub.

Re:C++ Translation to Pascal


"Huub van Dooren" <hvdoo...@iae.nl> wrote in message
news:3AECC6AB.BB90AC11@iae.nl...

Quote
> Joseph Lazarus schreef:

> > for (int i = 1; i <= x; ++i)
> > sum = sum + i;

>   i:=1;
>   while (i<=x) do
>   begin
>     sum:=sum+i;
>     i:=i+1
>   end;

Doesn't ++i increment the counter before loop
execution, rather than after it?

--
The Scarlet Manuka

Re:C++ Translation to Pascal


The Scarlet Manuka <sa...@maths.uwa.edu.au> wrote:

Quote
> "Huub van Dooren" <hvdoo...@iae.nl> wrote in message
> news:3AECC6AB.BB90AC11@iae.nl...
> > Joseph Lazarus schreef:

> > > for (int i = 1; i <= x; ++i)
> > > sum = sum + i;

> >   i:=1;
> >   while (i<=x) do
> >   begin
> >     sum:=sum+i;
> >     i:=i+1
> >   end;

> Doesn't ++i increment the counter before loop
> execution, rather than after it?

No. You switched something:

++i is like:
function preinc (var x: integer): integer;
begin
     x := x + 1;
     preinc := x;
end;

i++ is like
function postinc (var x: integer): integer;
begin
     postinc := x;
     x := x + 1;
end;

and for(A;B;C) {D} is like
label CONTINUE, BREAK;
A;
while B do
begin
     D; { break => goto BREAK; continue => goto CONTINUE;
CONTINUE:
     C;
end;
BREAK:

--
#!/usr/bin/perl -W -- WARNING: This will print 22,307 bytes! <strictsafe!>
use strict;for(my$y=-1;$y<1;$y+=.1){for(my$x=-1.9;$x<.4;$x+=.03){print'+';
my$X=my$Y=0;for(0..99){($X,$Y)=($X*$X-$Y*$Y+$x,2*$X*$Y+$y);print"\b "if$X*
$X+$Y*$Y>9;}}print"\n"};print''.reverse"\nHPAJ \a!rezloP .R yb torblednaM"

Re:C++ Translation to Pascal


Quote
"Rudolf Polzer" <e...@durchnull.de> wrote in message

news:slrn9eqagc.5jn.eins@www42.t-offline.de...

Quote
> The Scarlet Manuka <sa...@maths.uwa.edu.au> wrote:

> > Doesn't ++i increment the counter before loop
> > execution, rather than after it?

> No. You switched something:

Hmmm. I had the impression that it also applied to
loop control... did that change between C and C++,
or am I just imagining things again?

--
The Scarlet Manuka

Re:C++ Translation to Pascal


The Scarlet Manuka <sa...@maths.uwa.edu.au> wrote:

Quote
> "Rudolf Polzer" <e...@durchnull.de> wrote in message
> news:slrn9eqagc.5jn.eins@www42.t-offline.de...
> > The Scarlet Manuka <sa...@maths.uwa.edu.au> wrote:

> > > Doesn't ++i increment the counter before loop
> > > execution, rather than after it?

> > No. You switched something:

> Hmmm. I had the impression that it also applied to
> loop control... did that change between C and C++,
> or am I just imagining things again?

The latter one. It is exactly the same in C, C++, Perl, JavaScript and
Java.

--
#!/usr/bin/perl -W -- WARNING: This copies a random file from
use strict;my$s;my$n=0;for # the  current  directory  to your
(<*>){++$n;int rand$n or$s # signature  file.   Use  at  your
=$_};`cp $s ~/.signature`; # own risk! (c) 2001 Rudolf Polzer

Re:C++ Translation to Pascal


Thanks guys!
Quote
"Joseph Lazarus" <bren...@worldnet.att.net> wrote in message

news:eC0H6.41194$RF1.3501248@bgtnsc06-news.ops.worldnet.att.net...
Quote
> Can anyone help me translate this code written in C++ into Pascal:

> #include <iostream.h>

> void main()

> {
> int sum = 0;
> int x;
> cout<< "Enter a number: ";

> cin>> x;

> for (int i = 1; i <= x; ++i)

> sum = sum + i;

> cout <<sum  <<endl;

> }

Re:C++ Translation to Pascal


Quote
"Rudolf Polzer" <e...@durchnull.de> wrote in message

news:slrn9eqads.mm4.eins@www42.t-offline.de...

Quote
> The Scarlet Manuka <sa...@maths.uwa.edu.au> wrote:

> > loop control... did that change between C and C++,
> > or am I just imagining things again?

> The latter one. It is exactly the same in C, C++, Perl, JavaScript and
> Java.

Oh, that's okay then, I'm just going crazy. No problem there.

--
The Scarlet Manuka

Other Threads