diff --git a/README.md b/README.md index e8b22e0..99be9b2 100644 --- a/README.md +++ b/README.md @@ -2,20 +2,7 @@ Golang coding exercises uploaded while learning ## Exercises -- [Basic](#basic) - -### Basic -- [Sum of two numbers](basic/sum_of_two_numbers.go) -- [Arithmetic operations](basic/arithmetic_operations.go) -- [Input from user](basic/input_from_user.go) -- [Accept length and breadth of a rectangle. Then find perimeter and area of a rectangle](basic/rectangle_area_and_perimeter.go) -- [Accept radius of a circle. Then find diameter and circumference of a circle](basic/circle_diameter_and_circumference.go) -- [Accept temperature in Celsius and convert it into Fahrenheit.](basic/celsius_to_fahrenheit.go) -- [Program to convert days into years, weeks and days](basic/days_to_years.go) -- [Program to enter two angles of a triangle and find the third angle](basic/third_angle_of_triangle.go) -- [Program to enter base and height of a triangle and find its area](basic/area_of_triangle.go) -- [Program to calculate area of an equilateral triangle](basic/area_of_equilateral_triangle.go) -- [Program to find power of any number x ^ y](basic/power_of_a_number.go) -- [Program to enter marks of five subjects and calculate total, average and percentage](basic/student_marks.go) -- [Program to enter P, N, R and calculate Simple Interest](basic/simple_interest.go) -- [Program to enter P, N, R and calculate Compound Interest](basic/compound_interest.go) +- [Basic](basic) +- [Functions](functions) +- [Conditional Statements](conditional-statements) +- [Loops](loops) diff --git a/basic/circle_diameter_and_circumference.go b/basic/circle_diameter_and_circumference.go index eebc75e..e5fc7a8 100644 --- a/basic/circle_diameter_and_circumference.go +++ b/basic/circle_diameter_and_circumference.go @@ -1,14 +1,16 @@ package main -import "fmt" +import ( + "fmt" + "math" +) func main(){ fmt.Println("-----Find diameter and circumference of a circle-----") - const Pi = 3.14 var radius float64 fmt.Print("Enter radius of circle (in meters): ") fmt.Scan(&radius) - circumference := 2 * Pi * radius + circumference := 2 * math.Pi * radius diameter := 2 * radius fmt.Println("Radius :", radius) @@ -22,7 +24,7 @@ $ go run circle_diameter_and_circumference.go -----Find diameter and circumference of a circle----- Enter radius of circle (in meters): 5.5 Radius : 5.5 -Circumference: 34.54 +Circumference: 34.55751918948772 Diameter : 11 */ diff --git a/conditional-statements/README.md b/conditional-statements/README.md new file mode 100644 index 0000000..026bb14 --- /dev/null +++ b/conditional-statements/README.md @@ -0,0 +1,40 @@ +# Go Programming Exercises: if else statements +- [Program to find maximum between two numbers](max_of_two_numbers.go) +- [Program to find maximum between two numbers using functions](max_of_two_numbers_using_functions.go) +- [Program to find maximum between three numbers](max_of_three_numbers.go) +- [Program to find maximum between three numbers using functions](max_of_three_numbers_using_functions.go) +- [Program to check whether a number is negative, positive or zero](find_number_sign.go) +- [Program to check whether a number is divisible by 5 and 11 or not](is_divisible_by_5_and_11.go) +- [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) +- [Program to check whether a number is even or odd](is_even_or_odd.go) +- [Program to check whether a year is leap year or not](is_leap_year.go) +- [Program to input any character and check whether it is alphabet, digit or special character](is_alphabet_or_digit.go) +- [Program to input any alphabet and check whether it is vowel or consonant](is_vowel.go) +- [Program to check whether a character is uppercase or lowercase alphabet](is_upper_case.go) +- [Program to input week number and print week day](print_week_day.go) +- [Program to input week number and print week day using switch](print_week_day_using_switch.go) +- [Program to input week number and print week day using an array](print_week_day_using_array.go) +- [Program to input month number and print month name and number of days in that month](days_of_month.go) +- [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) +- [Program to input angles of a triangle and check whether triangle is valid or not](is_valid_triangle.go) +- [Program to check whether the triangle is equilateral, isosceles or scalene triangle](get_triangle_type.go) +## To be written +- Program to find all roots of a quadratic equation +- Program to calculate profit or loss +- Program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following: + * Percentage >= 90% : Grade A + * Percentage >= 80% : Grade B + * Percentage >= 70% : Grade C + * Percentage >= 60% : Grade D + * Percentage >= 40% : Grade E + * Percentage < 40% : Grade F +- Program to input basic salary of an employee and calculate its Gross salary according to following: + * Basic Salary <= 10000 : HRA = 20%, DA = 80% + * Basic Salary <= 20000 : HRA = 25%, DA = 90% + * Basic Salary > 20000 : HRA = 30%, DA = 95% +- Program to input electricity unit charges and calculate total electricity bill according to the given condition: + * For first 50 units Rs. 0.50/unit + * For next 100 units Rs. 0.75/unit + * For next 100 units Rs. 1.20/unit + * For unit above 250 Rs. 1.50/unit + * An additional surcharge of 20% is added to the bill diff --git a/conditional-statements/days_of_month.go b/conditional-statements/days_of_month.go new file mode 100644 index 0000000..5418e88 --- /dev/null +++ b/conditional-statements/days_of_month.go @@ -0,0 +1,34 @@ +package main +import "fmt" +func main(){ + var n int + fmt.Print("Enter month number(1 - 12): ") + fmt.Scan(&n) + + switch n { + case 1, 3, 5, 7, 8, 10, 12: + fmt.Println("31 days") + case 2: + fmt.Println("28/29 days") + case 4, 6, 9, 11: + fmt.Println("30 days") + default: + fmt.Println("Please enter a valid value between 1 and 12") + } +} + +/** Outputs + +$ go run days_of_month.go +Enter month number(1 - 12): 5 +31 days + +$ go run days_of_month.go +Enter month number(1 - 12): 2 +28/29 days + +$ go run days_of_month.go 13 +Enter month number(1 - 12): 13 +Please enter a valid value between 1 and 12 + +*/ \ No newline at end of file diff --git a/conditional-statements/days_of_month_using_map.go b/conditional-statements/days_of_month_using_map.go new file mode 100644 index 0000000..6ea703d --- /dev/null +++ b/conditional-statements/days_of_month_using_map.go @@ -0,0 +1,44 @@ +package main +import "fmt" +func main(){ + type Month struct { + name string + days int + } + monthsMap := map[int]Month{ + 1: { name: "January", days: 31 }, + 2: { name: "February", days: 28 }, + 3: { name: "March", days: 31 }, + 4: { name: "April", days: 30 }, + 5: { name: "May", days: 31 }, + 6: { name: "June", days: 30 }, + 7: { name: "July", days: 31 }, + 8: { name: "August", days: 31 }, + 9: { name: "September", days: 30 }, + 10: { name: "October", days: 31 }, + 11: { name: "November", days: 30 }, + 12: { name: "December", days: 31 }, + } + + var n int + fmt.Print("Enter month number(1 - 12): ") + fmt.Scan(&n) + + if monthsMap[n].name != "" { + fmt.Printf("%s - %d days\n", monthsMap[n].name, monthsMap[n].days) + } else { + fmt.Println("Please enter a valid value between 1 and 12") + } +} + +/** Outputs + +$ go run days_of_month_using_map.go +Enter month number(1 - 12): 6 +June - 30 days + +$ go run days_of_month_using_map.go +Enter month number(1 - 12): 66 +Please enter a valid value between 1 and 12 + +*/ \ No newline at end of file diff --git a/conditional-statements/find_number_sign.go b/conditional-statements/find_number_sign.go new file mode 100644 index 0000000..bd0ff94 --- /dev/null +++ b/conditional-statements/find_number_sign.go @@ -0,0 +1,30 @@ +package main +import "fmt" +func main(){ + var n int + fmt.Print("Enter an integer: ") + fmt.Scan(&n) + + if(n == 0){ + fmt.Println("Given number is zero") + } else if(n > 0){ + fmt.Println("Given number is positive") + } else{ /* if(n < 0) // Default case*/ + fmt.Println("Given number is negative") + } +} +/** Outputs + +$ go run find_number_sign.go +Enter an integer: 10 +Given number is positive + +$ go run find_number_sign.go +Enter an integer: -10 +Given number is negative + +$ go run find_number_sign.go +Enter an integer: 0 +Given number is zero + +*/ \ No newline at end of file diff --git a/conditional-statements/get_triangle_type.go b/conditional-statements/get_triangle_type.go new file mode 100644 index 0000000..b03ffef --- /dev/null +++ b/conditional-statements/get_triangle_type.go @@ -0,0 +1,46 @@ +package main +import "fmt" +func getTriangleType(side1, side2, side3 float64)string{ + if(side1 == side2 && side2 == side3){ + return "EQUILATERAL triangle" + } + + if(side1 == side2 || side2 == side3 || side3 == side1){ + return "ISOSCELES triangle" + } + + return "SCALENE triangle" // this is the remaining case after the aboce two cases +} +func main(){ + var side1, side2, side3 float64 + fmt.Print("Enter first side: ") + fmt.Scan(&side1) + fmt.Print("Enter second second: ") + fmt.Scan(&side2) + fmt.Print("Enter third third: ") + fmt.Scan(&side3) + + fmt.Println(getTriangleType(side1, side2, side3)) +} + +/** Output + +$ go run get_triangle_type.go +Enter first side: 10 +Enter second second: 10 +Enter third third: 10 +EQUILATERAL triangle + +$ go run get_triangle_type.go +Enter first side: 10 +Enter second second: 10 +Enter third third: 20 +ISOSCELES triangle + +$ go run get_triangle_type.go +Enter first side: 10 +Enter second second: 20 +Enter third third: 30 +SCALENE triangle + +*/ \ No newline at end of file diff --git a/conditional-statements/is_alphabet_or_digit.go b/conditional-statements/is_alphabet_or_digit.go new file mode 100644 index 0000000..33d4dc9 --- /dev/null +++ b/conditional-statements/is_alphabet_or_digit.go @@ -0,0 +1,57 @@ +/* +ASCII codes +0 : 48 +9 : 57 +A : 65 +B : 66 +Z : 90 +a : 97 +b : 98 +z : 122 +*/ +package main +import ( + "fmt" + "bufio" + "os" +) +func main(){ + reader := bufio.NewReader(os.Stdin) + + fmt.Print("Enter a character: ") + char, _, err := reader.ReadRune() + + if err != nil { + fmt.Println(err) + } + + if (char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z') { + fmt.Println("Given character is an alphabet") + } else if char >= '0' && char <= '9' { + fmt.Println("Given character is a digit") + } + /* + // Method 2: compare with ascii values + if (char >= 65 && char <= 122) || (char >= 97 && char <= 90) { + fmt.Println("Given character is an alphabet") + } else if char >= 48 && char <= 57 { + fmt.Println("Given character is a digit") + } + */ +} + +/** Outputs + +$ go run is_alphabet_or_digit.go +Enter a character: a +Given character is an alphabet + +$ go run is_alphabet_or_digit.go +Enter a character: G +Given character is an alphabet + +$ go run is_alphabet_or_digit.go +Enter a character: 5 +Given character is a digit + +*/ \ No newline at end of file diff --git a/conditional-statements/is_divisible_by_5_and_11.go b/conditional-statements/is_divisible_by_5_and_11.go new file mode 100644 index 0000000..b9d1e13 --- /dev/null +++ b/conditional-statements/is_divisible_by_5_and_11.go @@ -0,0 +1,41 @@ +package main +import "fmt" +func main(){ + var n int + fmt.Print("Enter a number: ") + fmt.Scan(&n) + + if(n % 5 == 0){ + if(n % 11 == 0){ + fmt.Println("The given number is divisible by both 5 and 11") + } else { + fmt.Println("The given number is divisible by 5, but not 11") + } + } else { + if(n % 11 == 0){ + fmt.Println("The given number is divisible by 11, but not 5") + } else { + fmt.Println("The given number is not divisible by 5 and 11") + } + } +} + +/** Outputs + +$ go run is_divisible_by_5_and_11.go +Enter a number: 55 +The given number is divisible by both 5 and 11 + +$ go run is_divisible_by_5_and_11.go +Enter a number: 22 +The given number is divisible by 11, but not 5 + +$ go run is_divisible_by_5_and_11.go +Enter a number: 25 +The given number is divisible by 5, but not 11 + +$ go run is_divisible_by_5_and_11.go +Enter a number: 13 +The given number is not divisible by 5 and 11 + +*/ \ No newline at end of file diff --git a/conditional-statements/is_divisible_by_5_and_11_using_functions_using_bool.go b/conditional-statements/is_divisible_by_5_and_11_using_functions_using_bool.go new file mode 100644 index 0000000..5c4c639 --- /dev/null +++ b/conditional-statements/is_divisible_by_5_and_11_using_functions_using_bool.go @@ -0,0 +1,42 @@ +package main +import "fmt" +func main(){ + var n int + fmt.Print("Enter a number: ") + fmt.Scan(&n) + + if(n % 5 == 0 && n % 11 == 0){ + fmt.Println("The given number is divisible by both 5 and 11") + } + + if(n % 5 == 0 && n % 11 != 0){ + fmt.Println("The given number is divisible by 5, but not 11") + } + if(n % 5 != 0 && n % 11 == 0){ + fmt.Println("The given number is divisible by 11, but not 5") + } + + if(n % 5 != 0 && n % 11 != 0){ + fmt.Println("The given number is not divisible by 5 and 11") + } +} + +/** Outputs + +$ go run is_divisible_by_5_and_11.go +Enter a number: 55 +The given number is divisible by both 5 and 11 + +$ go run is_divisible_by_5_and_11.go +Enter a number: 22 +The given number is divisible by 11, but not 5 + +$ go run is_divisible_by_5_and_11.go +Enter a number: 25 +The given number is divisible by 5, but not 11 + +$ go run is_divisible_by_5_and_11.go +Enter a number: 13 +The given number is not divisible by 5 and 11 + +*/ diff --git a/conditional-statements/is_even_or_odd.go b/conditional-statements/is_even_or_odd.go new file mode 100644 index 0000000..bae6996 --- /dev/null +++ b/conditional-statements/is_even_or_odd.go @@ -0,0 +1,29 @@ +package main +import "fmt" +func main(){ + var n int + fmt.Print("Enter a number: ") + fmt.Scan(&n) + + if(n % 2 == 0){ + fmt.Println("Given number is even") + } else { + fmt.Println("Given number is odd") + } +} + +/** Outputs + +$ go run is_even_or_odd.go +Enter a number: 2 +Given number is even + +$ go run is_even_or_odd.go +Enter a number: 3 +Given number is odd + +$ go run is_even_or_odd.go +Enter a number: 0 +Given number is even + +*/ diff --git a/conditional-statements/is_leap_year.go b/conditional-statements/is_leap_year.go new file mode 100644 index 0000000..7c0aa41 --- /dev/null +++ b/conditional-statements/is_leap_year.go @@ -0,0 +1,66 @@ +/* +Refer: Leap year: https://en.wikipedia.org/wiki/Leap_year + +Algorithm: +if (year is not divisible by 4) then (it is a common year) +else if (year is not divisible by 100) then (it is a leap year) +else if (year is not divisible by 400) then (it is a common year) +else (it is a leap year +*/ +package main +import "fmt" +func isLeapYear(year int)bool{ + if(year % 4 != 0 ){ + return false + } else if(year % 100 != 0){ + return true + } else if(year % 400 != 0){ + return false + } else { + return true + } + + /* + Another method: compact yet complex + if(((year % 4 == 0) and (year % 100 !=0)) or (year % 400==0)) + { + LEAP YEAR + } + else + { + COMMON YEAR; + } + */ +} +func main(){ + var year int + fmt.Print("Enter year: ") + fmt.Scan(&year) + + if(isLeapYear(year)){ + fmt.Println("Leap Year") + } else { + fmt.Println("Common Year") + } + +} + +/** Outputs + +$ go run is_leap_year.go +Enter year: 2018 +Common Year + +$ go run is_leap_year.go +Enter year: 2020 +Leap Year + +$ go run is_leap_year.go +1Enter year: 900 +Common Year + +$ go run is_leap_year.go +Enter year: 2024 +Leap Year + +*/ \ No newline at end of file diff --git a/conditional-statements/is_upper_case.go b/conditional-statements/is_upper_case.go new file mode 100644 index 0000000..fcc2fdc --- /dev/null +++ b/conditional-statements/is_upper_case.go @@ -0,0 +1,39 @@ +package main +import ( + "fmt" + "bufio" + "os" +) +func main(){ + reader := bufio.NewReader(os.Stdin) + fmt.Print("Enter a letter from alphabet: ") + char, _, err := reader.ReadRune() + + if err != nil { + fmt.Println(err) + } + + if char >= 'a' && char <= 'z' { + fmt.Println("LOWER_CASE") + } else if char >= 'A' && char <= 'Z' { + fmt.Println("UPPER_CASE") + } else { + fmt.Println("NOT_AN_ALPHABET") + } +} + +/** Outputs + +$ go run is_upper_case.go +Enter a letter from alphabet: g +LOWER_CASE + +$ go run is_upper_case.go +Enter a letter from alphabet: G +UPPER_CASE + +$ go run is_upper_case.go +Enter a letter from alphabet: 8 +NOT_AN_ALPHABET + +*/ \ No newline at end of file diff --git a/conditional-statements/is_valid_triangle.go b/conditional-statements/is_valid_triangle.go new file mode 100644 index 0000000..5b0a317 --- /dev/null +++ b/conditional-statements/is_valid_triangle.go @@ -0,0 +1,36 @@ +package main +import "fmt" +func isValidTriangle(angle1, angle2, angle3 float64)bool{ + return ((angle1 + angle2 + angle3) == 180) +} +func main(){ + var angle1, angle2, angle3 float64 + fmt.Print("Enter first angle: ") + fmt.Scan(&angle1) + fmt.Print("Enter second angle: ") + fmt.Scan(&angle2) + fmt.Print("Enter third angle: ") + fmt.Scan(&angle3) + + if(isValidTriangle(angle1, angle2, angle3)){ + fmt.Println("The give angles CAN form a triangle") + } else { + fmt.Println("The give angles CAN_NOT form a triangle") + } +} + +/** Output + +$ go run is_valid_triangle.go +Enter first angle: 30 +Enter second angle: 60 +Enter third angle: 90 +The give angles CAN form a triangle + +$ go run is_valid_triangle.go +Enter first angle: 90 +Enter second angle: 90 +Enter third angle: 90 +The give angles CAN_NOT form a triangle + +*/ \ No newline at end of file diff --git a/conditional-statements/is_vowel.go b/conditional-statements/is_vowel.go new file mode 100644 index 0000000..33e41e0 --- /dev/null +++ b/conditional-statements/is_vowel.go @@ -0,0 +1,41 @@ +package main +import ( + "fmt" + "bufio" + "os" +) +func main(){ + reader := bufio.NewReader(os.Stdin) + fmt.Print("Enter a letter from alphabet: ") + char, _, err := reader.ReadRune() + + if err != nil { + fmt.Println(err) + } + if (char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z') { + switch char { + case 'A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u': + fmt.Println("VOWEL") + default: + fmt.Println("CONSONANT") + } + } else { + fmt.Println("The input is not an alphabet") + } +} + +/** + +$ go run is_vowel.go +Enter a letter from alphabet: A +VOWEL + +$ go run is_vowel.go +Enter a letter from alphabet: g +CONSONANT + +$ go run is_vowel.go +Enter a letter from alphabet:6 +The input is not an alphabet + +*/ \ No newline at end of file diff --git a/conditional-statements/max_of_three_numbers.go b/conditional-statements/max_of_three_numbers.go new file mode 100644 index 0000000..279d0bb --- /dev/null +++ b/conditional-statements/max_of_three_numbers.go @@ -0,0 +1,50 @@ +package main +import "fmt" +func main(){ + var a int + var b int + var c int + + fmt.Print("Enter first number: ") + fmt.Scan(&a) + fmt.Print("Enter second number: ") + fmt.Scan(&b) + fmt.Print("Enter third number: ") + fmt.Scan(&c) + + if(a > b){ + if(a > c){ + fmt.Println("Max Number = ", a) + } else { + fmt.Println("Max Number = ", c) + } + } else { + if(b > c){ + fmt.Println("Max Number = ", b) + } else { + fmt.Println("Max Number = ", c) + } + } +} + +/** Outputs + +$ go run max_of_three_numbers.go +Enter first number: 10 +Enter second number: 20 +Enter third number: 30 +Max Number = 30 + +$ go run max_of_three_numbers.go +Enter first number: 10 +Enter second number: 30 +Enter third number: 20 +Max Number = 30 + +$ go run max_of_three_numbers.go +Enter first number: 30 +Enter second number: 10 +Enter third number: 20 +Max Number = 30 + +*/ \ No newline at end of file diff --git a/conditional-statements/max_of_three_numbers.goclear b/conditional-statements/max_of_three_numbers.goclear new file mode 100644 index 0000000..e69de29 diff --git a/conditional-statements/max_of_three_numbers_using_functions.go b/conditional-statements/max_of_three_numbers_using_functions.go new file mode 100644 index 0000000..d6bd8e2 --- /dev/null +++ b/conditional-statements/max_of_three_numbers_using_functions.go @@ -0,0 +1,46 @@ +package main +import "fmt" +func getMax(a, b int)(int){ + if(a > b){ + return a + } else { + return b + } +} +func main(){ + var a int + var b int + var c int + + fmt.Print("Enter first number: ") + fmt.Scan(&a) + fmt.Print("Enter second number: ") + fmt.Scan(&b) + fmt.Print("Enter third number: ") + fmt.Scan(&c) + + max := getMax(getMax(a, b), c) + fmt.Println("Max Number =", max) +} + +/** Outputs + +$ go run max_of_three_numbers.go +Enter first number: 10 +Enter second number: 20 +Enter third number: 30 +Max Number = 30 + +$ go run max_of_three_numbers.go +Enter first number: 10 +Enter second number: 30 +Enter third number: 20 +Max Number = 30 + +$ go run max_of_three_numbers.go +Enter first number: 30 +Enter second number: 10 +Enter third number: 20 +Max Number = 30 + +*/ \ No newline at end of file diff --git a/conditional-statements/max_of_two_numbers.go b/conditional-statements/max_of_two_numbers.go new file mode 100644 index 0000000..cdc6f75 --- /dev/null +++ b/conditional-statements/max_of_two_numbers.go @@ -0,0 +1,41 @@ +package main +import "fmt" +func main(){ + var a int + var b int + fmt.Print("Enter first number: ") + fmt.Scan(&a) + fmt.Print("Enter second number: ") + fmt.Scan(&b) + + if(a > b){ + fmt.Println("Max number is: ", a) + } + + if(b > a){ + fmt.Println("Max number is: ", b) + } + + if(a == b){ + fmt.Println("Both numbers are equal") + } +} + +/** Outputs + +$ go run max_of_two_numbers.go +Enter first number: 20 +Enter second number: 10 +Max number is: 20 + +$ go run max_of_two_numbers.go +Enter first number: 10 +Enter second number: 20 +Max number is: 20 + +$ go run max_of_two_numbers.go +Enter first number: 20 +Enter second number: 20 +Both numbers are equal + +*/ \ No newline at end of file diff --git a/conditional-statements/max_of_two_numbers_using_functions.go b/conditional-statements/max_of_two_numbers_using_functions.go new file mode 100644 index 0000000..31f7d13 --- /dev/null +++ b/conditional-statements/max_of_two_numbers_using_functions.go @@ -0,0 +1,38 @@ +package main +import "fmt" +func printMax(a int, b int){ + if(a > b){ + fmt.Println("Max number is: ", a) + } + + if(b > a){ + fmt.Println("Max number is: ", b) + } + + if(a == b){ + fmt.Println("Both numbers are equal") + } + + fmt.Println(".............") +} + +func main(){ + fmt.Print("20, 10 :> ") + printMax(20, 10) + fmt.Print("10, 20 :> ") + printMax(10, 20) + fmt.Print("20, 20 :> ") + printMax(20, 20) +} + +/** Output + +$ go run max_of_two_numbers_using_functions.go +20, 10 :> Max number is: 20 +............. +10, 20 :> Max number is: 20 +............. +20, 20 :> Both numbers are equal +............. + +*/ \ No newline at end of file diff --git a/conditional-statements/print_week_day.go b/conditional-statements/print_week_day.go new file mode 100644 index 0000000..2499143 --- /dev/null +++ b/conditional-statements/print_week_day.go @@ -0,0 +1,41 @@ +package main +import "fmt" +func main(){ + var n int + fmt.Print("Enter week number(1 - 7): ") + fmt.Scan(&n) + + if n == 1{ + fmt.Println("Monday") + } else if n == 2{ + fmt.Println("Tuesday") + } else if n == 3{ + fmt.Println("Wednesday") + } else if n == 4{ + fmt.Println("Thursday") + } else if n == 5 { + fmt.Println("Friday") + } else if n == 6{ + fmt.Println("Saturday") + } else if n == 7{ + fmt.Println("Sunday") + } else{ + fmt.Println("Please enter a valid value between 1 and 7") + } +} + +/** Outputs + +$ go run print_week_day.go +Enter week number(1 - 7): 4 +Thursday + +$ go run print_week_day.go +Enter week number(1 - 7): 7 +Sunday + +$ go run print_week_day.go +Enter week number(1 - 7): 8 +Please enter a valid value: 1 - 7 + +*/ \ No newline at end of file diff --git a/conditional-statements/print_week_day_using_array.go b/conditional-statements/print_week_day_using_array.go new file mode 100644 index 0000000..435ffd0 --- /dev/null +++ b/conditional-statements/print_week_day_using_array.go @@ -0,0 +1,33 @@ +package main +import "fmt" +func main(){ + var n int + fmt.Print("Enter week number(1 - 7): ") + fmt.Scan(&n) + /* Note: There is a dedicated chapter to demonstrate arrays. + * This purpose of this program is to show the easy method to 'print_week_day_using_switch.go' + */ + weekDays := [7]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"} + + if(n >= 1 && n <= 7){ + fmt.Println(weekDays[n - 1]) // Array index starts from 0. + } else{ + fmt.Println("Please enter a valid value between 1 and 7") + } +} + +/** Outputs + +$ go run print_week_day.go +Enter week number(1 - 7): 4 +Thursday + +$ go run print_week_day.go +Enter week number(1 - 7): 7 +Sunday + +$ go run print_week_day.go +Enter week number(1 - 7): 8 +Please enter a valid value: 1 - 7 + +*/ \ No newline at end of file diff --git a/conditional-statements/print_week_day_using_switch.go b/conditional-statements/print_week_day_using_switch.go new file mode 100644 index 0000000..6430329 --- /dev/null +++ b/conditional-statements/print_week_day_using_switch.go @@ -0,0 +1,42 @@ +package main +import "fmt" +func main(){ + var n int + fmt.Print("Enter week number(1 - 7): ") + fmt.Scan(&n) + + switch n { + case 1: + fmt.Println("Monday") + case 2: + fmt.Println("Tuesday") + case 3: + fmt.Println("Wednesday") + case 4: + fmt.Println("Thursday") + case 5: + fmt.Println("Friday") + case 6: + fmt.Println("Saturday") + case 7: + fmt.Println("Sunday") + default: + fmt.Println("Please enter a valid value between 1 and 7") + } +} + +/** Outputs + +$ go run print_week_day.go +Enter week number(1 - 7): 4 +Thursday + +$ go run print_week_day.go +Enter week number(1 - 7): 7 +Sunday + +$ go run print_week_day.go +Enter week number(1 - 7): 8 +Please enter a valid value: 1 - 7 + +*/ \ No newline at end of file diff --git a/loops/README.md b/loops/README.md new file mode 100644 index 0000000..e148702 --- /dev/null +++ b/loops/README.md @@ -0,0 +1,54 @@ +# Golang Programming Exercises - Loops +- [Program to print all natural numbers from 1 to n](natural_numbers.go) +- [Program to print all natural numbers in reverse (from n to 1)](natural_numbers_in_reverse.go) +- [Program to print all alphabets from a to z](print_alphabet.go) +- [Program to print all even numbers between 1 to 100](print_evens_till_100.go) +- [Program to print all odd number between 1 to 100](print_odds_till_100.go) +- [Program to find sum of all natural numbers between 1 to n](sum_of_naturals.go) +- [Program to find sum of all even numbers between 1 to n](sum_of_evens.go) +- [Program to find sum of all odd numbers between 1 to n](sum_of_odds.go) +- [Program to print multiplication table of any number](multiplication_table.go) +- [Program to count number of digits in a number](multiplication_table.go) +- [Program to find first and last digit of a number](digits_of_a_number.go) +- [Program to swap first and last digits of a number](swap_digits_in_a_number.go) +- [Program to calculate sum of digits of a number](sum_of_digits.go) +- [Program to calculate product of digits of a number](product_of_digits.go) +- [Program to enter a number and print its reverse](reverse_of_a_number.go) +- [Program to check whether a number is palindrome or not](is_pallindrome.go) +- [Program to find frequency of each digit in a given integer - using a map](frequency_of_digits.go) +- [Program to find frequency of each digit in a given integer - using an arrray](frequency_of_digits_using_array.go) +- [Program to enter a number and print it in words](print_num_in_words.go) +- [Program to print all ASCII character with their values](print_all_asii.go) +- [Program to find power of a number](get_power.go) +- [Program to find all factors of a number](find_factors.go) +- [Program to calculate factorial of a number](factorial.go) +- [Program to find HCF (GCD) of two numbers](gcd.go) +- [Program to find LCM of two numbers](lcm.go) +- [Program to check whether a number is Prime number or not](is_prime.go) +- [Program to print all Prime numbers between 1 to 100](print_primes.go) +- [Program to find all prime factors of a number](prime_factors.go) +- [Program to check whether a number is Armstrong number or not](is_armstrong.go) +- Program to print all Armstrong numbers between 1 to n (TO BE WRITTEN) +- [Program to check whether a number is Perfect number or not](is_perfect.go) +- Program to print all Perfect numbers between 1 to n (TO BE WRITTEN) +- [Program to check whether a number is Strong number or not](is_strong.go) +- Program to print all Strong numbers between 1 to n (TO BE WRITTEN) + +## To be written +- Program to print Fibonacci series up to n terms + +- Program to find one's complement of a binary number +- Program to find two's complement of a binary number +- Program to convert Binary to Octal number system +- Program to convert Binary to Decimal number system +- Program to convert Binary to Hexadecimal number system +- Program to convert Octal to Binary number system +- Program to convert Octal to Decimal number system +- Program to convert Octal to Hexadecimal number system +- Program to convert Decimal to Binary number system +- Program to convert Decimal to Octal number system +- Program to convert Decimal to Hexadecimal number system +- Program to convert Hexadecimal to Binary number system +- Program to convert Hexadecimal to Octal number system +- Program to convert Hexadecimal to Decimal number system +- Program to print Pascal triangle upto n rows \ No newline at end of file diff --git a/loops/digits_in_a_number.go b/loops/digits_in_a_number.go new file mode 100644 index 0000000..be86e12 --- /dev/null +++ b/loops/digits_in_a_number.go @@ -0,0 +1,30 @@ +package main +import "fmt" +func getNumDigits(num int)int{ + digitCount := 0 + for i := num; i > 0; i = i / 10{ + digitCount++ + } + + return digitCount +} + +func main(){ + var num int + fmt.Print("Entr a number: ") + fmt.Scan(&num) + + fmt.Println("Number of digits in the given number = ", getNumDigits(num)) +} + +/** Output + +$ go run digits_in_a_number.go +Entr a number: 2345 +Number of digits in the given number = 4 + +$ go run digits_in_a_number.go +Entr a number: 12345 +Number of digits in the given number = 5 + +*/ \ No newline at end of file diff --git a/loops/digits_of_a_number.go b/loops/digits_of_a_number.go new file mode 100644 index 0000000..389e15d --- /dev/null +++ b/loops/digits_of_a_number.go @@ -0,0 +1,36 @@ +package main +import "fmt" +func getDigits(num int)(int, int){ + lastDigit := num % 10 + firstDigit := lastDigit + for i := num / 10; i > 0; i = i / 10{ + firstDigit = i % 10 + } + + return firstDigit, lastDigit +} + +func main(){ + var num int + fmt.Print("Entr a number: ") + fmt.Scan(&num) + + f, l := getDigits(num) + + fmt.Println("First Digit =", f) + fmt.Println("Last Digit =", l) +} + +/** Output + +$ go run digits_of_a_number.go +Entr a number: 1234 +First Digit = 1 +Last Digit = 4 + +$ go run digits_of_a_number.go +Entr a number: 8899 +First Digit = 8 +Last Digit = 9 + +*/ \ No newline at end of file diff --git a/loops/factorial.go b/loops/factorial.go new file mode 100644 index 0000000..9aa2712 --- /dev/null +++ b/loops/factorial.go @@ -0,0 +1,24 @@ +package main +import "fmt" +func getFactorial(num int)int64{ + var fact int64 = 1 + for i := num; i > 0; i--{ + fact *= int64(i) + } + + return fact +} +func main(){ + fmt.Println("-----Finding Factorials-----") + fmt.Println("5! = ", getFactorial(5)) + fmt.Println("10! = ", getFactorial(10)) +} + +/** Output + +$ go run factorial.go +-----Finding Factorials----- +5! = 120 +10! = 3628800 + +*/ \ No newline at end of file diff --git a/loops/find_factors.go b/loops/find_factors.go new file mode 100644 index 0000000..bc9da92 --- /dev/null +++ b/loops/find_factors.go @@ -0,0 +1,27 @@ +package main +import "fmt" +func getFactors(n int)[]int{ + var factors []int + for i := 1; i <= n; i++ { + if n % i == 0 { + factors = append(factors, i) + } + } + return factors +} +func main(){ + fmt.Println("-----Printing Factors-----") + fmt.Println("12 -->", getFactors(12)) + fmt.Println("121 -->", getFactors(121)) + fmt.Println("1200 -->", getFactors(1200)) +} + +/** Output + +$ go run find_factors.go +-----Printing Factors----- +12 --> [1 2 3 4 6 12] +121 --> [1 11 121] +1200 --> [1 2 3 4 5 6 8 10 12 15 16 20 24 25 30 40 48 50 60 75 80 100 120 150 200 240 300 400 600 1200] + +*/ \ No newline at end of file diff --git a/loops/frequency_of_digits.go b/loops/frequency_of_digits.go new file mode 100644 index 0000000..5765da3 --- /dev/null +++ b/loops/frequency_of_digits.go @@ -0,0 +1,45 @@ +package main +import "fmt" +func getFrequencyOfDigits(num int)map[int]int{ + // Ten positions for 10 digits -> 0 to 9 + frequencies := map[int]int{ + 0: 0, + 1: 0, + 2: 0, + 3: 0, + 4: 0, + 5: 0, + 6: 0, + 7: 0, + 8: 0, + 9: 0, + } + + for i := num; i > 0; i /= 10{ + digit := i % 10 + frequencies[digit]++ + } + + // Deleting the zero valued keys + for i := 0; i <= 9; i++{ + if(frequencies[i] == 0){ + delete(frequencies, i) + } + } + return frequencies +} + +func main(){ + fmt.Println("-----Frequency of Digits-----") + fmt.Println("1001001 -->", getFrequencyOfDigits(1001001)) + fmt.Println("10221033111 -->", getFrequencyOfDigits(10221033111)) +} + +/** Output + +$ go run frequency_of_digits.go +-----Frequency of Digits----- +1001001 --> map[0:4 1:3] +10221033111 --> map[1:5 0:2 2:2 3:2] + +*/ \ No newline at end of file diff --git a/loops/frequency_of_digits_using_array.go b/loops/frequency_of_digits_using_array.go new file mode 100644 index 0000000..d8b6dd5 --- /dev/null +++ b/loops/frequency_of_digits_using_array.go @@ -0,0 +1,40 @@ +package main +import "fmt" +func getFrequencyOfDigits(num int)[10]int{ + // Ten positions for 10 digits -> 0 to 9 + frequencies := [10]int{} + + for i := num; i > 0; i /= 10{ + digit := i % 10 + frequencies[digit]++ + } + + /* + // Deleting the zero valued keys + for i := 0; i <= 9; i++{ + if(frequencies[i] == 0){ + delete(frequencies, i) + } + } + */ + return frequencies +} + +func main(){ + fmt.Println("-----Frequency of Digits-----") + fmt.Println(" 0 1 2 3 4 5 6 7 8 9") + fmt.Println(" ...................") + fmt.Println("1001001 -->", getFrequencyOfDigits(1001001)) + fmt.Println("10221033111 -->", getFrequencyOfDigits(10221033111)) +} + +/** Output + +$ go run frequency_of_digits_using_array.go +-----Frequency of Digits----- + 0 1 2 3 4 5 6 7 8 9 + ................... +1001001 --> [4 3 0 0 0 0 0 0 0 0] +10221033111 --> [2 5 2 2 0 0 0 0 0 0] + +*/ \ No newline at end of file diff --git a/loops/gcd.go b/loops/gcd.go new file mode 100644 index 0000000..cfba088 --- /dev/null +++ b/loops/gcd.go @@ -0,0 +1,26 @@ +package main +import "fmt" +func getGCD(n1, n2 int)int{ + for (n1 != n2){ + if(n1 > n2){ + n1 -= n2 + } else { + n2 -= n1 + } + } + return n1 +} +func main(){ + fmt.Println("-----GCD-----") + fmt.Println("GCD of 12 and 54:", getGCD(12, 54)) + fmt.Println("GCD of 121 and 343:", getGCD(121, 143)) +} + +/** Output + +$ go run gcd.go +-----GCD----- +GCD of 12 and 54: 6 +GCD of 121 and 343: 11 + +*/ \ No newline at end of file diff --git a/loops/get_power.go b/loops/get_power.go new file mode 100644 index 0000000..4592efd --- /dev/null +++ b/loops/get_power.go @@ -0,0 +1,27 @@ +// Power of a number x^y +package main +import "fmt" +func getPower(x ,y int)int{ + p := 1 + for i := 1; i <= y; i++{ + p *= x + } + + return p +} + +func main(){ + fmt.Println("2^3 = ", getPower(2, 3)) + fmt.Println("5^5 = ", getPower(5, 5)) + fmt.Println("10^3 = ", getPower(10, 3)) +} + +/** Output + +$ go run get_power.go + +2^3 = 8 +5^5 = 3125 +10^3 = 1000 + +*/ \ No newline at end of file diff --git a/loops/is_armstrong.go b/loops/is_armstrong.go new file mode 100644 index 0000000..8af715a --- /dev/null +++ b/loops/is_armstrong.go @@ -0,0 +1,43 @@ +/* +Arm strong number: + Def1: Sum of cubes of digits of the number = the number + Def2: A number that is the sum of its own digits each raised to the power of the number of digits. +eg: +153 = 1^3 + 5^3 + 3^3 +370 = 3^3 + 7^3 + 0^3 +371 = 3^3 + 7^3 + 1^3 +407 = 4^3 + 0^3 + 7^3 +*/ +package main +import ( + "fmt" + "math" +) +func isArmstrong(num int)bool{ + sum := 0 + for i := num; i > 0; i = i / 10{ + sum += int(math.Pow(float64(i % 10), 3)) + } + + return sum == num +} +func main(){ + fmt.Println("-----Is Armstrong Number-----") + fmt.Println("111 ---->", isArmstrong(100)) + fmt.Println("153 ---->", isArmstrong(153)) + fmt.Println("370 ---->", isArmstrong(370)) + fmt.Println("371 ---->", isArmstrong(371)) + fmt.Println("407 ---->", isArmstrong(407)) +} + +/** Output + +$ go run is_armstrong.go +-----Is Armstrong Number----- +111 ----> false +153 ----> true +370 ----> true +371 ----> true +407 ----> true + +*/ \ No newline at end of file diff --git a/loops/is_pallindrome.go b/loops/is_pallindrome.go new file mode 100644 index 0000000..cbf87ea --- /dev/null +++ b/loops/is_pallindrome.go @@ -0,0 +1,25 @@ +package main +import "fmt" +func getReverse(num int)int{ + rev := 0 + for i := num; i > 0; i = i/10{ + rev = rev * 10 + (i % 10) + } + + return rev +} + +func main(){ + fmt.Println("-----Is Pallindrome-----") + fmt.Println(1234, "-->", getReverse(1234) == 1234) + fmt.Println(1001, "-->", getReverse(1001) == 1001) +} + +/** Output + +$ go run is_pallindrome.go +-----Is Pallindrome----- +1234 --> false +1001 --> true + +*/ \ No newline at end of file diff --git a/loops/is_perfect.go b/loops/is_perfect.go new file mode 100644 index 0000000..baffdfa --- /dev/null +++ b/loops/is_perfect.go @@ -0,0 +1,45 @@ +/* +Perfect Number: + the number == the sum of its positive divisors excluding the number itself +eg: + 6 :> + devisors sum => 1 + 2 + 3 == 6 + 28 :> + devisors sum => 1 + 2 + 4 + 7 + 14 == 28 + 496 :> + devisors sum => 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 == 496 + 8128 :> + devisors sum => 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064 +*/ +package main +import "fmt" +func isPerfect(num int)bool{ + sum := 0 + for i := 1; i < num; i++{ + if num % i == 0 { + sum += i; + } + } + + return sum == num +} +func main(){ + fmt.Println("-----Is Perfect Number-----") + fmt.Println(6, "------->", isPerfect(6)) + fmt.Println(28, "------>", isPerfect(28)) + fmt.Println(90, "------>", isPerfect(90)) + fmt.Println(496, "----->", isPerfect(496)) + fmt.Println(8128, "---->", isPerfect(8128)) +} + +/** Output + +$ go run is_perfect.go +-----Is Perfect Number----- +6 -------> true +28 ------> true +90 ------> false +496 -----> true +8128 ----> true + +*/ \ No newline at end of file diff --git a/loops/is_prime.go b/loops/is_prime.go new file mode 100644 index 0000000..82500c1 --- /dev/null +++ b/loops/is_prime.go @@ -0,0 +1,33 @@ +package main +import "fmt" +func isPrime(num int)bool{ + var hasFactors = true + for i := 2; i <= num / 2; i++ { + if(num % i == 0){ + hasFactors = false + break + } + } + + return hasFactors +} +func main(){ + fmt.Println("-----Is Prime-----") + fmt.Println("2 --->", isPrime(2)) + fmt.Println("11 --->", isPrime(11)) + fmt.Println("12 --->", isPrime(12)) + fmt.Println("13 --->", isPrime(13)) + fmt.Println("14 --->", isPrime(14)) +} + +/** Output + +$ go run is_prime.go +-----Is Prime----- +2 ---> true +11 ---> true +12 ---> false +13 ---> true +14 ---> false + +*/ \ No newline at end of file diff --git a/loops/is_strong.go b/loops/is_strong.go new file mode 100644 index 0000000..b4b47cc --- /dev/null +++ b/loops/is_strong.go @@ -0,0 +1,48 @@ +/** +Strong Number: the sum of factorial of the digits in any number is equal the given number + +eg: +1 :> + 1! = 1 +2 :> + 2! = 2 +145 :> + 1! + 4! + 5! => 1 + 24 + 120 => 145 + +*/ +package main +import "fmt" +func getFactorial(num int)int64{ + var fact int64 = 1 + for i := num; i > 0; i--{ + fact *= int64(i) + } + + return fact +} +func isStrong(num int)bool{ + sum := 0 + for i := num; i > 0; i /= 10{ + sum += int(getFactorial(i % 10)) + } + + return sum == num +} +func main(){ + fmt.Println("-----Is Strong Number-----") + fmt.Println(1, "----->", isStrong(1)) + fmt.Println(2, "----->", isStrong(2)) + fmt.Println(145, "--->", isStrong(145)) + fmt.Println(20, "---->", isStrong(20)) +} + +/** Output + +$ go run is_strong.go +-----Is Strong Number----- +1 -----> true +2 -----> true +145 ---> true +20 ----> false + +*/ \ No newline at end of file diff --git a/loops/lcm.go b/loops/lcm.go new file mode 100644 index 0000000..4faa2e6 --- /dev/null +++ b/loops/lcm.go @@ -0,0 +1,51 @@ +package main +import "fmt" +func getLCM(num1, num2 int)int{ + var max int + if num1 > num2 { + max = num1 + } else { + max = num2 + } + + // First multiple to be checked + i := max + var lcm int + + // Run loop indefinitely till LCM is found + for { + if i % num1 == 0 && i % num2 == 0 { + /* + * If 'i' divides both 'num1' and 'num2' + * then 'i' is the LCM. + */ + lcm = i; + + /* Terminate the loop after LCM is found */ + break + } + + /* + * If LCM is not found then generate next + * multiple of max between both numbers + */ + i += max + } + + return lcm +} + +func main(){ + fmt.Println("-----LCM-----") + fmt.Println("LCM of 12 and 30:", getLCM(12, 30)) + fmt.Println("LCM of 4 and 10:", getLCM(4, 10)) +} + +/** Output + +$ go run lcm.go +-----LCM----- +LCM of 12 and 30: 60 +LCM of 4 and 10: 20 + +*/ \ No newline at end of file diff --git a/loops/multiplication_table.go b/loops/multiplication_table.go new file mode 100644 index 0000000..afdd7d9 --- /dev/null +++ b/loops/multiplication_table.go @@ -0,0 +1,39 @@ +package main +import "fmt" +func printTable(n int){ + for i := 1; i <=10; i++{ + fmt.Printf("%d x %d = %d\n", n, i, (n * i)) + } +} +func main(){ + printTable(2) + fmt.Println("...............") + printTable(5) +} + +/** Output + +$ go run multiplication_table.go +2 x 1 = 2 +2 x 2 = 4 +2 x 3 = 6 +2 x 4 = 8 +2 x 5 = 10 +2 x 6 = 12 +2 x 7 = 14 +2 x 8 = 16 +2 x 9 = 18 +2 x 10 = 20 +............... +5 x 1 = 5 +5 x 2 = 10 +5 x 3 = 15 +5 x 4 = 20 +5 x 5 = 25 +5 x 6 = 30 +5 x 7 = 35 +5 x 8 = 40 +5 x 9 = 45 +5 x 10 = 50 + +*/ \ No newline at end of file diff --git a/loops/natural_numbers.go b/loops/natural_numbers.go new file mode 100644 index 0000000..ada3ae7 --- /dev/null +++ b/loops/natural_numbers.go @@ -0,0 +1,30 @@ +package main +import "fmt" +func printNumbers(n int){ + for i := 1; i <= n; i++ { + fmt.Println(i) + } +} +func main(){ + var n int + fmt.Print("Enter a number: ") + fmt.Scan(&n) + printNumbers(n) +} + +/** Output + +$ go run natural_numbers.go +Enter a number: 10 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 + +*/ \ No newline at end of file diff --git a/loops/natural_numbers_in_reverse.go b/loops/natural_numbers_in_reverse.go new file mode 100644 index 0000000..01b0d34 --- /dev/null +++ b/loops/natural_numbers_in_reverse.go @@ -0,0 +1,30 @@ +package main +import "fmt" +func printNumbersInReverse(n int){ + for i := n; i >= 1; i-- { + fmt.Println(i) + } +} +func main(){ + var n int + fmt.Print("Enter a number: ") + fmt.Scan(&n) + printNumbersInReverse(n) +} + +/** Output + +$ go run natural_numbers_in_reverse.go +Enter a number: 10 +10 +9 +8 +7 +6 +5 +4 +3 +2 +1 + +*/ \ No newline at end of file diff --git a/loops/prime_factors.go b/loops/prime_factors.go new file mode 100644 index 0000000..cecc72f --- /dev/null +++ b/loops/prime_factors.go @@ -0,0 +1,39 @@ +package main +import "fmt" +func isPrime(num int)bool{ + var hasFactors = true + for i := 2; i <= num / 2; i++ { + if(num % i == 0){ + hasFactors = false + break + } + } + + return hasFactors +} +func getPrimeFactors(num int)[]int{ + primeFactors := []int{} + for i := 2; i <= num / 2; i++{ + if num % i == 0 && isPrime(i){ + primeFactors = append(primeFactors, i) + } + } + + return primeFactors +} +func main(){ + fmt.Println("-----Printing Prime Factors-----") + fmt.Println("Prime Factors of 12 :", getPrimeFactors(12)) + fmt.Println("Prime Factors of 100:", getPrimeFactors(100)) + fmt.Println("Prime Factors of 121:", getPrimeFactors(121)) +} + +/** Output + +$ go run prime_factors.go +-----Printing Prime Factors----- +Prime Factors of 12 : [2 3] +Prime Factors of 100: [2 5] +Prime Factors of 121: [11] + +*/ \ No newline at end of file diff --git a/loops/print_all_asii.go b/loops/print_all_asii.go new file mode 100644 index 0000000..18299cd --- /dev/null +++ b/loops/print_all_asii.go @@ -0,0 +1,267 @@ +package main +import "fmt" +func main(){ + for i := 0; i < 256; i++{ + fmt.Printf("%c - %d\n", i, i) + } +} +/** Output +$ go run print_all_asii.go + - 0 + - 1 + - 2 + - 3 + - 4 + - 5 + - 6 + - 7 - 8 + - 9 + + - 10 + - 11 + - 12 + - 13 + - 14 + - 15 + - 16 + - 17 + - 18 + - 19 + - 20 + - 21 + - 22 + - 23 + - 24 + - 25 + - 26 +7 + - 28 + - 29 + - 30 + - 31 + - 32 +! - 33 +" - 34 +# - 35 +$ - 36 +% - 37 +& - 38 +' - 39 +( - 40 +) - 41 +* - 42 ++ - 43 +, - 44 +- - 45 +. - 46 +/ - 47 +0 - 48 +1 - 49 +2 - 50 +3 - 51 +4 - 52 +5 - 53 +6 - 54 +7 - 55 +8 - 56 +9 - 57 +: - 58 +; - 59 +< - 60 += - 61 +> - 62 +? - 63 +@ - 64 +A - 65 +B - 66 +C - 67 +D - 68 +E - 69 +F - 70 +G - 71 +H - 72 +I - 73 +J - 74 +K - 75 +L - 76 +M - 77 +N - 78 +O - 79 +P - 80 +Q - 81 +R - 82 +S - 83 +T - 84 +U - 85 +V - 86 +W - 87 +X - 88 +Y - 89 +Z - 90 +[ - 91 +\ - 92 +] - 93 +^ - 94 +_ - 95 +` - 96 +a - 97 +b - 98 +c - 99 +d - 100 +e - 101 +f - 102 +g - 103 +h - 104 +i - 105 +j - 106 +k - 107 +l - 108 +m - 109 +n - 110 +o - 111 +p - 112 +q - 113 +r - 114 +s - 115 +t - 116 +u - 117 +v - 118 +w - 119 +x - 120 +y - 121 +z - 122 +{ - 123 +| - 124 +} - 125 +~ - 126 + - 127 +€ - 128 + - 129 +‚ - 130 +ƒ - 131 +„ - 132 +… - 133 +† - 134 +‡ - 135 +ˆ - 136 +‰ - 137 +Š - 138 +‹ - 139 +Œ - 140 + - 141 +Ž - 142 + - 143 + - 144 +‘ - 145 +’ - 146 +“ - 147 +” - 148 +• - 149 +– - 150 +— - 151 +˜ - 152 +™ - 153 +š - 154 + + - 156 + - 157 +ž - 158 +Ÿ - 159 +  - 160 +¡ - 161 +¢ - 162 +£ - 163 +¤ - 164 +¥ - 165 +¦ - 166 +§ - 167 +¨ - 168 +© - 169 +ª - 170 +« - 171 +¬ - 172 +­ - 173 +® - 174 +¯ - 175 +° - 176 +± - 177 +² - 178 +³ - 179 +´ - 180 +µ - 181 +¶ - 182 +· - 183 +¸ - 184 +¹ - 185 +º - 186 +» - 187 +¼ - 188 +½ - 189 +¾ - 190 +¿ - 191 +À - 192 +Á - 193 + - 194 +à - 195 +Ä - 196 +Å - 197 +Æ - 198 +Ç - 199 +È - 200 +É - 201 +Ê - 202 +Ë - 203 +Ì - 204 +Í - 205 +Î - 206 +Ï - 207 +Ð - 208 +Ñ - 209 +Ò - 210 +Ó - 211 +Ô - 212 +Õ - 213 +Ö - 214 +× - 215 +Ø - 216 +Ù - 217 +Ú - 218 +Û - 219 +Ü - 220 +Ý - 221 +Þ - 222 +ß - 223 +à - 224 +á - 225 +â - 226 +ã - 227 +ä - 228 +å - 229 +æ - 230 +ç - 231 +è - 232 +é - 233 +ê - 234 +ë - 235 +ì - 236 +í - 237 +î - 238 +ï - 239 +ð - 240 +ñ - 241 +ò - 242 +ó - 243 +ô - 244 +õ - 245 +ö - 246 +÷ - 247 +ø - 248 +ù - 249 +ú - 250 +û - 251 +ü - 252 +ý - 253 +þ - 254 +ÿ - 255 + +*/ \ No newline at end of file diff --git a/loops/print_alphabet.go b/loops/print_alphabet.go new file mode 100644 index 0000000..90a8966 --- /dev/null +++ b/loops/print_alphabet.go @@ -0,0 +1,42 @@ +package main +import "fmt" +func printAlphabet(){ + for char := 'a'; char <= 'z'; char++ { + fmt.Printf("%c\n", char) + } +} +func main(){ + printAlphabet() +} + +/** Output + +$ go run print_alphabet.go +a +b +c +d +e +f +g +h +i +j +k +l +m +n +o +p +q +r +s +t +u +v +w +x +y +z + +*/ \ No newline at end of file diff --git a/loops/print_evens_till_100.go b/loops/print_evens_till_100.go new file mode 100644 index 0000000..2778e4b --- /dev/null +++ b/loops/print_evens_till_100.go @@ -0,0 +1,10 @@ +package main +import "fmt" +func printEvens(limit int){ + for i := 0; i <= limit; i += 2 { + fmt.Println(i) + } +} +func main(){ + printEvens(100) +} diff --git a/loops/print_num_in_words.go b/loops/print_num_in_words.go new file mode 100644 index 0000000..f183c07 --- /dev/null +++ b/loops/print_num_in_words.go @@ -0,0 +1,130 @@ +package main +import ( + "fmt" + "math" +) + +var names = map[int]string{ + 0: "", + 1: "One", + 2: "Two", + 3: "Three", + 4: "Four", + 5: "Five", + 6: "Six", + 7: "Seven", + 8: "Eight", + 9: "Nine", + 10: "Ten", + 11: "Eleven", + 12: "Twelve", + 13: "Thirteen", + 14: "Fourteen", + 15: "Fifteen", + 16: "Sixteen", + 17: "Seventeen", + 18: "Eighteen", + 19: "Nineteen", + 20: "Twenty", + 30: "Thirty", + 40: "Forty", + 50: "Fifty", + 60: "Sixty", + 70: "Seventy", + 80: "Eighty", + 90: "Ninety", + 100: "Hundred", + 1000: "Thousand", + 10000: "Ten Thousand", + 100000: "Million", + 1000000: "Ten Million", + 10000000: "Billion", + 100000000: "Ten Billion", + 1000000000: "Trillion", +} +func isPowerOf10(num int)bool{ + var pow int64 = 1 + for pow < int64(num) { + pow *= 10 + } + return pow == int64(num) +} +func getNameForTwoDigitNumber(num int)string{ + if num < 20 { + return names[num] + } + + return names[(num / 10) * 10] + " " + names[num % 10] +} +func getNameForThreeDigitNumber(num int)string{ + return names[num/100] + " hundred " + getNameForTwoDigitNumber(num % 100) +} +func getNumberInWords(num int)string{ + // We choose map over array because map is more expressive when we read the code + + + if num == 0{ + return "zero" + } + + if isPowerOf10(num) || num < 20{ + return names[num] + } + numDigits := int(math.Log10(float64(num))) + 1 + + var namedNumber string + switch(numDigits){ + case 2: + namedNumber = getNameForTwoDigitNumber(num) + case 3: + namedNumber = getNameForThreeDigitNumber(num) + default: + powerIn10 := int(math.Pow(10, 3.0)) + namedNumber = getNumberInWords(num / powerIn10) + " " + names[powerIn10] + " " + getNumberInWords(num % powerIn10) + } + + return namedNumber +} + +func main(){ + fmt.Println("-----Printing numbers in words-----") + fmt.Println(0, "-------------->", getNumberInWords(0)) + fmt.Println(6, "-------------->", getNumberInWords(6)) + fmt.Println(13, "------------->", getNumberInWords(13)) + fmt.Println(20, "------------->", getNumberInWords(20)) + fmt.Println(54, "------------->", getNumberInWords(54)) + fmt.Println(100, "------------>", getNumberInWords(100)) + fmt.Println(197, "------------>", getNumberInWords(197)) + fmt.Println(999, "------------>", getNumberInWords(999)) + fmt.Println(1000, "----------->", getNumberInWords(1000)) + fmt.Println(1004, "----------->", getNumberInWords(1004)) + fmt.Println(1999, "----------->", getNumberInWords(1999)) + fmt.Println(10000, "---------->", getNumberInWords(10000)) + fmt.Println(99999, "---------->", getNumberInWords(99999)) + fmt.Println(100000, "--------->", getNumberInWords(100000)) + fmt.Println(999999, "--------->", getNumberInWords(999999)) + fmt.Println(10000000, "------->", getNumberInWords(10000000)) +} + +/** Output + +$ go run print_num_in_words.go +-----Printing numbers in words----- +0 --------------> zero +6 --------------> Six +13 -------------> Thirteen +20 -------------> Twenty +54 -------------> Fifty Four +100 ------------> Hundred +197 ------------> One hundred Ninety Seven +999 ------------> Nine hundred Ninety Nine +1000 -----------> Thousand +1004 -----------> One Thousand Four +1999 -----------> One Thousand Nine hundred Ninety Nine +10000 ----------> Ten Thousand +99999 ----------> Ninety Nine Thousand Nine hundred Ninety Nine +100000 ---------> Million +999999 ---------> Nine hundred Ninety Nine Thousand Nine hundred Ninety Nine +10000000 -------> Billion + +*/ \ No newline at end of file diff --git a/loops/print_odds_till_100.go b/loops/print_odds_till_100.go new file mode 100644 index 0000000..9ee7930 --- /dev/null +++ b/loops/print_odds_till_100.go @@ -0,0 +1,10 @@ +package main +import "fmt" +func printOdds(limit int){ + for i := 1; i <= limit; i += 2 { + fmt.Println(i) + } +} +func main(){ + printOdds(100) +} diff --git a/loops/print_primes.go b/loops/print_primes.go new file mode 100644 index 0000000..c9b76a7 --- /dev/null +++ b/loops/print_primes.go @@ -0,0 +1,54 @@ +package main +import "fmt" +func isPrime(num int)bool{ + var hasFactors = true + for i := 2; i <= num / 2; i++ { + if(num % i == 0){ + hasFactors = false + break + } + } + + return hasFactors +} + +func main(){ + fmt.Println("-----Printing Primes till 100-----") + for i := 2; i <= 100; i++{ + if(isPrime(i)){ + fmt.Println(i) + } + } +} + +/** Output + +$ go run print_primes.go +-----Printing Primes till 100----- +2 +3 +5 +7 +11 +13 +17 +19 +23 +29 +31 +37 +41 +43 +47 +53 +59 +61 +67 +71 +73 +79 +83 +89 +97 + +*/ \ No newline at end of file diff --git a/loops/product_of_digits.go b/loops/product_of_digits.go new file mode 100644 index 0000000..344dfed --- /dev/null +++ b/loops/product_of_digits.go @@ -0,0 +1,22 @@ +package main +import "fmt" +func getProductOfDigits(num int)int{ + p := 1 + for i := num; i > 0; i = i / 10{ + p *= i % 10 + } + + return p +} +func main(){ + fmt.Println("Product of digits of 1234 =", getProductOfDigits(1234)) + fmt.Println("Product of digits of 1001 =", getProductOfDigits(1001)) +} + +/** Output + +$ go run product_of_digits.go +Product of digits of 1234 = 24 +Product of digits of 1001 = 0 + +*/ \ No newline at end of file diff --git a/loops/reverse_of_a_number.go b/loops/reverse_of_a_number.go new file mode 100644 index 0000000..f39eaac --- /dev/null +++ b/loops/reverse_of_a_number.go @@ -0,0 +1,25 @@ +package main +import "fmt" +func getReverse(num int)int{ + rev := 0 + for i := num; i > 0; i = i/10{ + rev = rev * 10 + (i % 10) + } + + return rev +} + +func main(){ + fmt.Println("-----Reverse of a number-----") + fmt.Println(1234, "-->", getReverse(1234)) + fmt.Println(1001, "-->", getReverse(1001)) +} + +/** Output + +$ go run reverse_of_a_number.go +-----Reverse of a number----- +1234 --> 4321 +1001 --> 1001 + +*/ \ No newline at end of file diff --git a/loops/sum_of_digits.go b/loops/sum_of_digits.go new file mode 100644 index 0000000..180b34f --- /dev/null +++ b/loops/sum_of_digits.go @@ -0,0 +1,22 @@ +package main +import "fmt" +func getSumOfDigits(num int)int{ + sum := 0 + for i := num; i > 0; i = i / 10{ + sum += i % 10 + } + + return sum +} +func main(){ + fmt.Println("Sum of digits of 1234 =", getSumOfDigits(1234)) + fmt.Println("Sum of digits of 1001 =", getSumOfDigits(1001)) +} + +/** Output + +$ go run sum_of_digits.go +Sum of digits of 1234 = 10 +Sum of digits of 1001 = 2 + +*/ \ No newline at end of file diff --git a/loops/sum_of_evens.go b/loops/sum_of_evens.go new file mode 100644 index 0000000..84cc2c3 --- /dev/null +++ b/loops/sum_of_evens.go @@ -0,0 +1,28 @@ +package main +import "fmt" +func getSum(limit int)int{ + sum := 0 + for i := 0; i <= 2 * limit; i+=2{ + sum += i + } + + return sum +} +func main(){ + var n int + fmt.Print("Enter a number: ") + fmt.Scan(&n) + fmt.Printf("Sum of first %d even numbers = %d\n", n, getSum(n)) +} + +/** Output + +$ go run sum_of_evens.go +Enter a number: 5 +Sum of first 5 even numbers = 30 + +$ go run sum_of_evens.go +Enter a number: 10 +Sum of first 10 even numbers = 110 + +*/ \ No newline at end of file diff --git a/loops/sum_of_naturals.go b/loops/sum_of_naturals.go new file mode 100644 index 0000000..5d43bda --- /dev/null +++ b/loops/sum_of_naturals.go @@ -0,0 +1,28 @@ +package main +import "fmt" +func getSum(limit int)int{ + sum := 0 + for i := 1; i <= limit; i++{ + sum += i + } + + return sum +} +func main(){ + var n int + fmt.Print("Enter a number: ") + fmt.Scan(&n) + fmt.Printf("Sum of first %d natural numbers = %d\n", n, getSum(n)) +} + +/** Output + +$ go run sum_of_naturals.go +Enter a number: 5 +Sum of first 5 natural numbers = 15 + +$ go run sum_of_naturals.go +Enter a number: 10 +Sum of first 10 natural numbers = 55 + +*/ \ No newline at end of file diff --git a/loops/sum_of_odds.go b/loops/sum_of_odds.go new file mode 100644 index 0000000..852fbd5 --- /dev/null +++ b/loops/sum_of_odds.go @@ -0,0 +1,28 @@ +package main +import "fmt" +func getSum(limit int)int{ + sum := 0 + for i := 1; i <= 2 * limit; i+=2{ + sum += i + } + + return sum +} +func main(){ + var n int + fmt.Print("Enter a number: ") + fmt.Scan(&n) + fmt.Printf("Sum of first %d odd numbers = %d\n", n, getSum(n)) +} + +/** Output + +$ go run sum_of_odds.go +Enter a number: 5 +Sum of first 5 odd numbers = 25 + +$ go run sum_of_odds.go +10Enter a number: +Sum of first 10 odd numbers = 100 + +*/ \ No newline at end of file diff --git a/loops/swap_digits_in_a_number.go b/loops/swap_digits_in_a_number.go new file mode 100644 index 0000000..fe9fd45 --- /dev/null +++ b/loops/swap_digits_in_a_number.go @@ -0,0 +1,76 @@ +/** +Swap the first and last digits of a number +Algorithm: + start + step 1: read num + step 2: numDigits = log10(num) + step 3: lastDigit = num % 10 + step 4: firstDigit = num / 10^(numDigits - 1) + + Find number formed by the digits excluding first and last digits of the number + Exclude first digit + step 5: middleNmber = num % 10^(numDigits - 1) + Exclude last digit + step 6: middleNumber = middleNumber % 10 + + Add in the first digit at the end + step 7: resultedNumber = middleNumber * 10 + firstDigit + Add in the last digit at the beginning + step 8: resultedNumber = lastDigit * 10^(numDigits - 1) + rsultedNumber + + resultedNumber is the result. + end +Eg: + start + step 1: let the number be 1234 + step 2: numDigits = log10(1234) = 4 + step 3: lastDigit = 1234 % 10 = 4 + step 4: firstDigit = 1234 / 10^(4-1) = 1234 / 1000 = 1 + + step 5: middleNumber = 1234 % 10^(4-1) = 1234 % 1000 = 234 + step 6: middleNumber = 234 % 10 = 23 + + step 7: resultedNumber = 23 * 10 + 1 = 231 + step 8: resultedNumber = 4 * 10^(4-1) + 231 = 4000 + 231 = 4231 + end +*/ +package main +import ( + "fmt" + "math" +) +func getSwappedNum(num int)(int){ + numDigits := int(math.Log10(float64(num))) + 1 + if(numDigits == 1){ + return num + } + + lastDigit := num % 10 + firstDigit := num / int(math.Pow10(numDigits - 1)) + + middleNumber := num % int(math.Pow10(numDigits - 1)) + middleNumber = middleNumber / 10 + + resultedNumber := middleNumber * 10 + firstDigit + resultedNumber = lastDigit * int(math.Pow10(numDigits - 1)) + resultedNumber + + return resultedNumber + +} + +func main(){ + fmt.Println("1234 -->", getSwappedNum(1234)) + fmt.Println("12345 -->", getSwappedNum(12345)) + fmt.Println("12 -->", getSwappedNum(12)) + fmt.Println("1 -->", getSwappedNum(1)) +} + +/** Output + +$ go run swap_digits_in_a_number.go +1234 --> 4231 +12345 --> 52341 +12 --> 21 +1 --> 1 + +*/ \ No newline at end of file diff --git a/star-patterns/README.md b/star-patterns/README.md new file mode 100644 index 0000000..c5d6614 --- /dev/null +++ b/star-patterns/README.md @@ -0,0 +1,319 @@ +# Golang Programming Exercises - Star Patterns + +## To be written +- Pattern 1 - Square + ``` + ***** + ***** + ***** + ***** + ***** + ``` +- Pattern 2 - Hollow Square + ``` + Enter number of rows: 5 + ***** + * * + * * + * * + ***** + ``` + +- Pattern 3 - Hollow square with diagonal + ``` + Enter number of rows: 5 + Enter number of columns: 10 + ********** + * * + * * + * * + ********** + ``` +- Pattern 4 - Rhombus + ``` + Output + Enter number of rows: 5 + ***** + ** ** + * * * + ** ** + ***** + ``` +- Pattern 5 - Mirrored Rohmbus + ``` + Enter rows: 5 + ***** + ***** + ***** + ***** + ***** + ``` +- Pattern 6 - Hollow mirrored Rohmbus + ``` + Enter the value of n: 5 + ***** + * * + * * + * * + ***** + ``` +- Pattern 7 - Right triangle + ``` + Enter the value of n: 5 + * + ** + *** + **** + ***** + ``` +- Pattern 8 - hollow right triangle + ``` + Enter number of rows: 5 + * + ** + * * + * * + ***** + ``` +- Pattern 9 - mirrored right triangle + ``` + Enter number of rows: 5 + * + ** + *** + **** + ***** + ``` +- Pattern 10 - hollow mirrored right triangle + ``` + Enter number of rows: 5 + * + ** + * * + * * + ***** + ``` +- Pattern 11 - reverse right triangle + ``` + Enter number of rows: 5 + ***** + **** + *** + ** + * + ``` +- Pattern 12 - hollow inverted right triangle + ``` + Enter number of rows: 5 + ***** + * * + * * + ** + * + ``` +- Pattern 13 - reversed mirrored right triangle + ``` + Enter number of rows: 5 + **** + *** + ** + * + ``` +- Pattern 14 - hollow mirrored inverted right triangle + ``` + Enter number of rows: 5 + ***** + * * + * * + ** + * + ``` +- Pattern 15 - print Equilateral triangle (Pyramid) + ``` + Enter number of rows: 5 + * + *** + ***** + ******* + ********* + ``` +- Pattern 16 - hollow pyramid (Equilateral triangle) + ``` + Enter number of rows: 5 + * + * * + * * + * * + ********* + ``` +- Pattern 17 - reverse Pyramid + ``` + Enter number of rows: 5 + ********* + ******* + ***** + *** + * + ``` +- Pattern 18 - hollow inverted +pyramid + ``` + Enter number of rows: 5 + ********* + * * + * * + * * + * + ``` +- Pattern 19 - half diamond + ``` + Enter number of columns: 5 + * + ** + *** + **** + ***** + **** + *** + ** + * + ``` +- Pattern 20 - mirrored half diamond + ``` + Enter number of columns : 5 + * + ** + *** + **** + ***** + **** + *** + ** + * + ``` +- Pattern 21 - print diamond + ``` + Enter N: 5 + * + *** + ***** + ******* + ********* + ******* + ***** + *** + * + ``` +- Pattern 22 - hollow diamond + ``` + Enter value of n : 5 + ********** + **** **** + *** *** + ** ** + * * + * * + ** ** + *** *** + **** **** + ********** + ``` +- Pattern 23 - right arrow + ``` + Enter value of n : 5 + ***** + **** + *** + ** + * + ** + *** + **** + ***** + ``` +- Pattern 24 - left arrow + ``` + Enter value of n : 5 + ***** + **** + *** + ** + * + ** + *** + **** + ***** + ``` +- Pattern 25 - Plus + ``` + Enter N: 5 + + + + + + + + + +++++++++ + + + + + + + + + ``` +- Pattern 26 - X + ``` + Enter N: 5 + * * + * * + * * + * * + * + * * + * * + * * + * * + ``` +- Pattern 27 - 8 + ``` + *** + * * + * * + * * + *** + * * + * * + * * + *** + ``` +- Pattern 28 - Heart + ``` + Enter value of n: 10 + ***** ***** + ******* ******* + ********* ********* + ******************* + ***************** + *************** + ************* + *********** + ********* + ******* + ***** + *** + * + ``` +- Pattern 29 - Heart with name + ``` + Enter your name: Codeforwin + Enter value of n: 10 + ***** ***** + ******* ******* + ********* ********* + *****Codeforwin**** + ***************** + *************** + ************* + *********** + ********* + ******* + ***** + *** + * + ```