-6

I want to print a star pattern in GO .The desired output is as belows :

enter image description here

I wrote the program to print it but I could write it to print the output aliged on the leftmost side.

The code is :

package main

import "fmt"

func main() {
    for i := 1; i <= 6; i++ {
        if i == 1 {
            fmt.Printfln("#")
            fmt.Println()
        }
        if i == 2 {
            fmt.Println( "##")
            fmt.Println()
        }
        if i == 3 {
            fmt.Println("###")
            fmt.Println()
        }
        if i == 4 {
            fmt.Println("####")
            fmt.Println()
        }
        if i == 5 {
            fmt.Println("#####")
            fmt.Println()
        }
        if i == 6 {
            fmt.Println("######")
            fmt.Println()
        }
    }

    //Enter your code here. Read input from STDIN. Print output to STDOUT
}

The output that I get is : enter image description here

How can I achieve the desired format in GO ?

  • Dont understand why people are downvoting this ! If you cant provide a solution and still want to downvote then atleast state the reason why you did that! – pronessssssssssss Oct 13 '15 at 00:22
  • 7
    I would guess people are downvoting because it looks like asking someone to do your homework for you. There are multiple ways you could solve this problem. Consider looking at `strings.Repeat`, the padding options on `fmt.Printf` or even just using some nested loops. – jcbwlkr Oct 13 '15 at 00:37
  • @jcbwlkr Definetly this was not a homework . I was taking a online quiz and came across this . I posted my code, the output I am getting and the output that was epected. I definetlely dont see any form of spoonfeeding here. Strange to see so many downvotes for a question that was valid; uses String functions and generates output as described. – pronessssssssssss Oct 13 '15 at 07:49
  • Well, my take is that your question is about a thing *too simple* to ask for help on solving. No, really. You as a programmer are supposed to program. There's just no indication of any attempt at solving the problem in your example. Please consider reading http://whathaveyoutried.com – kostix Oct 14 '15 at 06:17
  • its is a testing problem, you can not asking help here – karlos Nov 30 '22 at 23:30

1 Answers1

3
package main

import (
    "fmt"
    "strings"
)

func main() {
    for i := 1; i <= 6; i++ {
        fmt.Printf("%6s\n", strings.Repeat("#", i))
    }
}

Try it on the Go playground

RoninDev
  • 5,446
  • 3
  • 23
  • 37
  • For more flexibility, you could avoid hardcoding the upper bound (`6`, in this case) in the format string: `fmtString := "%" + strconv.Itoa(n) + "s\n"` (where `n := 6`), and then, in the loop, `for i := 1; i <= n; i++ {fmt.Printf(fmtString, strings.Repeat("#", i))}`. – jub0bs Oct 14 '15 at 15:13