Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Tuesday, March 7, 2023

Qbasic program to find the sum of input digits


Qbasic program to find the sum of input digits


Qbasic program to find the sum of input digits


Introduction:


In the early days of personal computing, QBasic was a popular programming language for beginners. QBasic is a simple language that can be used to create small applications or games. In this article, we will show you how to write a Qbasic program to find the sum of input digits.

Explanation of the program:


The program will start by asking the user to input a number. Once the number is entered, the program will calculate the sum of the digits in the number and display the result. Here is the code:

CLS
INPUT "Enter a number: ", num
sum = 0
WHILE num > 0
digit = num MOD 10
sum = sum + digit
num = INT(num / 10)
WEND
PRINT "The sum of the digits is: "; sum
END


Explanation of Code:


  • CLS clears the screen before the program starts.
  • INPUT asks the user to enter a number and stores it in the variable num.
  • sum is initialized to 0.
  • The WHILE loop will continue until num is no longer greater than 0.
  • digit is assigned the value of the last digit in num.
  • sum is updated with the value of digit.
  • num is divided by 10 and rounded down to remove the last digit.
  • The loop continues until all the digits have been processed.
  • The result is displayed using PRINT statement.

Qbasic program to find the sum of input digits using SUB procedure

DECLARE SUB digit ()
CLS
CALL digit
END


SUB digit
INPUT "Enter a number: "; num
sum = 0
WHILE num > 0
di = num MOD 10
sum = sum + di
num = INT(num / 10)
WEND
PRINT "Sum of the digits is: "; sum
END SUB



Qbasic program to find the sum of input digits using FUNCTION procedure

DECLARE FUNCTION digit (num)
CLS
INPUT "Enter a number: "; num
PRINT "Sum of the digits is: "; digit(num)
END


FUNCTION digit (num)
WHILE num > 0
di = num MOD 10
sum = sum + di
num = num \ 10
WEND
digit = sum
END FUNCTION


Output:





Conclusion:


In this article, we have shown you how to write a Qbasic program to find the sum of input digits without modular programming and by using modular programming using SUB and FUNCTION procedure. This program is a simple example of how to use loops and variables in QBasic. By experimenting with this code, you can learn how to write more complex programs in QBasic.

You may also read:

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages