Author Topic: whats wrong with this program?  (Read 2622 times)

0 Members and 1 Guest are viewing this topic.

Offline sarmad

  • Newbie
  • Posts: 4
    • View Profile
whats wrong with this program?
« on: September 21, 2021, 01:22:58 am »
hello every one .. i try to make a simple program to seperate numbers from 1 to 100 into odd and even and if the number has a root it ll be a text next to number says that this number has a root .. so i did this

Console:Only
10 For i = 1 To 100
15  _Delay 0.5
20 If i / 2 = Int(i / 2) Then GoTo 30 Else GoTo 100
30 If Sqr(i) = Int(Sqr(i)) Then Print i; "has a root" Else Print i: GoTo 1000
100 If Sqr(i) = Int(Sqr(i)) Then Print , , , i; "has a root" Else Print , , , i
1000 Next

i have a problem .. when even number  has a root it ll be put also in the group of odd numbers as well .. i dont know why? maybe the idea of programming is completly wrong?

Offline sarmad

  • Newbie
  • Posts: 4
    • View Profile
Re: whats wrong with this program?
« Reply #1 on: September 21, 2021, 02:11:36 am »
i found the problem .. it was in goto 1000 i put it in seperate line to jump line 100 but i kept the post any way cuz maybe there is a simpler way to get this .. thank you

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: whats wrong with this program?
« Reply #2 on: September 21, 2021, 08:10:04 am »
Here's an example with structural changes to eliminate the GOTO code altogether. Not necessarily less code, but more readable with the IF...THEN...ELSE...END IF block separated out for indentation. It helps to be able to see the logic structure at a glance. I'm very much about formatting for human readability, but it's less important in smaller, simpler algorithms.

In larger programs, single line complex logical operators, line numbers and GOTOs will quickly become a nightmare to deal with. Especially if you have to debug it. It's best to get out of the habit as soon as possible. I added the MOD statement, which returns the remainder of a division, just because I find it a little easier and more intuitive than comparing a division with an INT of itself, but i/2=INT(i/2) is perfectly acceptable.

Code: QB64: [Select]
  1. FOR i = 1 TO 100
  2.     IF i MOD 2 <> 0 THEN '                                      check remainder of i/2
  3.         PRINT i; '                                              remainder present/ print odd/ no line feed
  4.     ELSE
  5.         PRINT , , , i; '                                        no remainder/ print indented even/ no line feed
  6.     END IF
  7.     IF SQR(i) = INT(SQR(i)) THEN PRINT "has a root"; '          indicate if perfect square/ no line feed
  8.     PRINT '                                                     now line feed and continue to next number
  9.     _DELAY .2
  10.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: whats wrong with this program?
« Reply #3 on: September 21, 2021, 09:28:04 am »
Don't have to use Console or Delay:
Code: QB64: [Select]
  1. For i = 1 To 100
  2.     row = Int((i - 1) / 4) + 1: col = (i - 1) Mod 4
  3.     Locate row, col * 20 + 1: Print Right$("   " + _Trim$(Str$(i)), 3);
  4.     If i Mod 2 = 0 Then Print " Even "; Else Print " Odd ";
  5.     If i <= 10 Then i2 = i * i: Locate Int((i2 - 1) / 4) + 1, ((i2 - 1) Mod 4) * 20 + 10: Print "Square";
  6.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: whats wrong with this program?
« Reply #4 on: September 21, 2021, 11:45:35 am »
Easier?
Code: QB64: [Select]
  1. For col = 1 To 100 Step 20
  2.     For row = 1 To 25
  3.         i = i + 1
  4.         Locate row, col: Print i;
  5.         If i Mod 2 Then Print "odd"; Else Print "even";
  6.         If Sqr(i) = Int(Sqr(i)) Then Print " square";
  7.     Next
  8.     If i = 100 Then Exit For
  9.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: whats wrong with this program?
« Reply #5 on: September 21, 2021, 12:46:44 pm »
Advanced?
Code: QB64: [Select]
  1. For i = 1 To 100
  2.     If i <= 10 Then locateN i * i: Print Space$(9); "square"; ' do this first so dont print over " 1 odd"
  3.     locateN i
  4.     Print Right$("   " + _Trim$(Str$(i)), 3);
  5.     If i Mod 2 Then Print " odd"; Else Print " even";
  6.  
  7. Sub locateN (n As Integer) ' convert a number N to row and col to Locate a print on default screen
  8.     row = Int((n - 1) Mod 25) + 1: col = Int((n - 1) / 25) * 20 + 1
  9.     Locate row, col
  10.