Show List

C Coding Questions

  • Write a C program to find the sum of two integers.
c
Copy code
#include <stdio.h> int main() { int a, b, sum; printf("Enter two integers: "); scanf("%d %d", &a, &b); sum = a + b; printf("Sum of %d and %d is %d\n", a, b, sum); return 0; }
  • Write a C program to find the factorial of a number.
c
Copy code
#include <stdio.h> int main() { int n, i, factorial = 1; printf("Enter a positive integer: "); scanf("%d", &n); for (i = 1; i <= n; i++) { factorial *= i; } printf("Factorial of %d is %d\n", n, factorial); return 0; }
  • Write a C program to find the largest among three numbers.
c
Copy code
#include <stdio.h> int main() { int a, b, c, largest; printf("Enter three numbers: "); scanf("%d %d %d", &a, &b, &c); if (a > b && a > c) { largest = a; } else if (b > a && b > c) { largest = b; } else { largest = c; } printf("Largest among %d, %d and %d is %d\n", a, b, c, largest); return 0; }
  • Write a C program to find the sum of numbers from 1 to n.
c
Copy code
#include <stdio.h> int main() { int n, i, sum = 0; printf("Enter a positive integer: "); scanf("%d", &n); for (i = 1; i <= n; i++) { sum += i; } printf("Sum of numbers from 1 to %d is %d\n", n, sum); return 0; }
  • Write a C program to swap two numbers without using a temporary variable.
c
Copy code
#include <stdio.h> int main() { int a, b; printf("Enter two numbers: "); scanf("%d %d", &a, &b); a = a + b; b = a - b; a = a - b; printf("After swapping, a = %d and b = %d\n", a, b); return 0; }
  • Write a C program to reverse a string.
c
Copy code
#include <stdio.h> #include <string.h> int main() { char str[100], temp; int i, j; printf("Enter a string: "); scanf("%s", str); j = strlen(str) - 1; for (i = 0; i < j; i++, j--) { temp = str[i]; str[i] = str[j]; str[j] = temp; } printf("Reversed string is: %s\n", str); return 0; }
  • Write a C program to find the second largest number in an array.
c
Copy code
#include <stdio.h> int main() { int n, i, largest, second_largest; printf("Enter the size of the array: "); scanf("%d", &n); int arr[n]; printf("Enter the elements of the array: "); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } largest = arr[0]; second_largest = arr[1]; for (i = 1; i < n; i++) { if (arr[i] > largest) { second_largest = largest; largest = arr[i]; } else if (arr[i] > second_largest && arr[i] != largest) { second_largest = arr[i]; } } printf("The second largest element in the array is: %d\n", second_largest); return 0; }
  • Write a C program to count the number of even and odd elements in an array.
c
Copy code
#include <stdio.h> int main() { int n, i, even_count = 0, odd_count = 0; printf("Enter the size of the array: "); scanf("%d", &n); int arr[n]; printf("Enter the elements of the array: "); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); if (arr[i] % 2 == 0) { even_count++; } else { odd_count++; } } printf("The number of even elements in the array is: %d\n", even_count); printf("The number of odd elements in the array is: %d\n", odd_count); return 0; }
  • Write a C program to reverse an array.
c
Copy code
#include <stdio.h> int main() { int n, i, j, temp; printf("Enter the size of the array: "); scanf("%d", &n); int arr[n]; printf("Enter the elements of the array: "); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } j = n - 1; for (i = 0; i < j; i++, j--) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } printf("The reversed array is: "); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; }
  • Write a C program to check if a given string is a palindrome or not.
c
Copy code
#include <stdio.h> #include <string.h> int main() { char str[100]; int i, j, n, flag = 0; printf("Enter a string: "); scanf("%s", str); n = strlen(str); for (i = 0, j = n - 1; i < j; i++, j--) { if (str[i] != str[j]) { flag = 1; break; } } if (flag == 0) { printf("%s is a palindrome.\n", str); } else { printf("%s is not a palindrome.\n", str); } return 0; }
  • Write a C program to find the sum of all elements in an array.
c
Copy code
#include <stdio.h> int main() { int n, i, sum = 0; printf("Enter the size of the array: "); scanf("%d", &n); int arr[n]; printf("Enter the elements of the array: "); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); sum += arr[i]; } printf("The sum of all elements in the array is: %d\n", sum); return 0; }
  • Write a C program to find the frequency of a given element in an array.
c
Copy code
#include <stdio.h> int main() { int n, i, element, frequency = 0; printf("Enter the size of the array: "); scanf("%d", &n); int arr[n]; printf("Enter the elements of the array: "); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } printf("Enter the element to find its frequency: "); scanf("%d", &element); for (i = 0; i < n; i++) { if (arr[i] == element) { frequency++; } } printf("The frequency of %d in the array is: %d\n", element, frequency); return 0; }
  • Write a C program to remove duplicates from an array.
c
Copy code
#include <stdio.h> int main() { int n, i, j, k; printf("Enter the size of the array: "); scanf("%d", &n); int arr[n]; printf("Enter the elements of the array: "); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (arr[i] == arr[j]) { for (k = j; k < n; k++) { arr[k] = arr[k + 1]; } n--; j--; } } } printf("The array with duplicates removed is: "); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; }
  • Write a C program to reverse a given string without using the built-in strrev() function.
c
Copy code
#include <stdio.h> #include <string.h> int main() { char str[100], temp; int i, j, n; printf("Enter a string: "); scanf("%s", str); n = strlen(str); for (i = 0, j = n - 1; i < j; i++, j--) { temp = str[i]; str[i] = str[j]; str[j] = temp; } printf("The reversed string is: %s\n", str); return 0; }
  • Write a C program to find the largest and smallest element in an array.
c
Copy code
#include <stdio.h> int main() { int n, i, max, min; printf("Enter the size of the array: "); scanf("%d", &n); int arr[n]; printf("Enter the elements of the array: "); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } max = min = arr[0]; for (i = 1; i < n; i++) { if (arr[i] > max) { max = arr[i]; } if (arr[i] < min) { min = arr[i]; } } printf("The largest element in the array is: %d\n", max); printf("The smallest element in the array is: %d\n", min); return 0; }
  • Write a C program to find the second largest element in an array.
c
Copy code
#include <stdio.h> int main() { int n, i, largest, second_largest; printf("Enter the size of the array: "); scanf("%d", &n); int arr[n]; printf("Enter the elements of the array: "); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } largest = second_largest = arr[0]; for (i = 1; i < n; i++) { if (arr[i] > largest) { second_largest = largest; largest = arr[i]; } else if (arr[i] > second_largest && arr[i] != largest) { second_largest = arr[i]; } } printf("The second largest element in the array is: %d\n", second_largest); return 0; }

    Leave a Comment


  • captcha text