write a qbasic program to display the sum of even numbers and odd numbers separately between 20 to 30.
Answers
Answered by
3
Solution:
The given code is written in QBASIC -
CLS
EVEN_SUM = 0
ODD_SUM = 0
FOR I = 20 TO 30
IF I MOD 2 = 0 THEN
EVEN_SUM = EVEN_SUM + I
ELSE
ODD_SUM = ODD_SUM + I
END IF
NEXT I
PRINT "SUM of Even Numbers: "; EVEN_SUM
PRINT "SUM of Odd Numbers: "; ODD_SUM
END
Logic:
- Logic is very simple, loop through numbers in the given range. If the number is divisible by 2, add the number to EVEN_SUM variable or else, add the number to the ODD_SUM variable. At last, display the values of the variable.
See the attachment for output.
•••♪
Attachments:
Similar questions