0
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.
12 ответов
+ 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 😂😋
0
That's insane. I don't know how they check code, or what are they formatting settings, but if they have some strict rules about styling, they should to tell about it.
I've wrote solutions dozen times, trying to figure out what is the problem. And why they do not show just compilation error, and instead show just that we have an error without even showing compilation error message, like "we can tell you where you can search bug, but... don't wanna, just try again and suffer more".
In mu final solution, all would not worked until i just swapped "years > 0" to "0 < years". That's my solution
package main
import (
"fmt"
)
func main() {
var years int
fmt.Scanln(&years)
//your code goes her
res := 7
for 0 < years {
res = res * 2
years = years -1
}
fmt.Println(res)
}