Questions from Vic The Dice Man

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
Pete
Site Admin
Posts: 887
Joined: Sun Dec 07, 2003 9:10 pm
Location: Candor, NY
Contact:

Questions from Vic The Dice Man

Post by Pete »

Take it, guys!
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
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows »

1- Write a program to print the sum of even numbers from 1 to 30.

Code: Select all

FOR Counter% = 1 TO 30
    IF Counter%/2 = INT(Counter%/2) THEN
       Total% = Total% + Counter% 
    END IF
NEXT Counter
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)

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
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

' ----------------------------------
'  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
User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

Counting Even Numbers

Post by Zim »

How about:

Code: Select all

sum=0
for i=2 to 30 step 2
     sum=sum+i
next i
print sum
We often forget the "skip" keyword on the for statement. Its use eliminates the need for a test inside the loop.

--Zim
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Good point, Zim.
*****
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows »

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
User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

Testing for divisibility by two

Post by Zim »

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
Mystik, also a good point. Oh, btw, when dividing by two you could also use the "integer division" operator, \, the backslash. For example:

Code: Select all

if n\2 = n/2 then ...(n is even)... 
should give the same results.
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---
Post Reply