Reverse a String

Explanation: This program reverses a given string.

Code:

function reverseString(str) { return str.split('').reverse().join(''); } const str = prompt("Enter a string:"); console.log("Reversed string: " + reverseString(str));



output:
Enter a string: hello
Reversed string: olleh


Check Prime Number

Explanation: This program checks if a given number is prime.

Code:

function isPrime(num) { if (num <= 1) return false; for (let i = 2; i <= Math.sqrt(num); i++) { if (num % i === 0) return false; } return true; } const num = parseInt(prompt("Enter a number:"), 10); if (isPrime(num)) { console.log(num + " is a prime number."); } else { console.log(num + " is not a prime number."); }


output:
Enter a number: 29
29 is a prime number.


Sum of Array Elements

Explanation: This program calculates the sum of elements in an array.

Code:

function sumArray(arr) { return arr.reduce((a, b) => a + b, 0); } const n = parseInt(prompt("Enter the number of elements:"), 10); let arr = []; for (let i = 0; i < n; i++) { arr.push(parseInt(prompt(`Enter element ${i + 1}:`), 10)); } console.log("The sum of the array elements is: " + sumArray(arr));


output:
Enter the number of elements: 5
Enter element 1: 1
Enter element 2: 2
Enter element 3: 3
Enter element 4: 4
Enter element 5: 5
The sum of the array elements is: 15


Factorial of a Number

Explanation: This program calculates the factorial of a given number.

Code:

function factorial(num) { if (num === 0 || num === 1) return 1; return num * factorial(num - 1); } const num = parseInt(prompt("Enter a number:"), 10); console.log("The factorial of " + num + " is: " + factorial(num));


output:
Enter a number: 5
The factorial of 5 is: 120


Check Palindrome Number

Explanation: This program checks if a given number is a palindrome.

Code:

function isPalindrome(num) { const original = num.toString(); const reversed = original.split('').reverse().join(''); return original === reversed; } const num = parseInt(prompt("Enter a number:"), 10); if (isPalindrome(num)) { console.log(num + " is a palindrome number."); } else { console.log(num + " is not a palindrome number."); }


output:
Enter a number: 121
121 is a palindrome number.


Fibonacci Series

Explanation: This program prints the Fibonacci series up to a given number of terms.

Code:

function fibonacci(n) { let t1 = 0, t2 = 1, nextTerm; let series = []; for (let i = 1; i <= n; ++i) { series.push(t1); nextTerm = t1 + t2; t1 = t2; t2 = nextTerm; } return series.join(', '); } const n = parseInt(prompt("Enter the number of terms:"), 10); console.log("Fibonacci Series: " + fibonacci(n));


output:
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34



Binary Search

Explanation: This program implements binary search on a sorted array.

Code:

function binarySearch(arr, key) { let low = 0, high = arr.length - 1; while (low <= high) { const mid = Math.floor((low + high) / 2); if (arr[mid] === key) return mid; if (arr[mid] < key) low = mid + 1; else high = mid - 1; } return -1; } const n = parseInt(prompt("Enter the number of elements:"), 10); let arr = []; for (let i = 0; i < n; i++) { arr.push(parseInt(prompt(`Enter element ${i + 1}:`), 10)); } arr.sort((a, b) => a - b); const key = parseInt(prompt("Enter the key to search:"), 10); const result = binarySearch(arr, key); if (result !== -1) { console.log("Element found at index: " + result); } else { console.log("Element not found."); }


output:
Enter the number of elements: 5
Enter element 1: 1
Enter element 2: 3
Enter element 3: 5
Enter element 4: 7
Enter element 5: 9
Enter the key to search: 7
Element found at index: 3


Insertion Sort

Explanation: This program implements insertion sort on an array.

Code:

