Sunday, January 25, 2015

Multiplication Table - Using FOR Loop in Go

FOR loop is a powerful construct and is frequently used in programming. You must understand it fully and feel at home while using it. 

Apart from this post you can practice this loop from the problems shared in the following examples:

1. FizzBuzz 
2. An interesting Go flag
2. for loop used in defer statement

Problem

Print Multiplication Table

Let us start

Suppose you wanted to print the multiplication table of a number say 2, in format like the following image:


Or may be like the following vertical format, the way we used to write in school:

Solution

To get the multiplication table of 2 you can simply write the following code snippet:

package main

import "fmt"

func main() {
 num := 2

 for j := 1; j <= 10; j++ {
  k := num * j
  fmt.Print(k)
  fmt.Printf("  ")
/*
//For a output in vertical format
fmt.Println(k) //Uncomment this line & comment above 2 lines
*/

 }
}

Play with the above code

The above code prints the multiplication table of 2. Change the value of variable num to get a multiplication table of a desired number.

The above code can be modified so that we can get multiplication tables up to a number defined by the user's input. The output could be like the following image:


The above output can be accomplished by the code shared in below image. Here we have used 2 FOR loops (also known nested FOR loop). The 2nd FOR loop is the same as you've seen in one of the above images captioned Simple FOR Loop. The second FOR loop has now become the inner loop and we have added one outer loop that takes care of the count of multiplication table up to a specific number - in this case I've entered 10 as you can see in the image just above this paragraph.


I am learning and sharing. To improve the code, feel free to share your experiences/tips in the comment section.

Monday, January 12, 2015

FOR Loop in Go Programming

Here's an exercise that will help you to understand the only looping construct present in Go programming language. Yes, you heard it right, Go has only one loop - for loop. Go is neat and concise. I really liked the way it has done away with the multiple confusing loops that are present in other languages. The code looks tidy with a few less number of brackets and no semicolons.

Let us try to get the following graphical image (Go flag) using for loop.

P.S. - for loop is a powerful control statement and is frequently used in programming. You must understand it fully and feel at home while using it. 

A few other golang for loop examples:
1. Multiplication Table - Using FOR Loop in Go . 
2. for loop used in defer statement.
3. FizzBuzz

The Output

The Solution

The solution with simple for loop is here:


package main

import "fmt"

func main() {
 str := "||Go"
 for i := 1; i <= 10; i++ {
  fmt.Println(str)
  str += "||Go"
 }
 for k := 19; k >= 1; k-- {
  fmt.Println("||")
 }
 fmt.Println("/=\\")
 fmt.Println("/==\\")
 fmt.Println("/===\\")
 fmt.Print("/====\\")
}

Want to Run the Code in Action and See the Output Right Now?
The alternate solution uses nested for loops. You can be a little creative and can print whatever you want in place of Go. For example replace Go with * and see a different output.

Hope you liked this. Can it be improved? Please share your views.

Friday, January 9, 2015

Fizz-Buzz Program in Golang

Like the highly popular Hello World code that we use to learn as our first program in any programming language; Fizz-Buzz is one step ahead which helps us to understand the basic nitty-gritty of a language. Also, it's a popular interview question that can be asked even to experienced programmers to check if s/he can actually write code! 


And you know what, most of them fail to write this even if they are working in the said language for say 4+ years! Has Ctrl+C, Ctrl+V taken a toll on our code writing ability :)

Problem Staement

Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “Fizz-Buzz”.

Expected Output Sample [Shown only till 20 and not 100]





FizzBuzz using If / Else
Run this code here at Playground and see the Output.

package main

import "fmt"

func main() {
 for i := 1; i <= 100; i++ {

  if i%15 == 0 {
        fmt.Println("Fizz-Buzz", i)
  } else if i%5 == 0 {
        fmt.Println("Buzz", i)
  } else if i%3 == 0 {
        fmt.Println("fizz", i)
  } else {
        fmt.Println(i)
  }
 }
}


FizzBuzz using Switch

package main
 
import "fmt"
 
func main() {
    for i := 1; i <= 100; i++ {
        switch {
        case i%15==0:
            fmt.Println("FizzBuzz")
        case i%3==0:
            fmt.Println("Fizz")
        case i%5==0:
            fmt.Println("Buzz")
        default: 
            fmt.Println(i)
        }
    }
}


Run this code at Playground and see the Output.

Hope this helps you to get started with the basic control flow of Google Go language. Please share your views.