New to this forum and fairly new in pascal. Trying to solve this problem. A pascal program that inputs an integer larger than 1 and calculates the sum of the squares from 1 to that integer.For example, if the integer is 4, the sum of the squares is 30(1+4+9+16).The output is to be the value of the integer and the sum.
This is what I have done so far, but the program isn't giving me what i want.Any ideas please?
Code:program Untitled; VAR Square, Integer_Value, Sum, Count: Integer; BEGIN Count := 0; Sum := 0; Writeln('Enter integer value'); Readln(Integer_Value); WHILE Count <= Integer_Value DO BEGIN Read(Integer_Value); Square := (Count * Count); Sum := Sum + Square; Count := Count + 1; Writeln('Sum'); Writeln('Integer_Value'); END END.
Why are you re-reading Integer_Value inside your while loop? Get rid of that line and you should be in good shape. Also, move the writeln's outside the while loop, or they'll print for every intermediate step.
Did some changes based on the comments but still have an error.Having errors with the last 2 writeln statements.
Here is the code:
Code:program Untitled; VAR Square, Integer_Value, Sum, Count: Integer; BEGIN Count := 0; Sum := 0; Writeln('Enter integer value'); Readln(Integer_Value); WHILE Count <= Integer_Value DO BEGIN Square := (Count * Count); Sum := Sum + Square; Count := Count + 1; END Writeln('Sum'); Writeln('Integer_Value'); END.
You're writing out a string, but not the variables themselves.Code:Writeln('Sum: ',Sum); Writeln('Integer_Value: ',Integer_value);
Still have an error on the last two writelns.
Code:program Untitled; VAR Square, Integer_Value, Sum, Count: Integer; BEGIN Count := 0; Sum := 0; Writeln('Enter integer value' ); Readln(Integer_Value); WHILE Count <= Integer_Value DO BEGIN Square := (Count * Count); Sum := Sum + Square; Count := Count + 1; END Writeln('Sum: ', Sum); Writeln('Integer_Value: ',Integer_Value); END.
What is the error?
You missed a semicolon. Above the writeln and after the END
Thanks all,finally it worked.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks