Skip to content

Commit c71f8ac

Browse files
authored and
committed
conditional-statements
1 parent 1b0d40e commit c71f8ac

File tree

5 files changed

+163
-4
lines changed

5 files changed

+163
-4
lines changed

conditional-statements/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
- [Program to check whether a character is uppercase or lowercase alphabet](is_upper_case.go)
1414
- [Program to input week number and print week day](print_week_day.go)
1515
- [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)
1617
- [Program to input month number and print month name and number of days in that month](days_of_month.go)
17-
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)
1821
## To be written
19-
- Program to input angles of a triangle and check whether triangle is valid or not
20-
- Program to input all sides of a triangle and check whether triangle is valid or not
21-
- Program to check whether the triangle is equilateral, isosceles or scalene triangle
2222
- Program to find all roots of a quadratic equation
2323
- Program to calculate profit or loss
2424
- Program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following:
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+
*/
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+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
import "fmt"
3+
func isValidTriangle(angle1, angle2, angle3 float64)bool{
4+
return ((angle1 + angle2 + angle3) == 180)
5+
}
6+
func main(){
7+
var angle1, angle2, angle3 float64
8+
fmt.Print("Enter first angle: ")
9+
fmt.Scan(&angle1)
10+
fmt.Print("Enter second angle: ")
11+
fmt.Scan(&angle2)
12+
fmt.Print("Enter third angle: ")
13+
fmt.Scan(&angle3)
14+
15+
if(isValidTriangle(angle1, angle2, angle3)){
16+
fmt.Println("The give angles CAN form a triangle")
17+
} else {
18+
fmt.Println("The give angles CAN_NOT form a triangle")
19+
}
20+
}
21+
22+
/** Output
23+
24+
$ go run is_valid_triangle.go
25+
Enter first angle: 30
26+
Enter second angle: 60
27+
Enter third angle: 90
28+
The give angles CAN form a triangle
29+
30+
$ go run is_valid_triangle.go
31+
Enter first angle: 90
32+
Enter second angle: 90
33+
Enter third angle: 90
34+
The give angles CAN_NOT form a triangle
35+
36+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
import "fmt"
3+
func main(){
4+
var n int
5+
fmt.Print("Enter week number(1 - 7): ")
6+
fmt.Scan(&n)
7+
/* Note: There is a dedicated chapter to demonstrate arrays.
8+
* This purpose of this program is to show the easy method to 'print_week_day_using_switch.go'
9+
*/
10+
weekDays := [7]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
11+
12+
if(n >= 1 && n <= 7){
13+
fmt.Println(weekDays[n - 1]) // Array index starts from 0.
14+
} else{
15+
fmt.Println("Please enter a valid value between 1 and 7")
16+
}
17+
}
18+
19+
/** Outputs
20+
21+
$ go run print_week_day.go
22+
Enter week number(1 - 7): 4
23+
Thursday
24+
25+
$ go run print_week_day.go
26+
Enter week number(1 - 7): 7
27+
Sunday
28+
29+
$ go run print_week_day.go
30+
Enter week number(1 - 7): 8
31+
Please enter a valid value: 1 - 7
32+
33+
*/

0 commit comments

Comments
 (0)