Work in Progress 🚧Arrays in C🚀
🚀
🎉 60 questions live – more coming soon! Stay tuned for updates. Last updated on May 20, 2025
Question 1: C Program to Read and Print Elements of an Array
easy#include <stdio.h>
void main()
{
int n, i, arr[50];
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter array elements -\n");
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("\nArray elements are -\n");
for (i = 0; i < n; i++)
printf("%d\n", arr[i]);
}Output:
Array elements are - 1 2 3 4 5
Question 2: C Program to Count the Total Number of Duplicate Elements in an Array
medium#include <stdio.h>
int main()
{
int arr[100];
int size, count = 0;
int i, j;
printf("Input the number of elements to be stored in the array :");
scanf("%d", &size);
printf("Input %d elements in the array :\n", size);
for (i = 0; i < size; i++)
{
printf("element - %d : ", i);
scanf("%d", &arr[i]);
}
for (i = 0; i < size; i++)
{
for (j = i + 1; j < size; j++)
{
if (arr[i] == arr[j])
{
count++;
break;
}
}
}
printf("Total number of duplicate elements found in the array: %d\n", count);
return 0;
}Output:
Total number of duplicate elements found in the array: 2
Question 3.1: C Program to Print All Unique Elements in an Array
medium#include <stdio.h>
int main()
{
int arr[100], size, count = 0;
int i, j, k;
printf("Input the number of elements to be stored in the array: ");
scanf("%d", &size);
printf("Input %d elements in the array :\n", size);
for (i = 0; i < size; i++)
{
printf("element - %d : ", i);
scanf("%d", &arr[i]);
}
printf("\nThe unique elements found in the array are: \n");
for (i = 0; i < size; i++)
{
count = 0;
for (j = 0, k = size; j < k + 1; j++)
{
if (i != j)
{
if (arr[i] == arr[j])
{
count++;
}
}
}
if (count == 0)
{
printf("%d ", arr[i]);
}
}
printf("\n\n");
return 0;
}Output:
The unique elements found in the array are: 3 4
Question 3.2: C Program to Print All Unique Elements in an Array (Method 2)
medium#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements -\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Unique elements in the array are: ");
for (int i = 0; i < size; i++) {
int isUnique = 1;
for (int j = 0; j < size; j++) {
if (i != j && arr[i] == arr[j]) {
isUnique = 0;
break;
}
}
if (isUnique) {
printf("%d ", arr[i]);
}
}
return 0;
}Output:
Unique elements in the array are: 3 4
Question 3.3: C Program to Print All Unique Elements in an Array (Method 3)
easy#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements -\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
int freq[100] = {0}; // Assuming the maximum possible value in the array is 99
for (int i = 0; i < size; i++) {
freq[arr[i]]++;
}
printf("Unique elements in the array are: ");
for (int i = 0; i < size; i++) {
if (freq[arr[i]] == 1) {
printf("%d ", arr[i]);
}
}
return 0;
}Output:
Unique elements in the array are: 3 4
Question 4: C Program to Print the Sum of Array Elements
easy#include <stdio.h>
void main()
{
int size, i, arr[50], sum = 0;
printf("Enter number of elements: ");
scanf("%d", &size);
printf("Enter array elements -\n");
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
for (i = 0; i < size; i++)
sum = sum + arr[i];
printf("Sum of array elements: %d", sum);
}Output:
Sum of array elements: 15
Question 5: C Program to Print Maximum and Minimum Elements in the Array
easy#include <stdio.h>
void main()
{
int arr[50], size, i, min, max;
printf("Enter number of elements: ");
scanf("%d", &size);
printf("Enter array elements-\n");
for(i = 0; i < size; i++)
scanf("%d", &arr[i]);
// Assuming the first element as minimum and maximum
min = arr[0];
max = arr[0];
for (i = 1; i < size; i++)
{
// Checking for minimum number
if (arr[i] < min)
min = arr[i];
// Checking for maximum number
if (arr[i] > max)
max = arr[i];
}
printf("\nMinimum number in the array is: %d\n", min);
printf("Maximum number in the array is: %d", max);
}Output:
Minimum number in the array is: 1 Maximum number in the array is: 7
Question 6: C Program to Print the Second Largest Element in the Array
medium#include <stdio.h>
#include <limits.h>
void main()
{
int arr[50], size, i, max1, max2;
printf("Enter number of array elements: ");
scanf("%d", &size);
printf("Enter array elements-\n");
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
max1 = max2 = INT_MIN;
for (i = 0; i < size; i++)
{
if (arr[i] > max1)
{
max2 = max1;
max1 = arr[i];
}
else if (arr[i] < max1 && arr[i] > max2)
{
max2 = arr[i];
}
}
if (max2 == INT_MIN)
printf("\nNo second largest element found.");
else
printf("\nSecond largest number in array is: %d", max2);
}Output:
Second largest number in array is: 7
Question 7: C Program to Print the Count of Even and Odd Numbers in the Array
easy#include <stdio.h>
void main()
{
int arr[50], size, i, even = 0, odd = 0;
printf("Enter number of elements: ");
scanf("%d", &size);
printf("Enter array elements-\n");
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
for (i = 0; i < size; i++)
{
if (arr[i] % 2 == 0)
even++;
else
odd++;
}
printf("\nCount of even numbers: %d\n", even);
printf("Count of odd numbers: %d", odd);
}Output:
Count of even numbers: 3 Count of odd numbers: 3
Question 8: C Program to Count Positive, Negative, and Zero Numbers in the Array
easy#include <stdio.h>
void main()
{
int arr[50], size, i, positive = 0, negative = 0, zero = 0;
printf("Enter number of elements: ");
scanf("%d", &size);
printf("Enter array elements-\n");
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
for (i = 0; i < size; i++)
{
if (arr[i] > 0)
positive++;
else if (arr[i] < 0)
negative++;
else
zero++;
}
printf("\nNumber of positive elements: %d\n", positive);
printf("Number of negative elements: %d\n", negative);
printf("Number of zeros: %d", zero);
}Output:
Number of positive elements: 3 Number of negative elements: 2 Number of zeros: 2
Question 9: C Program to Copy All Array Elements from One Array to Another
easy#include <stdio.h>
void main()
{
int oldArray[50], newArray[50], size, i;
printf("Enter number of elements: ");
scanf("%d", &size);
printf("Enter array elements-\n");
for (i = 0; i < size; i++)
scanf("%d", &oldArray[i]);
// Copying elements from old array to new array
for (i = 0; i < size; i++)
newArray[i] = oldArray[i];
printf("\nElements of old array-\n");
for (i = 0; i < size; i++)
printf("%d ", oldArray[i]);
printf("\nElements of new array-\n");
for (i = 0; i < size; i++)
printf("%d ", newArray[i]);
}Output:
Elements of old array- 1 2 3 4 5 Elements of new array- 1 2 3 4 5
Question 10: C Program to Insert an Element in the Array
medium#include <stdio.h>
void main()
{
int arr[50], size, pos, i, num;
printf("Enter number of elements: ");
scanf("%d", &size);
printf("Enter array elements-\n");
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
printf("\nEnter number to insert: ");
scanf("%d", &num);
printf("Enter position to insert element: ");
scanf("%d", &pos);
// Check for invalid position
if (pos < 0 || pos > size)
printf("Invalid position");
else
{
// Shifting elements to make space for the new element
for (i = size - 1; i >= pos - 1; i--)
arr[i + 1] = arr[i];
arr[pos - 1] = num;
size++;
printf("Array after insertion-\n");
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
}
}Output:
Array after insertion- 1 2 10 3 4 5
Question 11: C Program to Delete an Element from the Array at a Given Position
medium#include <stdio.h>
void main()
{
int arr[50], size, pos, i;
printf("Enter number of elements: ");
scanf("%d", &size);
printf("Enter array elements-\n");
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
printf("\nEnter position to delete: ");
scanf("%d", &pos);
// Check for invalid position
if (pos < 1 || pos > size)
printf("Invalid position");
else
{
// Shifting elements to remove the element at given position
for(i = pos - 1; i < size - 1; i++)
arr[i] = arr[i + 1];
size--;
printf("Array after deletion-\n");
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
}
}Output:
Array after deletion- 1 2 4 5
Question 12: C Program to Delete an Element from the Array by a Given Value
medium#include <stdio.h>
int main() {
int arr[50], size, num, i, pos = -1;
printf("Enter number of elements: ");
scanf("%d", &size);
printf("Enter array elements-\n");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("\nEnter element to delete: ");
scanf("%d", &num);
// Find position of the element to be deleted
for (i = 0; i < size; i++) {
if (arr[i] == num) {
pos = i;
break;
}
}
// If element is not found
if (pos == -1) {
printf("Element %d not found in the array.\n", num);
} else {
// Shift elements to remove the found element
for (i = pos; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
printf("Array after deletion:\n");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
return 0;
}Output:
Array after deletion: 1 2 3 5 6
Question 13.1: C Program to Print the Frequency of Each Element in the Array (Method 1)
medium#include <stdio.h>
void main()
{
int arr[50], size, i, j, k, isCounted, count;
printf("Enter number of elements: ");
scanf("%d", &size);
printf("Enter array elements-\n");
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
printf("\nFrequency of elements-\n");
for (i = 0; i < size; i++)
{
isCounted = 0;
for (j = 0; j < i; j++)
{
if (arr[i] == arr[j])
isCounted = 1;
}
if (!isCounted)
{
count = 0;
for (k = i; k < size; k++)
{
if (arr[i] == arr[k])
count++;
}
printf("%d occurs %d times\n", arr[i], count);
}
}
}Output:
Frequency of elements: 1 occurs 2 times 2 occurs 2 times 3 occurs 1 time 4 occurs 1 time
Question 13.2: C Program to Print the Frequency of Each Element in the Array (Method 2 - Optimized)
easy#include <stdio.h>
void main()
{
int arr[50], size, i;
printf("Enter number of elements: ");
scanf("%d", &size);
printf("Enter array elements-\n");
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
printf("\nFrequency of elements-\n");
int freq[100] = {0}; // Assuming max element value is <100
for (i = 0; i < size; i++) {
freq[arr[i]]++;
}
for (i = 0; i < size; i++) {
if (freq[arr[i]] > 0) {
printf("%d occurs %d times\n", arr[i], freq[arr[i]]);
freq[arr[i]] = 0; // Reset to avoid duplicate printing
}
}
}Output:
Frequency of elements: 1 occurs 2 times 2 occurs 2 times 3 occurs 1 time 4 occurs 1 time
Question 14.1: C Program to Delete All Duplicate Elements from an Array (Method 1)
medium#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements -\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Process to remove duplicates
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; ) {
if (arr[i] == arr[j]) {
// Shift elements to the left
for (int k = j; k < size - 1; k++) {
arr[k] = arr[k + 1];
}
size--; // Reduce the size of the array
} else {
j++;
}
}
}
// Printing the array after removing duplicates
printf("Array after removing duplicates: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}Output:
Array after removing duplicates: 1 2 3 4
Question 14.2: C Program to Delete All Duplicate Elements from an Array (Method 2 - Optimized)
easy#include <stdio.h>
#define MAX 100
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements -\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
int freq[MAX] = {0}; // Frequency array, assuming elements range from 0 to MAX-1
int uniqueArr[size]; // Array to store unique elements
int uniqueSize = 0; // Size of the unique array
// Populate frequency array and create array with unique elements
for (int i = 0; i < size; i++) {
if (freq[arr[i]] == 0) {
uniqueArr[uniqueSize++] = arr[i];
}
freq[arr[i]]++;
}
// Copy unique elements back to original array
for (int i = 0; i < uniqueSize; i++) {
arr[i] = uniqueArr[i];
}
size = uniqueSize; // Update the size to the number of unique elements
// Printing the array after removing duplicates
printf("Array after removing duplicates: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}Output:
Array after removing duplicates: 1 2 3 4
Question 15: C Program to Merge Two Arrays into a Third Array
easy#include <stdio.h>
int main() {
int size1, size2;
// Input size and elements of the first array
printf("Enter the size of the first array: ");
scanf("%d", &size1);
int arr1[size1];
printf("Enter %d elements for the first array -\n", size1);
for (int i = 0; i < size1; i++) {
scanf("%d", &arr1[i]);
}
// Input size and elements of the second array
printf("Enter the size of the second array: ");
scanf("%d", &size2);
int arr2[size2];
printf("Enter %d elements for the second array -\n", size2);
for (int i = 0; i < size2; i++) {
scanf("%d", &arr2[i]);
}
// Declare the third array with size equal to the sum of the first and second arrays
int size3 = size1 + size2;
int arr3[size3];
// Copy elements of the first array to the third array
for (int i = 0; i < size1; i++) {
arr3[i] = arr1[i];
}
// Copy elements of the second array to the third array
for (int i = 0; i < size2; i++) {
arr3[size1 + i] = arr2[i];
}
// Print the merged array
printf("Merged array: ");
for (int i = 0; i < size3; i++) {
printf("%d ", arr3[i]);
}
return 0;
}Output:
Merged array: 1 2 3 4 5 6
Question 16: C Program to Find the Reverse of an Array
easy#include <stdio.h>
int main() {
int size;
// Input size and elements of the array
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements -\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Reverse the array
for (int i = 0; i < size / 2; i++) {
int temp = arr[i];
arr[i] = arr[size - 1 - i];
arr[size - 1 - i] = temp;
}
// Print the reversed array
printf("Reversed array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}Output:
Reversed array: 5 4 3 2 1
Question 17: C Program to Search an Element in an Array
easy#include <stdio.h>
int main() {
int size, element, found = 0;
// Input size and elements of the array
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements -\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Input the element to search
printf("\nEnter the element to search: ");
scanf("%d", &element);
// Search the array
for (int i = 0; i < size; i++) {
if (arr[i] == element) {
found = 1;
printf("Element %d found at position %d", element, i + 1);
break;
}
}
if (!found) {
printf("Element %d not found in the array", element);
}
return 0;
}Output:
Element 30 found at position 3
Question 18: C Program for Sorting an Array in Ascending Order
easy#include <stdio.h>
int main() {
int size;
// Input size and elements of the array
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements -\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Bubble Sort in Ascending Order
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
// Swap the elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// Print the sorted array
printf("\nArray sorted in ascending order: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}Output:
Array sorted in ascending order: 9 12 21 34 56
Question 19: C Program for Sorting an Array in Descending Order
easy#include <stdio.h>
int main() {
int size;
// Input size and elements of the array
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements -\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Bubble Sort in Descending Order
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - 1 - i; j++) {
if (arr[j] < arr[j + 1]) {
// Swap the elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// Print the sorted array
printf("\nArray sorted in descending order: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}Output:
Array sorted in descending order: 56 34 21 12 9
Question 20: C Program to Left Rotate an Array
easy#include <stdio.h>
int main() {
int size, rotations;
// Step 1: Input size and elements of the array
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements -\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Step 2: Input the number of rotations
printf("Enter the number of positions to rotate the array to the left: ");
scanf("%d", &rotations);
// Step 3: Perform left rotation
int temp[rotations];
// Store the first 'rotations' elements in a temporary array
for (int i = 0; i < rotations; i++) {
temp[i] = arr[i];
}
// Shift the rest of the array elements to the left
for (int i = 0; i < size - rotations; i++) {
arr[i] = arr[i + rotations];
}
// Move the temporary array elements to the end of the array
for (int i = 0; i < rotations; i++) {
arr[size - rotations + i] = temp[i];
}
// Step 4: Print the rotated array
printf("Array after left rotation: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}Output:
Array after left rotation: 3 4 5 1 2
Question 21: C Program to Right Rotate an Array
easy#include <stdio.h>
int main() {
int size, rotations;
// Step 1: Input size and elements of the array
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements -\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Step 2: Input the number of rotations
printf("\nEnter the number of positions to rotate the array to the right: ");
scanf("%d", &rotations);
// Step 3: Adjust the number of rotations if it is greater than the size of the array
rotations = rotations % size;
// Step 4: Perform the right rotation
int temp[rotations];
// Store the last 'rotations' elements in a temporary array
for (int i = 0; i < rotations; i++) {
temp[i] = arr[size - rotations + i];
}
// Shift the rest of the array elements to the right
for (int i = size - 1; i >= rotations; i--) {
arr[i] = arr[i - rotations];
}
// Move the temporary array elements to the beginning of the array
for (int i = 0; i < rotations; i++) {
arr[i] = temp[i];
}
// Step 5: Print the rotated array
printf("Array after right rotation: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}Output:
Array after right rotation: 4 5 1 2 3
Question 22: C Program to Merge Two Arrays and Sort in Descending Order
medium#include <stdio.h>
int main() {
int arr1[] = {5, 3, 8, 1}; // First array
int arr2[] = {7, 2, 6, 4}; // Second array
int arr3[100];
int s1 = 4, s2 = 4, s3 = s1 + s2;
int i, j, k;
// Insert elements from arr1 into arr3
for (i = 0; i < s1; i++) {
arr3[i] = arr1[i];
}
// Insert elements from arr2 into arr3
for (j = 0; j < s2; j++) {
arr3[i] = arr2[j];
i++;
}
// Sort the array in descending order using Bubble Sort
for (i = 0; i < s3; i++) {
for (k = 0; k < s3 - 1; k++) {
if (arr3[k] < arr3[k + 1]) {
j = arr3[k + 1];
arr3[k + 1] = arr3[k];
arr3[k] = j;
}
}
}
// Print the merged and sorted array
printf("Merged and sorted array (Descending Order): ");
for (i = 0; i < s3; i++) {
printf("%d ", arr3[i]);
}
printf("\n");
return 0;
}Output:
Merged and sorted array (Descending Order): 8 7 6 5 4 3 2 1
Question 23: C Program to Separate Odd and Even Integers into Separate Arrays
easy#include <stdio.h>
int main() {
int arr1[10], arr2[10], arr3[10];
int i, j = 0, k = 0, n;
printf("Input the number of elements to be stored in the array: ");
scanf("%d", &n);
printf("Input %d elements in the array:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr1[i]);
}
// Separate even and odd numbers
for (i = 0; i < n; i++) {
if (arr1[i] % 2 == 0) {
arr2[j++] = arr1[i];
} else {
arr3[k++] = arr1[i];
}
}
// Print Even numbers
printf("The Even elements are:\n");
for (i = 0; i < j; i++) {
printf("%d ", arr2[i]);
}
// Print Odd numbers
printf("\nThe Odd elements are:\n");
for (i = 0; i < k; i++) {
printf("%d ", arr3[i]);
}
printf("\n");
return 0;
}Output:
The Even elements are: 12 18 4 The Odd elements are: 7 9 5
Question 24.1: C Program to Find the Second Smallest Element in an Array
easy#include <stdio.h>
#include <limits.h>
int main() {
int arr[50], n, i, j = 0, sml, sml2nd;
printf("Input the size of the array: ");
scanf("%d", &n);
printf("Input %d elements in the array:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Find the smallest element
sml = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] < sml) {
sml = arr[i];
j = i; // Store index of smallest element
}
}
// Find the second smallest element
sml2nd = INT_MAX;
for (i = 0; i < n; i++) {
if (i != j && arr[i] < sml2nd) {
sml2nd = arr[i];
}
}
printf("The second smallest element in the array is: %d\n", sml2nd);
return 0;
}Output:
The second smallest element in the array is: 3
Question 24.2: C Program to Find the Second Smallest Element in an Array (Method 2)
easy#include <stdio.h>
#include <limits.h>
int main() {
int arr[50], n, i, sml, sml2nd;
printf("Input the size of the array: ");
scanf("%d", &n);
printf("Input %d elements in the array:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Initialize smallest and second smallest
sml = INT_MAX;
sml2nd = INT_MAX;
// Find smallest and second smallest
for (i = 0; i < n; i++) {
if (arr[i] < sml) {
sml2nd = sml;
sml = arr[i];
} else if (arr[i] < sml2nd && arr[i] != sml) {
sml2nd = arr[i];
}
}
printf("The second smallest element in the array is: %d\n", sml2nd);
return 0;
}Output:
The second smallest element in the array is: 3
Question 25: C Program to Find the Majority Element in an Array
medium#include <stdio.h>
int main() {
int arr[] = {4, 8, 4, 6, 7, 4, 4, 8, 4};
int n = sizeof(arr) / sizeof(arr[0]);
int candidate = 0, count = 1, i, majorityCount = 0;
// Step 1: Find the candidate for the majority element
for (i = 1; i < n; i++) {
if (arr[candidate] == arr[i])
count++;
else
count--;
if (count == 0) {
candidate = i;
count = 1;
}
}
// Step 2: Verify the candidate by counting its occurrences
for (i = 0; i < n; i++) {
if (arr[i] == arr[candidate])
majorityCount++;
}
// Step 3: Check if the candidate is indeed a majority element
if (majorityCount > n / 2)
printf("Majority Element: %d\n", arr[candidate]);
else
printf("There is no Majority Element in the array.\n");
return 0;
}Output:
Majority Element: 4
Question 26: C Program to Find the Missing Number in an Array
easy#include <stdio.h>
int main() {
int arr[] = {1, 3, 4, 2, 5, 6, 9, 8};
int n = sizeof(arr) / sizeof(arr[0]) + 1;
int i, sum = 0, total;
printf("The given array is : ");
for (i = 0; i < n - 1; i++) {
printf("%d ", arr[i]);
sum += arr[i];
}
printf("\n");
total = (n * (n + 1)) / 2;
printf("The missing number is : %d\n", total - sum);
return 0;
}Output:
The missing number is: 7
Question 27: C Program to Check if an Element Appears More Than n/2 Times in a Sorted Array
easy#include <stdio.h>
int main() {
int arr[] = {1, 2, 2, 2, 2, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 2;
int count = 0;
// Count occurrences of x in the array
for (int i = 0; i < n; i++) {
if (arr[i] == x) {
count++;
}
}
// Check if count is greater than n/2
if (count > n / 2) {
printf("%d appears more than n/2 times in the array.\n", x);
} else {
printf("%d does not appear more than n/2 times in the array.\n", x);
}
return 0;
}Output:
2 appears more than n/2 times in the array.
Question 28: C Program to Move All Zeroes to the End of an Array
easy#include <stdio.h>
int main() {
int arr[] = {1, 0, 2, 0, 0, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int count = 0;
for(int i = 0; i < size; i++) {
if(arr[i] != 0) {
int temp = arr[count];
arr[count] = arr[i];
arr[i] = temp;
count++;
}
}
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}Output:
1 2 4 5 0 0 0
Question 29: C Program to Move All Zeroes to the Beginning of an Array
easy#include <stdio.h>
int main() {
int arr[] = {1, 0, 2, 0, 0, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int count = 0;
for(int i = 0; i < size; i++) {
if(arr[i] == 0) {
int temp = arr[count];
arr[count] = arr[i];
arr[i] = temp;
count++;
}
}
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}Output:
0 0 0 1 2 4 5
Question 30: C Program to Print All Unique Elements in an Unsorted Array
medium#include <stdio.h>
int main() {
int arr1[] = {1, 5, 8, 5, 7, 3, 2, 4, 1, 6, 2};
int n = sizeof(arr1) / sizeof(int);
int i, j;
// Print the original array
printf("The given array is : ");
for (i = 0; i < n; i++) {
printf("%d ", arr1[i]);
}
printf("\n");
printf("Unique Elements in the given array are: \n");
for (i = 0; i < n; i++) {
// Iterate through the array to find unique elements
for (j = 0; j < i; j++) {
// If a duplicate is found, break the loop
if (arr1[i] == arr1[j])
break;
}
// If 'i' reaches 'j', the current element is unique, hence print it
if (i == j) {
printf("%d ", arr1[i]);
}
}
return 0;
}Output:
1 5 8 7 3 2 4 6
Question 31: C Program to Find the Kth Largest and Kth Smallest Element in an Array
medium#include <stdio.h>
int main() {
int arr[] = {7, 10, 4, 3, 20, 15};
int n = sizeof(arr) / sizeof(arr[0]);
int k, i, j, minIndex, temp;
printf("Enter the value of K: ");
scanf("%d", &k);
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
if (k > 0 && k <= n) {
printf("The %dth smallest number is: %d\n", k, arr[k - 1]);
printf("The %dth largest number is: %d\n", k, arr[n - k]);
} else {
printf("Invalid value of K. It must be between 1 and %d.\n", n);
}
return 0;
}Output:
The 3th smallest number is: 7 The 3th largest number is: 10
Question 32: C Program to Print All Prime Numbers in an Array
easy#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Prime numbers in the array are: ");
for (int i = 0; i < n; i++) {
int num = arr[i];
int isPrime = 1;
if (num <= 1) isPrime = 0;
for (int j = 2; j * j <= num; j++) {
if (num % j == 0) {
isPrime = 0;
break;
}
}
if (isPrime) {
printf("%d ", num);
}
}
printf("\n");
return 0;
}Output:
Prime numbers in the array are: 2 3 5 7 11 13
Question 32: C Program to Print All Prime Numbers in an Array (Method 2)
easy#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Prime numbers in the array are: ");
for (int i = 0; i < n; i++) {
int num = arr[i];
int isPrime = 1;
if (num <= 1) isPrime = 0;
for (int j = 2; j <= arr[i] / 2; j++) {
if (arr[i] % j == 0) {
isPrime = 0;
break;
}
}
if (isPrime) {
printf("%d ", num);
}
}
printf("\n");
return 0;
}Output:
Prime numbers in the array are: 2 3 5 7 11 13
Question 33: C Program to Separate Prime and Non-Prime Numbers in an Array
easy#include <stdio.h>
int main() {
int arr[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
int n = sizeof(arr) / sizeof(arr[0]);
int prime[50], nonPrime[50];
int primeCount = 0, nonPrimeCount = 0;
for (int i = 0; i < n; i++) {
int num = arr[i];
int isPrime = 1;
if (num <= 1) {
isPrime = 0;
} else {
for (int j = 2; j * j <= num; j++) {
if (num % j == 0) {
isPrime = 0;
break;
}
}
}
if (isPrime) {
prime[primeCount++] = num;
} else {
nonPrime[nonPrimeCount++] = num;
}
}
printf("Prime numbers: ");
for (int i = 0; i < primeCount; i++) {
printf("%d ", prime[i]);
}
printf("\n");
printf("Non-prime numbers: ");
for (int i = 0; i < nonPrimeCount; i++) {
printf("%d ", nonPrime[i]);
}
printf("\n");
return 0;
}Output:
Prime numbers: 2 3 5 7 11 13 Non-prime numbers: 4 6 8 9 10 12 14 15
Question 34: C Program to Print All Negative Elements in an Array
easy#include <stdio.h>
int main() {
int arr[] = {12, -7, 5, -3, 8, -1, 0, 4, -2};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Negative elements in the array: ");
for (int i = 0; i < n; i++) {
if (arr[i] < 0) {
printf("%d ", arr[i]);
}
}
printf("\n");
return 0;
}Output:
Negative elements in the array: -7 -3 -1 -2
Question 35: C Program to Count the Total Number of Negative Elements in an Array
easy#include <stdio.h>
int main() {
int arr[] = {12, -7, 5, -3, 8, -1, 0, 4, -2};
int n = sizeof(arr) / sizeof(arr[0]);
int count = 0;
for (int i = 0; i < n; i++) {
if (arr[i] < 0) {
count++;
}
}
printf("Total number of negative elements in the array: %d\n", count);
return 0;
}Output:
Total number of negative elements in the array: 4
Question 36: C Program to Replace All Even Numbers with 0 and Odd Numbers with 1
easy#include <stdio.h>
int main() {
int arr[] = {12, 7, 5, 4, 8, 3, 0, 9, 10};
int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
arr[i] = 0;
} else {
arr[i] = 1;
}
}
printf("Modified array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}Output:
Modified array: 0 1 1 0 0 1 0 1 0
Question 37: C Program to Replace All Prime Numbers with 0 and Non-Prime Numbers with 1
easy#include <stdio.h>
int main() {
int arr[] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n; i++) {
int is_prime = 1;
if (arr[i] <= 1) {
is_prime = 0;
} else {
for (int j = 2; j * j <= arr[i]; j++) {
if (arr[i] % j == 0) {
is_prime = 0;
break;
}
}
}
if (is_prime) {
arr[i] = 0;
} else {
arr[i] = 1;
}
}
printf("Modified array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}Output:
Modified array: 0 0 1 0 1 0 1 1 1
Question 38: C Program to Find the Average of Numbers in an Array
easy#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
float average = (float)sum / n;
printf("The average of the numbers in the array is: %.2f\n", average);
return 0;
}Output:
The average of the numbers in the array is: 30.00
Question 39: C Program to Count the Total Number of Prime Numbers in an Array
easy#include <stdio.h>
int main() {
int arr[] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
int n = sizeof(arr) / sizeof(arr[0]);
int primeCount = 0;
for (int i = 0; i < n; i++) {
int num = arr[i];
int isPrime = 1;
if (num <= 1) {
isPrime = 0;
} else {
for (int j = 2; j * j <= num; j++) {
if (num % j == 0) {
isPrime = 0;
break;
}
}
}
if (isPrime) {
primeCount++;
}
}
printf("Total number of prime numbers in the array: %d\n", primeCount);
return 0;
}Output:
Total number of prime numbers in the array: 4
Question 40: C Program to Find the Sum of All Array Elements
easy#include <stdio.h>
int main() {
int arr[] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
int size = sizeof(arr) / sizeof(arr[0]);
int sum = 0;
for(int i = 0; i < size; i++){
sum = sum + arr[i];
}
printf("Sum : %d", sum);
return 0;
}Output:
Sum: 54
Question 41: C Program to Find the Sum of Even Numbers in an Array
easy#include <stdio.h>
int main() {
int arr[] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
int size = sizeof(arr) / sizeof(arr[0]);
int sumEven = 0;
// Check if the number is even
for (int i = 0; i < size; i++) {
if (arr[i] % 2 == 0) {
sumEven += arr[i];
}
}
printf("Sum of even numbers is : %d", sumEven);
return 0;
}Output:
Sum of even numbers is: 30
Question 42: C Program to Find the Sum of Odd Numbers in an Array
easy#include <stdio.h>
int main() {
int arr[] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
int size = sizeof(arr) / sizeof(arr[0]);
int sumOdd = 0;
for (int i = 0; i < size; i++) {
// Check if the number is odd
if (arr[i] % 2 != 0) {
sumOdd += arr[i];
}
}
printf("Sum of odd numbers is : %d", sumOdd);
return 0;
}Output:
Sum of odd numbers is: 24
Question 43: C Program to Find the Average of Even Numbers in an Array
easy#include <stdio.h>
int main() {
int arr[100], size, i, countEven = 0;
float sum = 0, avg;
printf("Enter Array Size : ");
scanf("%d", &size);
printf("Enter the elements : ");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
if (arr[i] % 2 == 0) {
sum += arr[i];
countEven++;
}
}
if (countEven == 0) {
printf("No even numbers entered, cannot calculate average.");
} else {
avg = sum / countEven;
printf("Average of even numbers is : %.2f", avg);
}
return 0;
}Output:
Average of even numbers is: 4.00
Question 44: C Program to Find the Average of Odd Numbers in an Array
easy#include <stdio.h>
int main() {
int arr[100], size, i, countOdd = 0;
float sum = 0, avg;
printf("Enter Array Size : ");
scanf("%d", &size);
printf("Enter the elements : ");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
if (arr[i] % 2 == 1) {
sum += arr[i];
countOdd++;
}
}
if (countOdd == 0) {
printf("No odd numbers entered, cannot calculate average.");
} else {
avg = sum / countOdd;
printf("Average of odd numbers is : %.2f", avg);
}
return 0;
}Output:
Average of odd numbers is: 5.00
Question 45: C Program to Find the Largest Element in an Array Without Sorting
easy#include <stdio.h>
int main() {
int arr[100], size, i, max;
printf("Enter Array Size : ");
scanf("%d", &size);
printf("Enter the elements : ");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
max = arr[0];
for (i = 1; i < size; i++) {
if( arr[i] > max ) {
max = arr[i];
}
}
printf("Largest element in the array is : %d", max);
return 0;
}Output:
Largest element in the array is: 47
Question 46: C Program to Find the Smallest Element in an Array Without Sorting
easy#include <stdio.h>
int main() {
int arr[100], size, i, min;
printf("Enter Array Size : ");
scanf("%d", &size);
printf("Enter the elements : ");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
min = arr[0];
for (i = 1; i < size; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
printf("Smallest element in the array is : %d", min);
return 0;
}Output:
Smallest element in the array is: 5
Question 47: C Program to Find the Most Repeated Number in an Array
medium#include <stdio.h>
int main() {
int arr[100], i, j, count, maxCount, mostRepeated, size;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter the elements: ");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
maxCount = 0;
mostRepeated = arr[0];
for (i = 0; i < size; i++) {
count = 1;
for (j = i + 1; j < size; j++) {
if (arr[i] == arr[j]) {
count++;
}
}
if (count > maxCount) {
maxCount = count;
mostRepeated = arr[i];
}
}
printf("Most repeated number: %d (Count: %d)\n", mostRepeated, maxCount);
return 0;
}Output:
Most repeated number: 2 (Count: 3)
Question 48: C Program to Find Array Elements That Occur Exactly 2 Times
medium#include <stdio.h>
int main() {
int arr[100], i, j, count, size;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter the elements: ");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Elements that occur exactly two times in the array: ");
for (i = 0; i < size; i++) {
count = 0;
for (j = 0; j < size; j++) {
if (arr[i] == arr[j]) {
if (i > j) {
count--;
break;
}
count++;
}
}
if (count == 2) {
printf("%d ", arr[i]);
}
}
return 0;
}Output:
Elements that occur exactly two times in the array: 1 2
Question 49: C Program to Find Non-Prime Numbers in an Array
easy#include <stdio.h>
int main() {
int arr[100], i, j, size, flag;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter the elements: ");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Non-prime numbers in the array: ");
for (i = 0; i < size; i++) {
if (arr[i] == 1) {
printf("%d ", arr[i]);
continue;
}
flag = 0;
for (j = 2; j <= arr[i] / 2; j++) {
if (arr[i] % j == 0) {
flag = 1;
break;
}
}
if (flag == 1) {
printf("%d ", arr[i]);
}
}
return 0;
}Output:
Non-prime numbers in the array: 4 6 8
Question 50: C Program to Remove All Odd Numbers from an Array
easy#include <stdio.h>
int main() {
int arr[100], i, j, size;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter the elements: ");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < size; i++) {
if (arr[i] % 2 == 1) {
for (j = i; j < size - 1; j++) {
arr[j] = arr[j + 1];
}
size = size - 1;
i--;
}
}
printf("Array after removing odd numbers: ");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}Output:
Array after removing odd numbers: 2 4 6
Question 51: C Program to Remove All Even Numbers from an Array
easy#include <stdio.h>
int main() {
int arr[100], i, j, size;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter the elements: ");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < size; i++) {
if (arr[i] % 2 == 0) {
for (j = i; j < size - 1; j++) {
arr[j] = arr[j + 1];
}
size = size - 1;
i--;
}
}
printf("Array after removing even numbers: ");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}Output:
Array after removing even numbers: 1 3 5 7
Question 52: C Program to Remove All Prime Numbers from an Array
medium#include <stdio.h>
int main() {
int arr[100], i, j, size, flag;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter the elements: ");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < size; i++) {
if (arr[i] < 2) {
flag = 1;
} else {
flag = 0;
for (j = 2; j <= arr[i] / 2; j++) {
if (arr[i] % j == 0) {
flag = 1;
break;
}
}
}
if (flag == 0) {
for (j = i; j < size - 1; j++) {
arr[j] = arr[j + 1];
}
size = size - 1;
i--;
}
}
printf("Array after removing prime numbers: ");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}Output:
Array after removing prime numbers: 4 6 8
Question 53: C Program to Delete Two Elements After the Occurrence of an Even Number
medium#include <stdio.h>
int main() {
int arr[100], i, j, size;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter the elements: ");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < size; i++) {
if (arr[i] % 2 == 0) {
if (i <= size - 3) {
for (j = i + 1; j < size - 2; j++) {
arr[j] = arr[j + 2];
}
size -= 2;
} else if (i == size - 2) {
size -= 1;
}
}
}
printf("Array after deleting two elements: ");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}Output:
Array after deleting two elements: 3 4 6 8
Question 54: C Program to Delete Two Elements After the Occurrence of a Prime Number
medium#include <stdio.h>
int main() {
int arr[100], i, j, size, flag;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter the elements: ");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < size; i++) {
flag = 1;
if (arr[i] == 1) {
flag = 0;
} else {
for (j = 2; j <= arr[i] / 2; j++) {
if (arr[i] % j == 0) {
flag = 0;
break;
}
}
}
if (flag == 1) {
if (i <= size - 3) {
for (j = i + 1; j < size - 2; j++) {
arr[j] = arr[j + 2];
}
size -= 2;
} else if (i == size - 2) {
size -= 1;
}
}
}
printf("Array after deleting two elements: ");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}Output:
Array after deleting two elements: 4 5 8 9 10
Question 55: C Program to Delete Two Elements After the Occurrence of an Odd Number
medium#include <stdio.h>
int main() {
int arr[100], i, j, size;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter the elements: ");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < size; i++) {
if (arr[i] % 2 != 0) {
if (i <= size - 3) {
for (j = i + 1; j < size - 2; j++) {
arr[j] = arr[j + 2];
}
size -= 2;
} else if (i == size - 2) {
size -= 1;
}
}
}
printf("Array after deleting two elements: ");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}Output:
Array after deleting two elements: 2 3 6 7 8
Question 56: C Program to Delete Three Elements After the Occurrence of a Prime Number
medium#include <stdio.h>
int main() {
int arr[100], size, flag, i, j;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter the elements: ");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; i < size; i++) {
flag = 0;
if (arr[i] == 1) {
flag = 1;
}
for (j = 2; j < arr[i]; j++) {
if (arr[i] % j == 0) {
flag = 1;
break;
}
}
if (flag == 0) {
if (i <= size - 4) {
for (j = i + 1; j < size - 3; j++) {
arr[j] = arr[j + 3];
}
size -= 3;
} else if (i == size - 3) {
size -= 2;
} else if (i == size - 2) {
size -= 1;
}
}
}
printf("Array after deleting three elements: ");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}Output:
Array after deleting three elements: 2 4 7 8 9 10
Question 57: C Program to Find the Third Largest Number in an Array
easy#include <stdio.h>
#include <limits.h>
int main() {
int arr[100], size;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter the elements: ");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
int firstMax = INT_MIN, secondMax = INT_MIN, thirdMax = INT_MIN;
for (int i = 0; i < size; i++) {
if (arr[i] > firstMax) {
thirdMax = secondMax;
secondMax = firstMax;
firstMax = arr[i];
} else if (arr[i] > secondMax && arr[i] < firstMax) {
thirdMax = secondMax;
secondMax = arr[i];
} else if (arr[i] > thirdMax && arr[i] < secondMax) {
thirdMax = arr[i];
}
}
if (thirdMax == INT_MIN) {
printf("Array doesn't have three distinct numbers.\n");
} else {
printf("The third largest number is: %d\n", thirdMax);
}
return 0;
}Output:
The third largest number is: 12
Question 58: C Program to Find the Third Smallest Number in an Array
easy#include <stdio.h>
#include <limits.h>
int main() {
int arr[100], size;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter the elements: ");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
int firstMin = INT_MAX, secondMin = INT_MAX, thirdMin = INT_MAX;
for (int i = 0; i < size; i++) {
if (arr[i] < firstMin) {
thirdMin = secondMin;
secondMin = firstMin;
firstMin = arr[i];
} else if (arr[i] < secondMin && arr[i] > firstMin) {
thirdMin = secondMin;
secondMin = arr[i];
} else if (arr[i] < thirdMin && arr[i] > secondMin) {
thirdMin = arr[i];
}
}
if (thirdMin == INT_MAX) {
printf("Array doesn't have three distinct numbers.\n");
} else {
printf("The third smallest number is: %d\n", thirdMin);
}
return 0;
}Output:
The third smallest number is: 10
Question 59: C Program to Find the Average of Prime and Non-Prime Numbers in an Array
medium#include <stdio.h>
int main() {
int arr[100], size;
int sumPrime = 0, countPrime = 0;
int sumNonPrime = 0, countNonPrime = 0;
printf("Enter the size of the array: ");
scanf("%d", &size);
printf("Enter the elements: ");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < size; i++) {
int num = arr[i];
int isPrime = 1;
if (num <= 1) {
isPrime = 0;
} else {
for (int j = 2; j * j <= num; j++) {
if (num % j == 0) {
isPrime = 0;
break;
}
}
}
if (isPrime) {
sumPrime += num;
countPrime++;
} else {
sumNonPrime += num;
countNonPrime++;
}
}
float avgPrime = countPrime ? (float)sumPrime / countPrime : 0;
float avgNonPrime = countNonPrime ? (float)sumNonPrime / countNonPrime : 0;
printf("Average of prime numbers: %.2f\n", avgPrime);
printf("Average of non-prime numbers: %.2f\n", avgNonPrime);
return 0;
}Output:
Average of prime numbers: 4.67 Average of non-prime numbers: 6.00
Question 60: C Program to Delete Odd or Even Numbers Based on Array Sum
medium#include <stdio.h>
int main() {
int arr[100], size, sum = 0;
// Input size of the array
printf("Enter the size of the array: ");
scanf("%d", &size);
// Input elements of the array and calculate sum
printf("Enter the elements: ");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
// Print the sum
printf("The sum of the array elements is: %d\n", sum);
// Condition to delete odd or even numbers based on the sum
int deleteOdd = (sum > 100) ? 1 : 0;
printf("Sum is %s 100, deleting %s numbers...\n", (deleteOdd ? "greater than" : "less than or equal to"), (deleteOdd ? "odd" : "even"));
// Delete odd or even numbers and shift the elements
for (int i = 0; i < size; i++) {
if ((deleteOdd && arr[i] % 2 != 0) || (!deleteOdd && arr[i] % 2 == 0)) {
for (int j = i; j < size - 1; j++) {
arr[j] = arr[j + 1];
}
size--; // Decrease size of array
i--; // Recheck current index after shifting
}
}
// Print the modified array
printf("Modified array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}Output:
The sum of the array elements is: 105 Sum is greater than 100, deleting odd numbers... Modified array: 20 30 10