Skip to content

Commit 012ee56

Browse files
authored
Merge pull request #4 from srekan/conditional-statements
Conditional statements
2 parents a789f84 + c71f8ac commit 012ee56

21 files changed

+836
-0
lines changed

Diff for: conditional-statements/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Go Programming Exercises: if else statements
2+
- [Program to find maximum between two numbers](max_of_two_numbers.go)
3+
- [Program to find maximum between two numbers using functions](max_of_two_numbers_using_functions.go)
4+
- [Program to find maximum between three numbers](max_of_three_numbers.go)
5+
- [Program to find maximum between three numbers using functions](max_of_three_numbers_using_functions.go)
6+
- [Program to check whether a number is negative, positive or zero](find_number_sign.go)
7+
- [Program to check whether a number is divisible by 5 and 11 or not](is_divisible_by_5_and_11.go)
8+
- [Program to check whether a number is divisible by 5 and 11 or not using boolean operators](is_divisible_by_5_and_11_using_functions_using_bool.go)
9+
- [Program to check whether a number is even or odd](is_even_or_odd.go)
10+
- [Program to check whether a year is leap year or not](is_leap_year.go)
11+
- [Program to input any character and check whether it is alphabet, digit or special character](is_alphabet_or_digit.go)
12+
- [Program to input any alphabet and check whether it is vowel or consonant](is_vowel.go)
13+
- [Program to check whether a character is uppercase or lowercase alphabet](is_upper_case.go)
14+
- [Program to input week number and print week day](print_week_day.go)
15+
- [Program to input week number and print week day using switch](print_week_day_using_switch.go)
16+
- [Program to input week number and print week day using an array](print_week_day_using_array.go)
17+
- [Program to input month number and print month name and number of days in that month](days_of_month.go)
18+
- [Program to input month number and print month name and number of days in that month using a map](days_of_month_using_map.go)
19+
- [Program to input angles of a triangle and check whether triangle is valid or not](is_valid_triangle.go)
20+
- [Program to check whether the triangle is equilateral, isosceles or scalene triangle](get_triangle_type.go)
21+
## To be written
22+
- Program to find all roots of a quadratic equation
23+
- Program to calculate profit or loss
24+
- Program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following:
25+
* Percentage >= 90% : Grade A
26+
* Percentage >= 80% : Grade B
27+
* Percentage >= 70% : Grade C
28+
* Percentage >= 60% : Grade D
29+
* Percentage >= 40% : Grade E
30+
* Percentage < 40% : Grade F
31+
- Program to input basic salary of an employee and calculate its Gross salary according to following:
32+
* Basic Salary <= 10000 : HRA = 20%, DA = 80%
33+
* Basic Salary <= 20000 : HRA = 25%, DA = 90%
34+
* Basic Salary > 20000 : HRA = 30%, DA = 95%
35+
- Program to input electricity unit charges and calculate total electricity bill according to the given condition:
36+
* For first 50 units Rs. 0.50/unit
37+
* For next 100 units Rs. 0.75/unit
38+
* For next 100 units Rs. 1.20/unit
39+
* For unit above 250 Rs. 1.50/unit
40+
* An additional surcharge of 20% is added to the bill

Diff for: conditional-statements/days_of_month.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
import "fmt"
3+
func main(){
4+
var n int
5+
fmt.Print("Enter month number(1 - 12): ")
6+
fmt.Scan(&n)
7+
8+
switch n {
9+
case 1, 3, 5, 7, 8, 10, 12:
10+
fmt.Println("31 days")
11+
case 2:
12+
fmt.Println("28/29 days")
13+
case 4, 6, 9, 11:
14+
fmt.Println("30 days")
15+
default:
16+
fmt.Println("Please enter a valid value between 1 and 12")
17+
}
18+
}
19+
20+
/** Outputs
21+
22+
$ go run days_of_month.go
23+
Enter month number(1 - 12): 5
24+
31 days
25+
26+
$ go run days_of_month.go
27+
Enter month number(1 - 12): 2
28+
28/29 days
29+
30+
$ go run days_of_month.go 13
31+
Enter month number(1 - 12): 13
32+
Please enter a valid value between 1 and 12
33+
34+
*/

