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

Wednesday, December 7, 2022

Write a QBASIC program to check the input number is Armstrong number or not

Write a QBASIC program to check the input number is Armstrong number or not

Write a QBASIC program to check the input number is  Armstrong number or not

QBasic is a high-level programming language that is widely used for developing software applications. It is a simple yet powerful language that can be used for a variety of applications, including mathematical calculations. In this article, we will discuss how to write a QBASIC program to check whether the input number is an Armstrong number or not.

What is an Armstrong Number?


An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153. Similarly, 371, 407, and 1634 are also Armstrong numbers.

Writing a QBASIC Program to Check for Armstrong Numbers

CLS

INPUT "Enter any number:"; d

n = d

su = 0

WHILE d > 0

re = d MOD 10

su = su + re ^ 3

d = INT(d / 10)

WEND

IF n = su THEN

PRINT n; "is Armstrong number"

ELSE

PRINT n; "is not Armstrong number"

END IF

END



Explanation:


  • The above program is written in QBASIC and is used to check whether a given number is an Armstrong number or not.
  • First, the user is prompted to enter a number. The program stores the number in the variable "d" and also creates a copy of the number in the variable "n".
  • Then, the program initializes the variable "su" to 0.
  • The program uses a "WHILE" loop to iterate over each digit of the input number. In each iteration, the program uses the "MOD" operator to extract the last digit of the number and stores it in the variable "re". The program then adds the cube of the digit to the variable "su". Finally, the program updates the value of "d" to remove the last digit.
  • After the loop is complete, the program checks whether the original number "n" is equal to the value of "su". If the two values are equal, the program prints a message that the input number is an Armstrong number. If they are not equal, the program prints a message that the input number is not an Armstrong number.
Overall, this program can be used to check whether a given number is an Armstrong number or not and can be easily modified to check multiple numbers using a loop.

We can write the above program by using the SUB and FUNCTION procedures.

Using SUB procedure (SUB..END SUB)

DECLARE SUB arm (d)

CLS

INPUT "Enter any number:"; d

CALL arm(d)

END

 

SUB arm (d)

n = d

su = 0

WHILE d > 0

re = d MOD 10

su = su + re ^ 3

d = INT(d / 10)

WEND

IF n = su THEN

PRINT n; "is Armstrong number"

ELSE

PRINT n; "is not Armstrong number"

END IF

END SUB

 

Using FUNCTION procedure (FUNCTION..END FUNCTION)

DECLARE FUNCTION arm$ (d)

CLS

INPUT "Enter any number:"; d

PRINT d; arm$(d)

END

 

FUNCTION arm$ (d)

n = d

su = 0

WHILE d > 0

re = d MOD 10

su = su + re ^ 3

d = INT(d / 10)

WEND

IF n = su THEN

arm$ = "is Armstrong number"

ELSE

arm$ = "is not Armstrong number"

END IF

END FUNCTION


Output:




Conclusion:


QBASIC is a simple and powerful programming language that can be used to develop a variety of applications, including mathematical calculations. We have discussed how to write a QBASIC program to check whether the input number is an Armstrong number or not. By following the steps outlined in this article, you can easily write your own QBASIC program to check for Armstrong numbers.

FAQs


1. What is an Armstrong number?
Ans: An Armstrong number is a number whose sum of the cube of its digits is equal to the number itself.


2. What is the formula for calculating the sum of the cube of each digit in a number?
Ans: The formula for calculating the sum of the cube of each digit in a number.


3. Can I use a different programming language to check for Armstrong numbers?
Ans: Yes, you can use any programming language to check for Armstrong numbers. The logic for checking for Armstrong numbers is the same regardless of the programming language used.


4. How can I use the program to check for multiple numbers?
Ans: You can modify the program to use a loop to prompt the user for multiple numbers and check if each number is an Armstrong number or not.

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages