New course! Every coder should learn Generative AI!
Try a free lesson0
Errors and errors
The population of rabbits at a lab doubles every year. The lab was started initially with 7 rabbits. Your program needs to calculate the number of rabbits you will have after the number of years provided as input.
11 Answers
+ 2
Chinmay Anand
Review the loops lesson just prior to this practice. Start with 7 rabbits. You need to loop years times (get years as an input). Inside the loop double the amount of rabbits (rabbits = rabbits * 2) and set the new amount back to the rabbits variable. After the loop output the amount of rabbits.
+ 1
rabbits * 2**years
+ 1
Atul [Inactive] it is simply the formula
+ 1
Here is my attempt, but it says my loop syntax is wrong. Can anyone help?
package main
import "fmt"
func main() {
var years int
fmt.Scanln(&years)
//your code goes here
start:=7
i:=2
for start := 7; i<years; i++ {
start =start*i** years
fmt.Println(start)
}
}
+ 1
package main
import "fmt"
func main() {
var years int
fmt.Scanln(&years)
rabbits := 7
//your code goes here
for i := 0; i < years; i++ {
rabbits *= 2
}
fmt.Println(rabbits)
}
0
Post your code here
0
Oma Falk Is this valid in Golang also?
0
Thanks.
0
It is in golang
0
Can someone please help
0
package main
import "fmt"
func main() {
var years int
fmt.Scanln(&years)
pow := make([]int, years )
for i:= range pow {
pow[i] = 7 << uint(i) // == 2**i
}
for _, value := range pow {
fmt.Printf("%d\n" ,value)
}
}
any one can help reduce the number of range of values to print the last number of year
cause mine a whole range of years 😂😋