email: VRBaltimore@aol.com
name: Vic The Dice Man
Comments: Hi I'm writing to see if you can help me on a big exam that I have on Friday Dec. 9th in QBasic. I have some class examples which i don't know how it's done maybe you can explain.
1- Write a program to print the sum of even numbers from 1 to 30.
2- Write a program which would ask the user to enter a string. It would also ask the user to enter a word and serach for that word in the stringwhich is entered by the user (I believe i should be using a string function)
3- Write a program which would generate the following menu:
Square of a number
Cube of a number
Factorial of a number
Exit
The program should ask the user to enter a number and perform the function accordingly.(I don't know if i should use CASE or IF-THEN-Else)
I would really appreciate if you can help me solve these programs so I can study and understand how to write them for an exam that I have.
Thanks so much for your help, if you can I would really appreciate it
Thanks
VIC
Questions from Vic The Dice Man
Questions from Vic The Dice Man
Take it, guys!
-
- Veteran
- Posts: 703
- Joined: Sun Nov 14, 2004 7:36 am
- Contact:
1- Write a program to print the sum of even numbers from 1 to 30.
2- Write a program which would ask the user to enter a string. It would also ask the user to enter a word and serach for that word in the stringwhich is entered by the user (I believe i should be using a string function)
3- Write a program which would generate the following menu:
Square of a number
Cube of a number
Factorial of a number
Exit
The program should ask the user to enter a number and perform the function accordingly.(I don't know if i should use CASE or IF-THEN-Else)
Code: Select all
FOR Counter% = 1 TO 30
IF Counter%/2 = INT(Counter%/2) THEN
Total% = Total% + Counter%
END IF
NEXT Counter
Code: Select all
' ----------------------------- ↑
' Acquire Input from the user ░
' -----------------------------
INPUT "Enter Sentence: ", Sentence$
INPUT "Enter Search Word:", Word$
' ------------------------------------------
' Perform the Search and return the result
' ------------------------------------------
IF INSTR(1, Sentence$, Word$) <> 0 THEN
PRINT "The Search Word was found at position: "; STR$(INSTR(1, Sentence$, Word$))
ELSE
PRINT "Unable to find the word in the sentence."
END IF
Square of a number
Cube of a number
Factorial of a number
Exit
The program should ask the user to enter a number and perform the function accordingly.(I don't know if i should use CASE or IF-THEN-Else)
Code: Select all
' ----------------------------------
' Initialize loop control variable
' ----------------------------------
CanExit% = 0
' ---------------------------------------------
' Main loop to present menu and perform tasks
' ---------------------------------------------
DO WHILE CanExit% = 0
CLS
' ------------------------------
' Present the menu to the user
' ------------------------------
PRINT " Menu Options"
PRINT "------------------------"
PRINT "1. Square of a number"
PRINT "2. Cube of a number"
PRINT "3. Factorial of a number"
PRINT "4. Exit Program"
PRINT "------------------------"
' -----------------------
' Inquire a menu option
' -----------------------
INPUT "Enter Choice [1 to 4]: ", Choice%
' --------------------------------------
' Select case to perform selected task
' --------------------------------------
SELECT CASE Choice%
CASE 1 ' Square of a number
INPUT "Enter Number: ", Number#
PRINT STR$(Number#); " Squared is "; STR$(Number# ^ 2)
SLEEP
CASE 2 ' Cube of a number
INPUT "Enter Number: ", Number#
PRINT "The cube of "; STR$(Number#); " is "; STR$(Number# ^ 3)
SLEEP
CASE 3 ' Factorial of a number
' -----------------------------
' Acquire Input from the user
' -----------------------------
INPUT "Enter Number: ", Number#
' -----------------------------
' Loop to calculate factorial
' -----------------------------
WorkResult# = Number#
FOR Counter = Number# - 1 TO 1 STEP -1
WorkResult# = WorkResult# * Counter
NEXT Counter
' ---------------------
' Display the results
' ---------------------
PRINT "The Factorial is: "; ; WorkResult#
SLEEP
CASE 4 ' Exit the program
CanExit% = 1
END SELECT
LOOP
' --------------------------------------
' Clear the screen and end the program
' --------------------------------------
CLS
END
When God created light, so too was born, the first Shadow!
MystikShadows
Need hosting? http://www.jc-hosting.net
Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
MystikShadows
Need hosting? http://www.jc-hosting.net
Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
Counting Even Numbers
How about:
We often forget the "skip" keyword on the for statement. Its use eliminates the need for a test inside the loop.
--Zim
Code: Select all
sum=0
for i=2 to 30 step 2
sum=sum+i
next i
print sum
--Zim
-
- Veteran
- Posts: 703
- Joined: Sun Nov 14, 2004 7:36 am
- Contact:
You're right, this would produce the same results as my way of doing it. However, considering the fact that it was school, I simply dared to assume that part of the exercise was to be able to determine if a number was even or not .
When God created light, so too was born, the first Shadow!
MystikShadows
Need hosting? http://www.jc-hosting.net
Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
MystikShadows
Need hosting? http://www.jc-hosting.net
Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
Testing for divisibility by two
Mystik, also a good point. Oh, btw, when dividing by two you could also use the "integer division" operator, \, the backslash. For example:considering the fact that it was school, I simply dared to assume that part of the exercise was to be able to determine if a number was even
Code: Select all
if n\2 = n/2 then ...(n is even)...
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---
--- Time flies like an arrow, but fruit flies like a banana ---