Diff for: conditional-statements/days_of_month_using_map.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
import "fmt"
3+
func main(){
4+
type Month struct {
5+
name string
6+
days int
7+
}
8+
monthsMap := map[int]Month{
9+
1: { name: "January", days: 31 },
10+
2: { name: "February", days: 28 },
11+
3: { name: "March", days: 31 },
12+
4: { name: "April", days: 30 },
13+
5: { name: "May", days: 31 },
14+
6: { name: "June", days: 30 },
15+
7: { name: "July", days: 31 },
16+
8: { name: "August", days: 31 },
17+
9: { name: "September", days: 30 },
18+
10: { name: "October", days: 31 },
19+
11: { name: "November", days: 30 },
20+
12: { name: "December", days: 31 },
21+
}
22+
23+
var n int
24+
fmt.Print("Enter month number(1 - 12): ")
25+
fmt.Scan(&n)
26+
27+
if monthsMap[n].name != "" {
28+
fmt.Printf("%s - %d days\n", monthsMap[n].name, monthsMap[n].days)
29+
} else {
30+
fmt.Println("Please enter a valid value between 1 and 12")
31+
}
32+
}
33+
34+
/** Outputs
35+
36+
$ go run days_of_month_using_map.go
37+
Enter month number(1 - 12): 6
38+
June - 30 days
39+
40+
$ go run days_of_month_using_map.go
41+
Enter month number(1 - 12): 66
42+
Please enter a valid value between 1 and 12
43+
44+
*/

Diff for: conditional-statements/find_number_sign.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
import "fmt"
3+
func main(){
4+
var n int
5+
fmt.Print("Enter an integer: ")
6+
fmt.Scan(&n)
7+
8+
if(n == 0){
9+
fmt.Println("Given number is zero")
10+
} else if(n > 0){
11+
fmt.Println("Given number is positive")
12+
} else{ /* if(n < 0) // Default case*/
13+
fmt.Println("Given number is negative")
14+
}
15+
}
16+
/** Outputs
17+
18+
$ go run find_number_sign.go
19+
Enter an integer: 10
20+
Given number is positive
21+
22+
$ go run find_number_sign.go
23+
Enter an integer: -10
24+
Given number is negative
25+
26+
$ go run find_number_sign.go
27+
Enter an integer: 0
28+
Given number is zero
29+
30+
*/

Diff for: conditional-statements/get_triangle_type.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
import "fmt"
3+
func getTriangleType(side1, side2, side3 float64)string{
4+
if(side1 == side2 && side2 == side3){
5+
return "EQUILATERAL triangle"
6+
}
7+
8+
if(side1 == side2 || side2 == side3 || side3 == side1){
9+
return "ISOSCELES triangle"
10+
}
11+
12+
return "SCALENE triangle" // this is the remaining case after the aboce two cases
13+
}
14+
func main(){
15+
var side1, side2, side3 float64
16+
fmt.Print("Enter first side: ")
17+
fmt.Scan(&side1)
18+
fmt.Print("Enter second second: ")
19+
fmt.Scan(&side2)
20+
fmt.Print("Enter third third: ")
21+
fmt.Scan(&side3)
22+
23+
fmt.Println(getTriangleType(side1, side2, side3))
24+
}
25+
26+
/** Output
27+
28+
$ go run get_triangle_type.go
29+
Enter first side: 10
30+
Enter second second: 10
31+
Enter third third: 10
32+
EQUILATERAL triangle
33+
34+
$ go run get_triangle_type.go
35+
Enter first side: 10
36+
Enter second second: 10
37+
Enter third third: 20
38+
ISOSCELES triangle
39+
40+
$ go run get_triangle_type.go
41+
Enter first side: 10
42+
Enter second second: 20
43+
Enter third third: 30
44+
SCALENE triangle
45+
46+
*/

Diff for: conditional-statements/is_alphabet_or_digit.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
ASCII codes
3+
0 : 48
4+
9 : 57
5+
A : 65
6+
B : 66
7+
Z : 90
8+
a : 97
9+
b : 98
10+
z : 122
11+
*/
12+
package main
13+
import (
14+
"fmt"
15+
"bufio"
16+
"os"
17+
)
18+
func main(){
19+
reader := bufio.NewReader(os.Stdin)
20+
21+
fmt.Print("Enter a character: ")
22+
char, _, err := reader.ReadRune()
23+
24+
if err != nil {
25+
fmt.Println(err)
26+
}
27+
28+
if (char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z') {
29+
fmt.Println("Given character is an alphabet")
30+
} else if char >= '0' && char <= '9' {
31+
fmt.Println("Given character is a digit")
32+
}
33+
/*
34+
// Method 2: compare with ascii values
35+
if (char >= 65 && char <= 122) || (char >= 97 && char <= 90) {
36+
fmt.Println("Given character is an alphabet")
37+
} else if char >= 48 && char <= 57 {
38+
fmt.Println("Given character is a digit")
39+
}
40+
*/
41+
}
42+
43+
/** Outputs
44+
45+
$ go run is_alphabet_or_digit.go
46+
Enter a character: a
47+
Given character is an alphabet
48+
49+
$ go run is_alphabet_or_digit.go
50+
Enter a character: G
51+
Given character is an alphabet
52+
53+
$ go run is_alphabet_or_digit.go
54+
Enter a character: 5
55+
Given character is a digit
56+
57+
*/

Diff for: conditional-statements/is_divisible_by_5_and_11.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
import "fmt"
3+
func main(){
4+
var n int
5+
fmt.Print("Enter a number: ")
6+
fmt.Scan(&n)
7+
8+
if(n % 5 == 0){
9+
if(n % 11 == 0){
10+
fmt.Println("The given number is divisible by both 5 and 11")
11+
} else {
12+
fmt.Println("The given number is divisible by 5, but not 11")
13+
}
14+
} else {
15+
if(n % 11 == 0){
16+
fmt.Println("The given number is divisible by 11, but not 5")
17+
} else {
18+
fmt.Println("The given number is not divisible by 5 and 11")
19+
}
20+
}
21+
}
22+
23+
/** Outputs
24+
25+
$ go run is_divisible_by_5_and_11.go
26+
Enter a number: 55
27+
The given number is divisible by both 5 and 11
28+
29+
$ go run is_divisible_by_5_and_11.go
30+
Enter a number: 22
31+
The given number is divisible by 11, but not 5
32+
33+
$ go run is_divisible_by_5_and_11.go
34+
Enter a number: 25
35+
The given number is divisible by 5, but not 11
36+
37+
$ go run is_divisible_by_5_and_11.go
38+
Enter a number: 13
39+
The given number is not divisible by 5 and 11
40+
41+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
import "fmt"
3+
func main(){
4+
var n int
5+
fmt.Print("Enter a number: ")
6+
fmt.Scan(&n)
7+
8+
if(n % 5 == 0 && n % 11 == 0){
9+
fmt.Println("The given number is divisible by both 5 and 11")
10+
}
11+
12+
if(n % 5 == 0 && n % 11 != 0){
13+
fmt.Println("The given number is divisible by 5, but not 11")
14+
}
15+
if(n % 5 != 0 && n % 11 == 0){
16+
fmt.Println("The given number is divisible by 11, but not 5")
17+
}
18+
19+
if(n % 5 != 0 && n % 11 != 0){
20+
fmt.Println("The given number is not divisible by 5 and 11")
21+
}
22+
}
23+
24+
/** Outputs
25+
26+
$ go run is_divisible_by_5_and_11.go
27+
Enter a number: 55
28+
The given number is divisible by both 5 and 11
29+
30+
$ go run is_divisible_by_5_and_11.go
31+
Enter a number: 22
32+
The given number is divisible by 11, but not 5
33+
34+
$ go run is_divisible_by_5_and_11.go
35+
Enter a number: 25
36+
The given number is divisible by 5, but not 11
37+
38+
$ go run is_divisible_by_5_and_11.go
39+
Enter a number: 13
40+
The given number is not divisible by 5 and 11
41+
42+
*/

Diff for: conditional-statements/is_even_or_odd.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
import "fmt"
3+
func main(){
4+
var n int
5+
fmt.Print("Enter a number: ")
6+
fmt.Scan(&n)
7+
8+
if(n % 2 == 0){
9+
fmt.Println("Given number is even")
10+
} else {
11+
fmt.Println("Given number is odd")
12+
}
13+
}
14+
15+
/** Outputs
16+
17+
$ go run is_even_or_odd.go
18+
Enter a number: 2
19+
Given number is even
20+
21+
$ go run is_even_or_odd.go
22+
Enter a number: 3
23+
Given number is odd
24+
25+
$ go run is_even_or_odd.go
26+
Enter a number: 0
27+
Given number is even
28+
29+
*/

0 commit comments

Comments
 (0)