Write a Pascal program that would read alist of integers, separated by space, in
ID: 3613197 • Letter: W
Question
Write a Pascal program that would read alist of integers, separated by space, into a stringvariable. Your program must then add upthe integers in that list. You would be required
to first extract the numerical values fromthe string and then add them up.
Your program should look as follows:
Enter a list of integers separated byspace: 1 2 3 4 5
The sum of your list is 15
Remember that the list ‘1 2 3 4 5’ is to be stored in astring variable. The user may enter as
many numbers as he/she wishes.
********************************************************************
So far i have got this in the body:
write('Enter a list of integers separated byspace: ');
readln(str);
Then i thought i would extract the numerical values by this:
for i:=1 to length(str); DO
if str[i]<>' ' THEN
....??write(a[i])??.....
Now i'm not sure how to save that output into a new stringvariable. Once that is done I can convert that variable to integerby using the val function(which i'm not too sure exactly how itworks)
and then i guess summing from here should not be too hard.
If someone could please just tellme how I can extract and save the numerical values of the input toa new string, im sure I can finish from there, or possiblygive other ideas to solve this
Explanation / Answer
Dear User,I try to another way. program sum;
var
counter: integer;
number: integer;
sum: integer;
begin
sum := 0;
write('Enter a list of integers separated by space:'); readln(number); for counter := 1 to number do
writeln(counter); repeat
readln(number);
sum:=sum+number;
until (number = 0);
writeln(number); readln; writeln(sum);
readln; end ITS HELPFUL TO YOU.......
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.