C Language Coding Questions and Answers
-
Write a C program to swap two numbers without using a temporary variable.
Use the following code to swap two numbers:
Use the following code:
#include <stdio.h> int main() { int a = 5, b = 10; a = a + b; b = a - b; a = a - b; printf("After swapping: a = %d, b = %d", a, b); return 0;
-
Write a C program to find the factorial of a number using recursion.
Use the following code:
#include <stdio.h> int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } int main() { int num = 5; printf("Factorial of %d is %d", num, factorial(num)); return 0; }
-
Write a C program to reverse a string.
Use the following code:
#include <stdio.h> #include <string.h> void reverse(char *str) { int n = strlen(str); for (int i = 0; i < n / 2; i++) { char temp = str[i]; str[i] = str[n - i - 1]; str[n - i - 1] = temp; } } int main() { char str[] = "Hello"; reverse(str); printf("Reversed string: %s", str); return 0; }
-
Write a C program to check if a number is prime.
Use the following code:
#include <stdio.h> int isPrime(int n) { if (n <= 1) return 0; for (int i = 2; i <= n / 2; i++) { if (n % i == 0) return 0; } return 1; } int main() { int num = 29; if (isPrime(num)) printf("%d is a prime number", num); else printf("%d is not a prime number", num); return 0; }
-
Write a C program to find the GCD of two numbers.
Use the following code:
#include <stdio.h> int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int a = 56, b = 98; printf("GCD of %d and %d is %d", a, b, gcd(a, b)); return 0; }
-
Write a C program to implement binary search.
Use the following code:
#include <stdio.h> int binarySearch(int arr[], int n, int x) { int left = 0, right = n - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == x) return mid; if (arr[mid] < x) left = mid + 1; else right = mid - 1; } return -1; } int main() { int arr[] = {2, 3, 4, 10, 40}; int n = sizeof(arr) / sizeof(arr[0]); int x = 10; int result = binarySearch(arr, n, x); if (result != -1) printf("Element is present at index %d", result); else printf("Element is not present in array"); return 0; }
-
Write a C program to implement bubble sort.
Use the following code:
#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; }
-
Write a C program to implement selection sort.
Use the following code:
#include <stdio.h> void selectionSort(int arr[], int n) { for (int i = 0; i < n-1; i++) { int min_idx = i; for (int j = i+1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; int temp = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = temp; } } int main() { int arr[] = {64, 25, 12, 22, 11}; int n = sizeof(arr)/sizeof(arr[0]); selectionSort(arr, n); printf("Sorted array: "); for (int i=0; i < n; i++) printf("%d ", arr[i]); return 0; }
-
Write a C program to implement insertion sort.
Use the following 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 = j - 1; } arr[j + 1] = key; } } int main() { int arr[] = {12, 11, 13, 5, 6}; int n = sizeof(arr)/sizeof(arr[0]); insertionSort(arr, n); printf("Sorted array: "); for (int i=0; i < n; i++) printf("%d ", arr[i]); return 0; }
-
Write a C program to find the length of a string without using a built-in function.
Use the following code:
#include <stdio.h> int stringLength(char *str) { int length = 0; while (*str != '\0') { length++; str++; } return length; } int main() { char str[] = "Hello World"; printf("Length of string: %d", stringLength(str)); return 0; }
-
Write a C program to merge two sorted arrays.
Use the following code:
#include <stdio.h> void merge(int arr1[], int arr2[], int n1, int n2, int arr3[]) { int i = 0, j = 0, k = 0; while (i < n1 && j < n2) { if (arr1[i] < arr2[j]) arr3[k++] = arr1[i++]; else arr3[k++] = arr2[j++]; } while (i < n1) arr3[k++] = arr1[i++]; while (j < n2) arr3[k++] = arr2[j++]; } int main() { int arr1[] = {1, 3, 5, 7}; int n1 = sizeof(arr1)/sizeof(arr1[0]); int arr2[] = {2, 4, 6, 8}; int n2 = sizeof(arr2)/sizeof(arr2[0]); int arr3[n1+n2]; merge(arr1, arr2, n1, n2, arr3); printf("Merged array: "); for (int i=0; i < n1+n2; i++) printf("%d ", arr3[i]); return 0; }
-
Write a C program to find the sum of digits of a number.
Use the following code:
#include <stdio.h> int sumOfDigits(int n) { int sum = 0; while (n != 0) { sum += n % 10; n /= 10; } return sum; } int main() { int num = 1234; printf("Sum of digits of %d is %d", num, sumOfDigits(num)); return 0; }
-
Write a C program to check if a number is a palindrome.
Use the following code:
#include <stdio.h> int isPalindrome(int n) { int original = n, reversed = 0; while (n != 0) { int digit = n % 10; reversed = reversed * 10 + digit; n /= 10; } return original == reversed; } int main() { int num = 121; if (isPalindrome(num)) printf("%d is a palindrome", num); else printf("%d is not a palindrome", num); return 0; }
-
Write a C program to find the largest of three numbers.
Use the following code:
#include <stdio.h> int main() { int a = 10, b = 20, c = 15; if (a >= b && a >= c) printf("Largest number is %d", a); else if (b >= a && b >= c) printf("Largest number is %d", b); else printf("Largest number is %d", c); return 0; }
-
Write a C program to find the Fibonacci series up to a given number.
Use the following code:
#include <stdio.h> void printFibonacci(int n) { int t1 = 0, t2 = 1, nextTerm = 0; printf("Fibonacci Series: %d, %d", t1, t2); nextTerm = t1 + t2; while (nextTerm <= n) { printf(", %d", nextTerm); t1 = t2; t2 = nextTerm; nextTerm = t1 + t2; } } int main() { int n = 50; printFibonacci(n); return 0; }
-
Write a C program to reverse a linked list.
Use the following C code:
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } Node; void push(Node** head_ref, int new_data) { Node* new_node = (Node*) malloc(sizeof(Node)); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } void printList(Node *node) { while (node != NULL) { printf("%d - ", node->data); node = node->next; } printf("NULL\n"); } void reverse(Node** head_ref) { Node* prev = NULL; Node* current = *head_ref; Node* next = NULL; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } *head_ref = prev; } int main() { Node* head = NULL; push(&head, 20); push(&head, 15); push(&head, 10); push(&head, 5); printf("Original List: "); printList(head); reverse(&head); printf("Reversed List: "); printList(head); return 0; }
-
Write a C program to check if two strings are anagrams.
Use the following C code:
#include <stdio.h> #include <string.h> #define CHAR_COUNT 256 int areAnagrams(char* str1, char* str2) { int count[CHAR_COUNT] = {0}; int i; if (strlen(str1) != strlen(str2)) return 0; for (i = 0; str1[i] && str2[i]; i++) { count[str1[i]]++; count[str2[i]]--; } for (i = 0; i < CHAR_COUNT; i++) { if (count[i]) return 0; } return 1; } int main() { char str1[] = "listen"; char str2[] = "silent"; if (areAnagrams(str1, str2)) { printf("The strings are anagrams.\n"); } else { printf("The strings are not anagrams.\n"); } return 0; }
-
Write a C program to find the longest common prefix of an array of strings.
Use the following C code:
#include <stdio.h> #include <string.h> #define MAX 100 char* longestCommonPrefix(char* strs[], int strsSize) { if (strsSize == 0) return ""; char* prefix = strs[0]; for (int i = 1; i < strsSize; i++) { while (strncmp(prefix, strs[i], strlen(prefix)) != 0) { prefix[strlen(prefix) - 1] = '\0'; } } return prefix; } int main() { char* strs[] = {"flower", "flow", "flight"}; int strsSize = sizeof(strs) / sizeof(strs[0]); printf("Longest Common Prefix: %s\n", longestCommonPrefix(strs, strsSize)); return 0; }
-
Write a C program to implement a basic stack using an array.
Use the following C code:
#include <stdio.h> #include <stdlib.h> #define MAX 100 typedef struct { int top; int arr[MAX]; } Stack; void init(Stack* stack) { stack->top = -1; } int isFull(Stack* stack) { return stack->top == MAX - 1; } int isEmpty(Stack* stack) { return stack->top == -1; } void push(Stack* stack, int value) { if (isFull(stack)) { printf("Stack overflow\n"); return; } stack->arr[++stack->top] = value; } int pop(Stack* stack) { if (isEmpty(stack)) { printf("Stack underflow\n"); return -1; } return stack->arr[stack->top--]; } int peek(Stack* stack) { if (isEmpty(stack)) { printf("Stack is empty\n"); return -1; } return stack->arr[stack->top]; } int main() { Stack stack; init(&stack); push(&stack, 10); push(&stack, 20); push(&stack, 30); printf("Top element: %d\n", peek(&stack)); printf("Popped element: %d\n", pop(&stack)); printf("Top element after pop: %d\n", peek(&stack)); return 0; }
-
Write a C program to find all unique pairs in an array that sum up to a given number.
Use the following C code:
#include <stdio.h> #include <stdlib.h> void findPairs(int arr[], int size, int sum) { int* hash = (int*) calloc(size, sizeof(int)); printf("Pairs with sum %d:\n", sum); for (int i = 0; i < size; i++) { int complement = sum - arr[i]; if (complement >= 0 && hash[complement] != 0) { printf("%d + %d = %d\n", arr[i], complement, sum); } hash[arr[i]] = 1; } free(hash); } int main() { int arr[] = {1, 4, 6, 8, 10}; int size = sizeof(arr) / sizeof(arr[0]); int sum = 10; findPairs(arr, size, sum); return 0; }