Skip to content

Commit 9baecde

Browse files
authored
Merge pull request #1 from srekan/basic
Basic
2 parents f9f9017 + e50c6bb commit 9baecde

15 files changed

+394
-0
lines changed

basic/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Golang Programming exercises: Basic
2+
- [Sum of two numbers](sum_of_two_numbers.go)
3+
- [Arithmetic operations](arithmetic_operations.go)
4+
- [Input from user](input_from_user.go)
5+
- [Accept length and breadth of a rectangle. Then find perimeter and area of a rectangle](rectangle_area_and_perimeter.go)
6+
- [Accept radius of a circle. Then find diameter and circumference of a circle](circle_diameter_and_circumference.go)
7+
- [Accept temperature in Celsius and convert it into Fahrenheit.](celsius_to_fahrenheit.go)
8+
- [Program to convert days into years, weeks and days](days_to_years.go)
9+
- [Program to enter two angles of a triangle and find the third angle](third_angle_of_triangle.go)
10+
- [Program to enter base and height of a triangle and find its area](area_of_triangle.go)
11+
- [Program to calculate area of an equilateral triangle](area_of_equilateral_triangle.go)
12+
- [Program to find power of any number x ^ y](power_of_a_number.go)
13+
- [Program to enter marks of five subjects and calculate total, average and percentage](student_marks.go)
14+
- [Program to enter P, N, R and calculate Simple Interest](simple_interest.go)
15+
- [Program to enter P, N, R and calculate Compound Interest](compound_interest.go)

basic/area_of_equilateral_triangle.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
import "fmt"
3+
import "math"
4+
func main(){
5+
fmt.Println("-----Area of equilateral triangle-----")
6+
var length float64
7+
fmt.Print("Enter length of a side (in meters): ")
8+
fmt.Scan(&length)
9+
10+
area := (math.Sqrt(3) / 4) * length * length
11+
fmt.Printf("Area = %.2f square meters\n", area)
12+
}
13+
14+
/** Output
15+
16+
$ go run area_of_equilateral_triangle.go
17+
-----Area of equilateral triangle-----
18+
Enter length of a side (in meters): 5
19+
Area = 10.83 square meters
20+
21+
*/

basic/area_of_triangle.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
import "fmt"
3+
func main(){
4+
fmt.Println("----Finding area of a triangle-----")
5+
var height float32
6+
var breadth float32
7+
8+
fmt.Print("Enter height: ")
9+
fmt.Scan(&height)
10+
11+
fmt.Print("Enter base: ")
12+
fmt.Scan(&breadth)
13+
14+
area := height * breadth
15+
fmt.Println("Area =", area)
16+
}
17+
18+
/** Output
19+
20+
$ go run area_of_triangle.go
21+
----Finding area of a triangle-----
22+
Enter height: 5.6
23+
Enter base: 6.7
24+
Area = 37.519997
25+
26+
*/

basic/arithmetic_operations.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
import "fmt"
3+
func main(){
4+
fmt.Println("-----Arithmetic Operations-----")
5+
var a = 25
6+
var b = 2
7+
fmt.Printf("%d + %d = %d\n", a, b, a + b)
8+
fmt.Printf("%d - %d = %d\n", a, b, a - b)
9+
fmt.Printf("%d * %d = %d\n", a, b, a * b)
10+
fmt.Printf("%d / %d = %.2f\n", a, b, (float64(a) / float64(b)))
11+
}
12+
13+
/** Output
14+
15+
$ go run arithmetic_operations.go
16+
-----Arithmetic Operations-----
17+
25 + 2 = 27
18+
25 - 2 = 23
19+
25 * 2 = 50
20+
25 / 2 = 12.50
21+
25 % 2 = 1
22+
fmt.Printf("%d %% %d = %d\n", a, b, a % b)
23+
}
24+
25+
*/

