Hi there, i’m trying to do a pretty simple exercise but I don’t know how to solve the problem; the text exercise is:
Write a program that reads from standard input an integer number n and that, as shown in the Execution example, prints to video a square of n lines consisting each of n symbols interspersed with spaces, alternating between them lines consisting only of symbols * (asterisk) interspersed with spaces and lines consisting only of symbols + (plus) interspersed with spaces.
Tip: You can use two nested loops and use the % operator to distinguish even lines from odd lines.
Execution example:
Enter a number: 5
the code that I’ve wrote is:
package main
import “fmt”
func main() {
var n int
var riga int
var riga2 int
fmt.Println("Enter a number: ")
fmt.Scan(&n)
for riga = 1; riga <= n; riga++ {
for riga2 = 1; riga2 <= n; riga2++ {
fmt.Print("*")
}
for riga2 = 1; riga2 <= n; riga2++ {
fmt.Print("+")
}
fmt.Print("\n")
}
}
Anyone can help? I can’t use the operator %, how can I use it?
Apply the % operator within the your outer loop to the counter. % yields the remainder of integer division. You should be able to use that to distinguish odd numbered rows from even numbered rows.