Code Examples in Multiple Programming Languages
Date: 12.09.2024
In this blog post, we will explore various programming languages through a series of code examples. Each example will demonstrate a simple task, such as printing "Hello, World!" or calculating the sum of two numbers. This will give you a glimpse into the syntax and style of different languages.
Python
Python is known for its readability and simplicity. Here’s how to print "Hello, World!" in Python:
print("Hello, World!")
Sum of Two Numbers
def sum(a, b):
return a + b
result = sum(5, 3)
print("The sum is:", result)
JavaScript
JavaScript is widely used for web development. Here’s the same "Hello, World!" example:
console.log("Hello, World!");
Sum of Two Numbers
function sum(a, b) {
return a + b;
}
let result = sum(5, 3);
console.log("The sum is: " + result);
Java
Java is a statically typed language that is popular for enterprise applications. Here’s how to print "Hello, World!" in Java:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Sum of Two Numbers
public class Sum {
public static int sum(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = sum(5, 3);
System.out.println("The sum is: " + result);
}
}
C++
C++ is an extension of C that includes object-oriented features. Here’s the "Hello, World!" example:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
Sum of Two Numbers
#include <iostream>
using namespace std;
int sum(int a, int b) {
return a + b;
}
int main() {
int result = sum(5, 3);
cout << "The sum is: " << result << endl;
return 0;
}
Ruby
Ruby is known for its elegant syntax. Here’s how to print "Hello, World!" in Ruby:
puts "Hello, World!"
Sum of Two Numbers
def sum(a, b)
a + b
end
result = sum(5, 3)
puts "The sum is: #{result}"
Go
Go is a statically typed language designed for simplicity and efficiency. Here’s the "Hello, World!" example:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Sum of Two Numbers
package main
import "fmt"
func sum(a int, b int) int {
return a + b
}
func main() {
result := sum(5, 3)
fmt.Println("The sum is:", result)
}
Conclusion
In this post, we explored how to perform simple tasks in various programming languages. Each language has its own syntax and style, but the fundamental concepts remain the same. Whether you are a beginner or an experienced developer, understanding multiple languages can enhance your programming skills and open up new opportunities.
Feel free to try out these examples in your own development environment! Happy coding!