basic/celsius_to_fahrenheit.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
import "fmt"
3+
func main(){
4+
fmt.Println("-----Convert Celcius to Fahrenheit-----")
5+
var celcius float64
6+
fmt.Print("Enter temperature in celcius degrees: ")
7+
fmt.Scan(&celcius)
8+
fahrenheit := (celcius * (9/5)) + 32
9+
fmt.Println("Temperature in Fahrenheit degrees= ", fahrenheit)
10+
}
11+
12+
/** Output
13+
14+
$ go run celsius_to_fahrenheit.go
15+
-----Convert Celcius to Fahrenheit-----
16+
Enter temperature in celcius degrees: 30
17+
Temperature in Fahrenheit degrees= 86
18+
19+
*/
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
import "fmt"
3+
func main(){
4+
fmt.Println("-----Find diameter and circumference of a circle-----")
5+
const Pi = 3.14
6+
var radius float64
7+
8+
fmt.Print("Enter radius of circle (in meters): ")
9+
fmt.Scan(&radius)
10+
11+
circumference := 2 * Pi * radius
12+
diameter := 2 * radius
13+
14+
fmt.Println("Radius :", radius)
15+
fmt.Println("Circumference:", circumference)
16+
fmt.Println("Diameter :", diameter)
17+
}
18+
19+
/** Output -
20+
21+
$ go run circle_diameter_and_circumference.go
22+
-----Find diameter and circumference of a circle-----
23+
Enter radius of circle (in meters): 5.5
24+
Radius : 5.5
25+
Circumference: 34.54
26+
Diameter : 11
27+
28+
*/

basic/compound_interest.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
CI = P * (1 + (R / 100))^T
3+
*/
4+
package main
5+
import (
6+
"math"
7+
"fmt"
8+
)
9+
func main(){
10+
fmt.Println("-----Compound Interest-----")
11+
var principle float64
12+
var numberOFYears float64
13+
var rateOfInterest float64
14+
15+
fmt.Print("Enter principle: ")
16+
fmt.Scan(&principle)
17+
fmt.Print("Enter number of years: ")
18+
fmt.Scan(&numberOFYears)
19+
fmt.Print("Enter rate of interest: ")
20+
fmt.Scan(&rateOfInterest)
21+
22+
compoundInterest := principle * math.Pow((1 + rateOfInterest / 100), numberOFYears)
23+
24+
fmt.Printf("Compound Interest = %.2f\n", compoundInterest)
25+
}
26+
27+
/** Output
28+
29+
$ go run compound_interest.go
30+
-----Compound Interest-----
31+
Enter principle: 2000
32+
Enter number of years: 5
33+
Enter rate of interest: 10
34+
Compound Interest = 3221.02
35+
36+
*/

basic/days_to_years.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
import "fmt"
3+
func main(){
4+
fmt.Println("-----Convert Days to Years and Months-----")
5+
var days int
6+
fmt.Print("Enter number of days: ")
7+
fmt.Scan(&days)
8+
9+
years := days / 365
10+
weeks := (days % 365) / 7
11+
days = days - ((years * 365) + (weeks * 7))
12+
13+
fmt.Printf("Years: %d\n", years);
14+
fmt.Printf("Weeks: %d\n", weeks);
15+
fmt.Printf("Days : %d\n", days);
16+
}
17+
18+
/** Output
19+
$ go run days_to_years.go
20+
-----Convert Days to Years and Months-----
21+
Enter number of days: 417
22+
Years: 1
23+
Weeks: 7
24+
Days : 3
25+
26+
*/

basic/input_from_user.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
import "fmt"
3+
func main(){
4+
fmt.Println("-----Input from User-----")
5+
var myVal int
6+
fmt.Print("Enter a number: ")
7+
fmt.Scan(&myVal)
8+
fmt.Printf("Thank you! You entered %d!\n", myVal)
9+
}
10+
11+
/** Output
12+
13+
$ go run input_from_user.go
14+
-----Input from User-----
15+
Enter a number: 234
16+
Thank you! You entered 234!
17+
18+
*/

basic/power_of_a_number.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
import "fmt"
3+
import "math"
4+
func main(){
5+
fmt.Println("-----Power of a number: x^y-----")
6+
var base float64
7+
var exponent float64
8+
9+
fmt.Print("Enter base: ")
10+
fmt.Scan(&base)
11+
fmt.Print("Enter exponent: ")
12+
fmt.Scan(&exponent)
13+
14+
power := math.Pow(base, exponent)
15+
fmt.Printf("%.0f ^ %.0f = %.0f\n", base, exponent, power)
16+
}
17+
18+
/** Output
19+
20+
$ go run power_of_a_number.go
21+
-----Power of a number: x^y-----
22+
Enter base: 2
23+
Enter exponent: 3
24+
2 ^ 3 = 8
25+
26+
*/

