Q:

Golang If/Else | Find Output Programs | Set 1

belongs to collection: Golang Find Output Programs

0

This section contains the Golang conditional statements (if/else) find output programs (set 1) with their output and explanations.

Program 1:

package main

import "fmt"

func main() {
    var var1 string 
    var1 = "Hello World"
    
    if(var1=="Hello World")
	fmt.Println("Hello")
    else
	fmt.Println("Hiii")    
}

 

Program 2:

package main

import "fmt"

func main() {
	if 1 {
		fmt.Println("Hello")
	} else {
		fmt.Println("Hiii")
	}
}

 

Program 3:

package main

import "fmt"

func main() {

	var num float32 = 6.34

	if int(num)%2 == 0 {
		fmt.Println("Even Number")
	} else {
		fmt.Println("Odd Number")
	}
}

 

Program 4:

package main

import "fmt"

func main() {

	var num int = 0

	fmt.Print("Enter Number: ")
	fmt.Scanln(&num)

	if num%2 == 0 {
		fmt.Println("Even Number")
	} else {
		fmt.Println("Odd Number")
	}
}

 

Program 5:

package main

import "fmt"

func main() {

	var num, _ = fmt.Print("Hello World")

	if num%2 == 0 {
		fmt.Println("Even Number")
	} else {
		fmt.Println("Odd Number")
	}
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

Output:

./prog.go:10:22: syntax error: unexpected newline, expecting { after if clause

Explanation:

The above program will generate a syntax error. Here we did not use the correct syntax for if..else statements.

The correct syntax is given below,

if var1 == "Hello World" {
	fmt.Println("Hello")
} else {
	fmt.Println("Hiii")
}

The curly braces "{ }"  are mandatory with the "if..else" statement.


 

Answer Program 2:

Output:

./prog.go:6:2: non-bool 1 (type int) used as if condition

Explanation:

The above program will generate a syntax error. Here we used integer value 1 in the "if" condition. The "if" condition accepts only a Boolean value.


 

Answer Program 3:

Output:

Even Number

Explanation:

In the above program, we created a variable num of type float32, which was initialized with 6.34. Then we converted the variable num from float32 to integer using the int() function and checked the condition to find out the resulted number is Even number or Odd number.


 

Answer Program 4:

Output:

Enter Number: 7
Odd Number

Explanation:

In the above program, we created a variable num of type int. Then we read the value of variable num from the user using fmt.Scanln() function. After that, we checked the condition to find out the entered number is even or odd.


 

Answer Program 5:

Output:

Hello WorldOdd Number

Explanation:

 

In the above program, we created a variable num, which was initialized with the value returned by fmt.Print() function. The fmt.Print("Hello World") printed "Hello World" and returned 11, which was assigned to num. Then we checked the condition for EVEN or not and print the appropriate message.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Golang Find Output Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Golang If/Else | Find Output Programs | Set 2... >>
<< Golang Basics | Find Output Programs | Set 5...