Page 1 of 1

Questions from Vic The Dice Man

Posted: Wed Dec 07, 2005 9:40 pm
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

Posted: Thu Dec 08, 2005 5:52 am
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

Counting Even Numbers

Posted: Fri Dec 09, 2005 12:17 pm
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

Posted: Fri Dec 09, 2005 8:09 pm
by moneo
Good point, Zim.
*****

Posted: Mon Dec 12, 2005 6:07 am
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 :-).

Testing for divisibility by two

Posted: Mon Dec 12, 2005 11:57 am
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.