👤

Write in pseudocode an algorithm to compute the sum of the numbers BETWEEN (and INCLUDING) p and q, where p, q are integers. There is no constraint for p and q (p could be smaller, equal to or greater than q).

The algorithm should work for the following cases:

p = 5, q = 9 => sum = 35

p = 7, q = 7 => sum = 14

p = 12, q = 8 => sum = 50


Răspuns :

read p,q
if p>=q then
| sum = p
| while p<=q execute
| | sum <- sum + p
| | p <- p + 1
| end_while
end_if
else
| sum = q
| while q<=p execute
| | sum <- sum + q
| | q <- q + 1
| end_while
end_else
print sum

intreg p,q,i,suma;

suma<-0;

citeste p,q ;

pentru i <-p,q, executa

suma<-suma+i;

scrie suma.