Top 10+ C Language Interview Programs for 2024


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





0 Comments:

Post a Comment