#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
This is a simple C++ program that prints "Hello, World!" to the console.
#include <iostream>
int main() {
int n = 10;
int first = 0, second = 1;
std::cout << "Fibonacci Series: " << first << " " << second;
for (int i = 2; i < n; i++) {
int next = first + second;
std::cout << " " << next;
first = second;
second = next;
}
return 0;
}
This C++ program generates the Fibonacci series up to a given number.
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
print(factorial(5))
This Python function calculates the factorial of a number using recursion.
public class LinearSearch {
public static int search(int[] arr, int target) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int target = 3;
int index = search(arr, target);
System.out.println("Element found at index: " + index);
}
}
This Java program performs a linear search to find an element in an array.
#include <stdio.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
This C program implements the bubble sort algorithm to sort an array of integers.
function isPalindrome(str) {
const reversed = str.split('').reverse().join('');
return str === reversed;
}
console.log(isPalindrome('madam')); // true
console.log(isPalindrome('hello')); // false
This JavaScript function checks if a given string is a palindrome.
using System;
class Node {
public int data;
public Node left;
public Node right;
public Node(int item) {
data = item;
left = null;
right = null;
}
}
class BinarySearchTree {
public Node root;
public BinarySearchTree() {
root = null;
}
public void Insert(int key) {
root = InsertRec(root, key);
}
private Node InsertRec(Node root, int key) {
if (root == null) {
root = new Node(key);
return root;
}
if (key < root.data)
root.left = InsertRec(root.left, key);
else if (key > root.data)
root.right = InsertRec(root.right, key);
return root;
}
}
class Program {
static void Main(string[] args) {
BinarySearchTree bst = new BinarySearchTree();
bst.Insert(50);
bst.Insert(30);
bst.Insert(20);
bst.Insert(40);
bst.Insert(70);
bst.Insert(60);
bst.Insert(80);
}
}
This C# program demonstrates the implementation of a binary search tree and inserting elements into it.
def mergeSort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left = arr[:mid]
right = arr[mid:]
mergeSort(left)
mergeSort(right)
i = j = k = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
arr[k] = left[i]
i += 1
else:
arr[k] = right[j]
j += 1
k += 1
while i < len(left):
arr[k] = left[i]
i += 1
k += 1
while j < len(right):
arr[k] = right[j]
j += 1
k += 1
arr = [64, 34, 25, 12, 22, 11, 90]
mergeSort(arr)
print("Sorted array:", arr)
This Python program demonstrates the merge sort algorithm to sort an array of integers.