basic/rectangle_area_and_perimeter.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
import "fmt"
3+
func main(){
4+
fmt.Println("-----Find Reactangle Area and Perimeter-----")
5+
var length float64
6+
var breadth float64
7+
8+
fmt.Print("Enter length of rectangle (in meters): ")
9+
fmt.Scan(&length)
10+
fmt.Print("Enter breadth of rectangle (in meters): ")
11+
fmt.Scan(&breadth)
12+
13+
area := length * breadth
14+
perimeter := 2 * (length + breadth)
15+
16+
fmt.Println("Length :", length, "meters")
17+
fmt.Println("Breadth :", breadth, "meters")
18+
fmt.Println("Area :", area, "square meters")
19+
fmt.Println("Perimeter:", perimeter, "meters")
20+
}
21+
22+
/** Output
23+
24+
$ go run rectangle_area_and_perimeter.go
25+
-----Find Reactangle Area and Perimeter-----
26+
Enter length of rectangle (in meters): 2.5
27+
Enter breadth of rectangle (in meters): 5.5
28+
Length : 2.5 meters
29+
Breadth : 5.5 meters
30+
Area : 13.75 square meters
31+
Perimeter: 16 meters
32+
33+
*/

basic/simple_interest.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
import "fmt"
3+
func main(){
4+
fmt.Println("-----Simple Interest-----")
5+
var principle float64
6+
var numberOFYears float64
7+
var rateOfInterest float64
8+
9+
fmt.Print("Enter principle: ")
10+
fmt.Scan(&principle)
11+
fmt.Print("Enter number of years: ")
12+
fmt.Scan(&numberOFYears)
13+
fmt.Print("Enter rate of interest: ")
14+
fmt.Scan(&rateOfInterest)
15+
16+
simpleInterest := principle * numberOFYears * rateOfInterest / 100
17+
18+
fmt.Printf("Simple Interest = %.2f\n", simpleInterest)
19+
}
20+
21+
/** Output
22+
23+
$ go run simple_interest.go
24+
-----Simple Interest-----
25+
Enter principle: 2000
26+
Enter number of years: 5
27+
Enter rate of interest: 10
28+
Simple Interest = 1000.00
29+
30+
*/

basic/student_marks.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
import "fmt"
3+
func main(){
4+
fmt.Println("-----Student Marks-----")
5+
var marks1 int
6+
var marks2 int
7+
var marks3 int
8+
var marks4 int
9+
var marks5 int
10+
const maxMarksInASubject int = 100
11+
12+
fmt.Print("Enter Marks in subject 1 of 5: ")
13+
fmt.Scan(&marks1)
14+
fmt.Print("Enter Marks in subject 2 of 5: ")
15+
fmt.Scan(&marks2)
16+
fmt.Print("Enter Marks in subject 3 of 5: ")
17+
fmt.Scan(&marks3)
18+
fmt.Print("Enter Marks in subject 4 of 5: ")
19+
fmt.Scan(&marks4)
20+
fmt.Print("Enter Marks in subject 5 of 5: ")
21+
fmt.Scan(&marks5)
22+
23+
total := marks1 + marks2 + marks3 + marks4 + marks5
24+
average := float64(total) / 5
25+
percentage := 100 * float64(total) / (float64(maxMarksInASubject) * 5)
26+
27+
fmt.Println("Total :", total)
28+
fmt.Printf("Average : %.2f\n", average)
29+
fmt.Printf("Percentage: %.2f\n", percentage)
30+
}
31+
32+
/** Output
33+
34+
$ go run student_marks.go
35+
-----Student Marks-----
36+
Enter Marks in subject 1 of 5: 95
37+
Enter Marks in subject 2 of 5: 97
38+
Enter Marks in subject 3 of 5: 98
39+
Enter Marks in subject 4 of 5: 96
40+
Enter Marks in subject 5 of 5: 93
41+
Total : 479
42+
Average : 95.80
43+
Percentage: 95.80
44+
45+
*/

basic/sum_of_two_numbers.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
import "fmt"
3+
func main(){
4+
fmt.Println("----Sum of two numbers-----")
5+
var a = 10
6+
var b = 20
7+
var sum = a + b
8+
fmt.Println("a = ", a)
9+
fmt.Println("b = ", b)
10+
fmt.Println(a, "+", b, "=", sum)
11+
}
12+
13+
/** Outputs
14+
15+
$ go run sum_of_two_numbers.go
16+
----Sum of two numbers-----
17+
a = 10
18+
b = 20
19+
10 + 20 = 30
20+
21+
*/

0 commit comments

Comments
 (0)