New Age Programming languages
New Age Programming languages
MANGALAM UNIVERSITY
Practical File
New Age Programming Languages
COURSE CODE: ENSP465
2
17 Program 17: Implement a program using loops and lists 34
to calculate the sum of elements.
18 Program 18: Implement a program design a `Student` 35-36
class with properties and display student details.
3
Program 1: Basic Calculator
Objective and Problem Statement: Use functions for arithmetic
operations. Implement a Go program to perform basic arithmetic
operations.
package main
import (
"fmt"
)
4
}
func main() {
var a, b float64
var operator string
5
fmt.Println("Invalid operator")
}
}
Output:
6
Program 2: Check Prime Number
Objective and Problem Statement: Use control structures to check if a
given number is prime.
package main
import (
"fmt"
"math"
)
func main() {
7
var number int
Output:
8
Program 3: String Reversal
Objective and Problem Statement: Use arrays and strings to reverse a
string entered by the user.
package main
import (
"fmt"
)
func main() {
var input string
9
reversed := reverseString(input)
Output:
10
Program 4: Manage Employee Data
Objective and Problem Statement: Use structs to store and display
employee details.
package main
import (
"fmt"
)
func main() {
emp6 := Employee{
firstName: "Shyam",
lastName: "Shyam",
age: 22,
salary: 20000,
}
fmt.Println("First Name:", emp6.firstName)
fmt.Println("Last Name:", emp6.lastName)
fmt.Println("Age:", emp6.age)
fmt.Printf("Salary: $%d\n", emp6.salary)
emp6.salary = 30000
fmt.Printf("New Salary: $%d", emp6.salary)
}
11
Output:
12
Program 5: Simple File Reader
Objective and Problem Statement: Read and display content from a
file.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
// Ask the user for the file name
fmt.Print("Enter the file name to read: ")
var fileName string
fmt.Scanln(&fileName)
13
// Check for scanner errors
if err := scanner.Err(); err != nil {
fmt.Printf("Error reading file: %v\n", err)
}
}
Output:
14
Program 6: Simple Interest Calculator
Objective and Problem Statement: Use basic functions to calculate
simple interest.
package main
import (
"fmt"
)
func main() {
var principal, rate, time float64
15
fmt.Printf("The simple interest is: %.2f\n", interest)
}
Output:
16
Program 7: Find Maximum in List
Objective and Problem Statement: Use list operations to find the
maximum number in a list.
package main
import (
"fmt"
)
max := numbers[0]
for _, num := range numbers {
if num > max {
max = num
}
}
return max
}
func main() {
var n int
fmt.Println("Enter the number of elements in the list:")
fmt.Scanln(&n)
17
// Take input for the list
fmt.Println("Enter the numbers:")
for i := 0; i < n; i++ {
fmt.Scanln(&numbers[i])
}
Output:
18
Program 8: Factorial Using Recursion
Objective and Problem Statement: Create a recursive function to
calculate the factorial of a number.
package main
import (
"fmt"
)
func main() {
var num int
if num < 0 {
fmt.Println("Factorial is not defined for negative numbers.")
} else {
// Calculate factorial using recursion
result := factorial(num)
fmt.Printf("The factorial of %d is: %d\n", num, result)
19
}
}
Output:
20
Program 9: Pattern Matching Example
Objective and Problem Statement: Determine if a given input is
positive, negative, or zero using pattern matching
package main
import (
"fmt"
)
func main() {
var num int
Output:
21
Program 10: Asynchronous Greeting
Objective and Problem Statement: Display a greeting message after a
delay using asynchronous programming.
package main
import (
"fmt"
"time"
)
func main() {
// Launch the displayGreeting function asynchronously using a goroutine
go displayGreeting()
Output:
22
Program 11: Square of Numbers in a List
Objective and Problem Statement: Use sequences and functions to
compute squares.
package main
import (
"fmt"
)
func main() {
// Define a list of numbers
numbers := []int{1, 2, 3, 4, 5, 6}
// Compute and display the square of each number using the square function
fmt.Println("Squares of numbers in the list:")
for _, num := range numbers {
fmt.Printf("The square of %d is %d\n", num, square(num))
}
}
Output:
23
24
Program 12: Simple Map Example
Objective and Problem Statement: Store and display information
using a map.
package main
import (
"fmt"
)
func main() {
// Creating a map to store employee details
employees := make(map[int]map[string]string)
25
for key, value := range details {
fmt.Printf("%s: %s\n", key, value)
}
fmt.Println() // Add a blank line for separation
}
}
Output:
26
Program 13: Even Numbers Filter
Objective and Problem Statement: Filter out even numbers from a
given list.
package main
import (
"fmt"
)
// Function to filter out even numbers and return only odd numbers
func filterEvenNumbers(numbers []int) []int {
var oddNumbers []int
for _, num := range numbers {
if num%2 != 0 {
oddNumbers = append(oddNumbers, num)
}
}
return oddNumbers
}
func main() {
// Define a list of numbers
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
27
}
}
Output:
28
Program 14: Handle Division by Zero
Objective and Problem Statement: Handle division by zero using
exception handling.
package main
import (
"fmt"
"errors"
)
// Function to divide two numbers with error handling for division by zero
func divide(a, b float64) (float64, error) {
if b == 0 {
// Return an error if the divisor is zero
return 0, errors.New("division by zero is not allowed")
}
return a / b, nil
}
func main() {
var num1, num2 float64
29
if err != nil {
// If there was an error (i.e., division by zero), print the error
fmt.Println("Error:", err)
} else {
// Otherwise, print the result
fmt.Printf("The result of %.2f / %.2f is %.2f\n", num1, num2, result)
}
}
Output:
30
Program 15: Combine Two Lists
Objective and Problem Statement: Merge two lists into one and
display the result
package main
import (
"fmt"
)
func main() {
// Define two lists (slices)
list1 := []int{1, 2, 3, 4}
list2 := []int{5, 6, 7, 8}
Output:
31
Program 16: Check Palindrome String
Objective and Problem Statement: c
package main
import (
"fmt"
"strings"
)
func main() {
var input string
32
fmt.Println("Enter a string to check if it's a palindrome:")
fmt.Scanln(&input)
Output:
33
Program 17: Sum of List Elements
Objective and Problem Statement: Use loops and lists to calculate the
sum of elements.
package main
import (
"fmt"
)
func main() {
// Define a list of numbers
numbers := []int{1, 2, 3, 4, 5, 6}
Output:
34
Program 18: Create and Use Classes
Objective and Problem Statement: Design a `Student` class with
properties and display student details.
package main
import "fmt"
func main() {
// Creating an instance of Student
student1 := Student{
Name: "Alice",
Age: 22,
Grade: "A",
RollNo: 22,
35
}
Output:
36
Program 19: Safe Call Example
Objective and Problem Statement: Demonstrate the use of safe calls
and the Elvis operator.
package main
import "fmt"
func main() {
// Create a student with a grade
grade := "A"
student1 := Student{
Name: "Bob",
Grade: &grade,
}
37
student2 := Student{
Name: "Ram",
Grade: nil,
}
Output:
38
Program 20: Simple Coroutine Example
Objective and Problem Statement: Display a message after a delay
using coroutines.
package main
import (
"fmt"
"time"
)
func main() {
// Create a channel to signal when the task is done
done := make(chan bool)
39
fmt.Println("Main function ends.")
}
Output:
40