function insertionSort(arr) { for (let i = 1; i < arr.length; i++) { let key = arr[i]; let j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } } const n = parseInt(prompt("Enter the number of elements:"), 10); let arr = []; for (let i = 0; i < n; i++) { arr.push(parseInt(prompt(`Enter element ${i + 1}:`), 10)); } insertionSort(arr); console.log("Sorted array: " + arr.join(' '));


output:
Enter the number of elements: 5
Enter element 1: 5
Enter element 2: 2
Enter element 3: 9
Enter element 4: 1
Enter element 5: 5
Sorted array: 1 2 5 5 9


Check Palindrome String

Explanation: This program checks if a given string is a palindrome.

Code:

function isPalindrome(str) { const reversed = str.split('').reverse().join(''); return str === reversed; } const str = prompt("Enter a string:"); if (isPalindrome(str)) { console.log(str + " is a palindrome string."); } else { console.log(str + " is not a palindrome string."); }


output:
Enter a string: madam
madam is a palindrome string.




 


 


 


 


 


 


 


 


 


 


 


Reverse a String

Explanation: This program reverses a given string.

Code:

#include <stdio.h> #include <string.h> int main() { char str[100]; printf("Enter a string: "); gets(str); strrev(str); printf("Reversed string: %s\n", str); return 0; }


output:
Enter a string: hello Reversed string: olleh



Check Prime Number

Explanation: This program checks if a given number is prime.

Code:


#include <stdio.h> #include <math.h> int isPrime(int num) { if (num <= 1) return 0; for (int i = 2; i <= sqrt(num); i++) { if (num % i == 0) return 0; } return 1; } int main() { int num; printf("Enter a number: "); scanf("%d", &num); if (isPrime(num)) { printf("%d is a prime number.\n", num); } else { printf("%d is not a prime number.\n", num); } return 0; }


output:
Enter a number: 29
29 is a prime number.


Sum of Array Elements

Explanation: This program calculates the sum of elements in an array.

Code:


#include <stdio.h> int main() { int n; printf("Enter the number of elements: "); scanf("%d", &n); int arr[n], sum = 0; printf("Enter the elements of the array:\n"); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); sum += arr[i]; } printf("The sum of the array elements is: %d\n", sum); return 0; }


output:
Enter the number of elements: 5 Enter the elements of the array: 1 2 3 4 5 The sum of the array elements is: 15


Factorial of a Number

Explanation: This program calculates the factorial of a given number.

Code:

#include <stdio.h> int factorial(int num) { if (num == 0 || num == 1) return 1; return num * factorial(num - 1); } int main() { int num; printf("Enter a number: "); scanf("%d", &num); printf("The factorial of %d is: %d\n", num, factorial(num)); return 0; }


output:
Enter a number: 5
The factorial of 5 is: 120


Check Palindrome Number

Explanation: This program checks if a given number is a palindrome.

Code:


#include <stdio.h> void insertionSort(int arr[], int n) { for (int i = 1; i < n; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } } int main() { int n; printf("Enter the number of elements: "); scanf("%d", &n); int arr[n]; printf("Enter the elements of the array:\n"); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } insertionSort(arr, n); printf("Sorted array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; }


output:
Enter the number of elements: 5 Enter the elements of the array: 5 2 9 1 5 Sorted array: 1 2 5 5 9


Binary Search

Explanation: This program implements binary search on a sorted array.

Code:


#include <stdio.h> int binarySearch(int arr[], int size, int key) { int low = 0, high = size - 1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == key) return mid; if (arr[mid] < key) low = mid + 1; else high = mid - 1; } return -1; } int main() { int n, key; printf("Enter the number of elements: "); scanf("%d", &n); int arr[n]; printf("Enter the sorted elements of the array:\n"); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } printf("Enter the key to search: "); scanf("%d", &key); int result = binarySearch(arr, n, key); if (result != -1) { printf("Element found at index: %d\n", result); } else { printf("Element not found.\n"); } return 0; }


output:
Enter the number of elements: 5 Enter the sorted elements of the array: 1 3 5 7 9 Enter the key to search: 7 Element found at index: 3