Micro Focus QTP (UFT) Forums
Odd and Even Number Puzzle aka FizzBuzz - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: UFT / QTP Interview Questions (https://www.learnqtp.com/forums/Forum-UFT-QTP-Interview-Questions)
+--- Thread: Odd and Even Number Puzzle aka FizzBuzz (/Thread-Odd-and-Even-Number-Puzzle-aka-FizzBuzz)



Odd and Even Number Puzzle aka FizzBuzz - Varshin - 08-08-2015

Please solve this program!

If a number is even means, should print as even number else should print in the below pattern

Suppose the given number is 3, the output should be as below

*
* *
* * *
* *
*


RE: Odd and Even Number Puzzle aka FizzBuzz - Ankur - 08-09-2015

This is a variation of Fizz Buzz puzzle usually asked in programming interviews.

Here is one of the approach that can be used to solve this question. There can be many ways to arrive at the solution. It will be interesting to see how other QTP forum members approach this question.

  1. Declare a string you wish to make a pattern (in this case "*" )
  2. Take a mod of the given number (in this case since you need to identify an even number use 2)
  3. If mod is 0, print the even number Else use a loop to iterate till n , where "n" is the odd number.
  4. Use another for loop to print the string. first in ascending order and then in descending order.

PHP Code:
Dim strPatterniNumber
strPattern 
"*"
iNumber 3


If iNumber Mod 2 0 Then
    
print iNumber
    
else
    
    For 
1 To iNumber Step 1
        
For 1 To i Step 1 
             
print strPattern
        Next
        
print vbNewLine
    Next    
    
    
For 1 To iNumber Step 1
        
For = (iNumber-1To i Step -
             
print strPattern
        Next
        
print vbNewLine
    Next
    
    
End 
If 



RE: Odd and Even Number Puzzle aka FizzBuzz - Varshin - 08-10-2015

Thanks Ankur for ur prompt reply.


RE: Odd and Even Number Puzzle aka FizzBuzz - kotaramamohana - 08-10-2015

I have modified second For Loop little bit
Code:
Dim strPattern, iNumber
strPattern = "*"
iNumber = 3
m=""

If iNumber Mod 2 = 0 Then
    print iNumber
    else
    
    For i = 1 To iNumber Step 1
        For j = 1 To i Step 1
             m= m & strPattern
        Next
        m= m & vbNewLine
    Next    
    
    For i = iNumber -1 To 1 Step -1
        For j = 1 To i Step 1
             m= m & strPattern
        Next
        m= m & vbNewLine
    Next
    
    
End If

msgbox m

Hi Ankur,
I have come up with one more logic is below
Code:
Dim strPattern, iNumber
strPattern = "*"
iNumber = 3
m=""

If iNumber mod 2 = 0 Then
    print iNumber
Else
maxNumber=inumber *2 -1
for i=1 to maxNumber  step 1
if i<=iNumber then : a=i: else :a=a-1: end if
    for j=1 to a step 1
     m= m& strPattern
    next
m=m&vbnewline
next
End if
Msgbox m