Added new Examples to use as templates

This commit is contained in:
Lia Brüggemann 2023-09-27 21:17:12 +02:00
parent bcc4a48e08
commit e6f4dc5cc8
4 changed files with 263 additions and 0 deletions

83
Example.cs Normal file
View file

@ -0,0 +1,83 @@
using System;
class Example
{
static void Main()
{
// Example of printing to the console
Console.WriteLine("Hello, World!");
// Basic math operations
int num1 = 10;
int num2 = 5;
// Addition
int sum = Add(num1, num2);
Console.WriteLine($"Sum: {sum}");
// Subtraction
int difference = Subtract(num1, num2);
Console.WriteLine($"Difference: {difference}");
// Multiplication
int product = Multiply(num1, num2);
Console.WriteLine($"Product: {product}");
// Division with error handling
ResultAndError resultAndError = Divide(num1, num2);
if (resultAndError.Error != null)
{
Console.WriteLine($"Error: {resultAndError.Error}");
}
else
{
Console.WriteLine($"Quotient: {resultAndError.Result}");
}
// Example of using a loop
for (int i = 0; i < 5; i++)
{
Console.WriteLine($"Loop iteration {i}");
}
}
// Function to add two integers
static int Add(int a, int b)
{
return a + b;
}
// Function to subtract two integers
static int Subtract(int a, int b)
{
return a - b;
}
// Function to multiply two integers
static int Multiply(int a, int b)
{
return a * b;
}
// Function to divide two integers, returns an error for division by zero
static ResultAndError Divide(int a, int b)
{
ResultAndError resultAndError = new ResultAndError();
if (b == 0)
{
resultAndError.Error = "Division by zero";
}
else
{
resultAndError.Result = (double)a / b;
}
return resultAndError;
}
// Helper class to store both a result and an error
class ResultAndError
{
public double Result { get; set; }
public string Error { get; set; }
}
}

62
Example.go Normal file
View file

@ -0,0 +1,62 @@
package main
import (
"fmt"
)
func test() {
// Example of printing to the console
fmt.Println("Hello, World!")
// Basic math operations
num1 := 10
num2 := 5
// Addition
sum := add(num1, num2)
fmt.Printf("Sum: %d\n", sum)
// Subtraction
difference := subtract(num1, num2)
fmt.Printf("Difference: %d\n", difference)
// Multiplication
product := multiply(num1, num2)
fmt.Printf("Product: %d\n", product)
// Division with error handling
quotient, err := divide(num1, num2)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Printf("Quotient: %f\n", quotient)
}
// Example of using a loop
for i := 0; i < 5; i++ {
fmt.Printf("Loop iteration %d\n", i)
}
}
// Function to add two integers
func add(a, b int) int {
return a + b
}
// Function to subtract two integers
func subtract(a, b int) int {
return a - b
}
// Function to multiply two integers
func multiply(a, b int) int {
return a * b
}
// Function to divide two integers, returns an error for division by zero
func divide(a, b int) (float64, error) {
if b == 0 {
return 0, fmt.Errorf("division by zero")
}
return float64(a) / float64(b), nil
}

67
Example.java Normal file
View file

@ -0,0 +1,67 @@
public class Example {
public static void main(String[] args) {
// Example of printing to the console
System.out.println("Hello, World!");
// Basic math operations
int num1 = 10;
int num2 = 5;
// Addition
int sum = add(num1, num2);
System.out.println("Sum: " + sum);
// Subtraction
int difference = subtract(num1, num2);
System.out.println("Difference: " + difference);
// Multiplication
int product = multiply(num1, num2);
System.out.println("Product: " + product);
// Division with error handling
ResultAndError resultAndError = divide(num1, num2);
if (resultAndError.error != null) {
System.out.println("Error: " + resultAndError.error);
} else {
System.out.println("Quotient: " + resultAndError.result);
}
// Example of using a loop
for (int i = 0; i < 5; i++) {
System.out.println("Loop iteration " + i);
}
}
// Function to add two integers
public static int add(int a, int b) {
return a + b;
}
// Function to subtract two integers
public static int subtract(int a, int b) {
return a - b;
}
// Function to multiply two integers
public static int multiply(int a, int b) {
return a * b;
}
// Function to divide two integers, returns an error for division by zero
public static ResultAndError divide(int a, int b) {
ResultAndError resultAndError = new ResultAndError();
if (b == 0) {
resultAndError.error = "Division by zero";
} else {
resultAndError.result = (double) a / b;
}
return resultAndError;
}
// Helper class to store both a result and an error
public static class ResultAndError {
public Double result;
public String error;
}
}

51
Example.py Normal file
View file

@ -0,0 +1,51 @@
def main():
# Example of printing to the console
print("Hello, World!")
# Basic math operations
num1 = 10
num2 = 5
# Addition
sum_result = add(num1, num2)
print(f"Sum: {sum_result}")
# Subtraction
difference = subtract(num1, num2)
print(f"Difference: {difference}")
# Multiplication
product = multiply(num1, num2)
print(f"Product: {product}")
# Division with error handling
quotient, error = divide(num1, num2)
if error is not None:
print(f"Error: {error}")
else:
print(f"Quotient: {quotient}")
# Example of using a loop
for i in range(5):
print(f"Loop iteration {i}")
# Function to add two numbers
def add(a, b):
return a + b
# Function to subtract two numbers
def subtract(a, b):
return a - b
# Function to multiply two numbers
def multiply(a, b):
return a * b
# Function to divide two numbers, returns an error for division by zero
def divide(a, b):
if b == 0:
return None, "Division by zero"
return a / b, None
if __name__ == "__main__":
main()