Example of different question in c programming
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements of the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Negative elements in the array: ");
for (int i = 0; i < size; i++) {
if (arr[i] < 0) {
printf("%d ", arr[i]);
}
}
return 0;
}
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements of the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
int negativeCount = 0;
for (int i = 0; i < size; i++) {
if (arr[i] < 0) {
negativeCount++;
}
}
printf("Total number of negative elements: %d\n", negativeCount);
return 0;
}
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements of the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Elements of the array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}
#include <stdio.h>
int main() {
int size, sum = 0;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements of the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Sum of all elements: %d\n", sum);
return 0;
}
#include <stdio.h>
int main() {
int size, evenCount = 0, oddCount = 0;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements of the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
if (arr[i] % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
}
printf("Even elements: %d\n", evenCount);
printf("Odd elements: %d\n", oddCount);
return 0;
}
#include <stdio.h>
int main() {
int size, max, min;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements of the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
if (i == 0 || arr[i] > max) {
max = arr[i];
}
if (i == 0 || arr[i] < min) {
min = arr[i];
}
}
printf("Maximum element: %d\n", max);
printf("Minimum element: %d\n", min);
return 0;
}
#include <stdio.h>
int main() {
int size, element, position;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements of the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to insert: ");
scanf("%d", &element);
printf("Enter the position to insert (0-%d): ", size);
scanf("%d", &position);
if (position < 0 || position > size) {
printf("Invalid position.\n");
} else {
for (int i = size - 1; i >= position; i--) {
arr[i + 1] = arr[i];
}
arr[position] = element;
size++;
printf("Element inserted successfully.\n");
printf("Updated array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements of the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Unique elements in the array: ");
for (int i = 0; i < size; i++) {
int isUnique = 1;
for (int j = 0; j < i; j++) {
if (arr[i] == arr[j]) {
isUnique = 0;
break;
}
}
if (isUnique) {
printf("%d ", arr[i]);
}
}
printf("\n");
return 0;
}
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements of the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Bubble Sort to sort in ascending order
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("Sorted array in ascending order: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the source array: ");
scanf("%d", &size);
int source[size];
printf("Enter %d elements of the source array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &source[i]);
}
int destination[size];
for (int i = 0; i < size; i++) {
destination[i] = source[i];
}
printf("Elements copied to another array: ");
for (int i = 0; i < size; i++) {
printf("%d ", destination[i]);
}
printf("\n");
return 0;
}
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements of the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Number of each element in the array:\n");
for (int i = 0; i < size; i++) {
int count = 1;
for (int j = i + 1; j < size; j++) {
if (arr[i] == arr[j]) {
count++;
// To avoid counting the same element multiple times
arr[j] = arr[size - 1];
size--;
}
}
if (arr[i] != arr[size - 1]) {
printf("%d occurs %d times\n", arr[i], count);
}
}
return 0;
}
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements of the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
int newSize = size;
for (int i = 0; i < newSize; i++) {
for (int j = i + 1; j < newSize;) {
if (arr[i] == arr[j]) {
for (int k = j; k < newSize - 1; k++) {
arr[k] = arr[k + 1];
}
newSize--;
} else {
j++;
}
}
}
printf("Array with duplicate elements removed: ");
for (int i = 0; i < newSize; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements of the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
int duplicateCount = 0;
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (arr[i] == arr[j]) {
duplicateCount++;
// To avoid counting the same element multiple times
arr[j] = arr[size - 1];
size--;
}
}
}
printf("Total duplicate elements in the array: %d\n", duplicateCount);
return 0;
}
#include <stdio.h>
int main() {
int size1, size2;
printf("Enter the size of the first array: ");
scanf("%d", &size1);
int arr1[size1];
printf("Enter %d elements of the first array (in ascending order):\n", size1);
for (int i = 0; i < size1; i++) {
scanf("%d", &arr1[i]);
}
printf("Enter the size of the second array: ");
scanf("%d", &size2);
int arr2[size2];
printf("Enter %d elements of the second array (in ascending order):\n", size2);
for (int i = 0; i < size2; i++) {
scanf("%d", &arr2[i]);
}
int mergedArray[size1 + size2];
int sizeMerged = size1 + size2;
int index1 = 0, index2 = 0;
for (int i = 0; i < sizeMerged; i++) {
if (index1 < size1 && (index2 >= size2 || arr1[index1] < arr2[index2])) {
mergedArray[i] = arr1[index1];
index1++;
} else {
mergedArray[i] = arr2[index2];
index2++;
}
}
printf("Merged array: ");
for (int i = 0; i < sizeMerged; i++) {
printf("%d ", mergedArray[i]);
}
printf("\n");
return 0;
}
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements of the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
int evenArray[size];
int oddArray[size];
int evenCount = 0, oddCount = 0;
for (int i = 0; i < size; i++) {
if (arr[i] % 2 == 0) {
evenArray[evenCount] = arr[i];
evenCount++;
} else {
oddArray[oddCount] = arr[i];
oddCount++;
}
}
printf("Even elements: ");
for (int i = 0; i < evenCount; i++) {
printf("%d ", evenArray[i]);
}
printf("\n");
printf("Odd elements: ");
for (int i = 0; i < oddCount; i++) {
printf("%d ", oddArray[i]);
}
printf("\n");
return 0;
}
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements of the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Original array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
int reversedArray[size];
for (int i = 0; i < size; i++) {
reversedArray[i] = arr[size - i - 1];
}
printf("Reversed array: ");
for (int i = 0; i < size; i++) {
printf("%d ", reversedArray[i]);
}
printf("\n");
return 0;
}
#include <stdio.h>
void rightRotate(int arr[], int size, int d) {
int temp[d];
for (int i = 0; i < d; i++) {
temp[i] = arr[size - d + i];
}
for (int i = size - 1; i >= d; i--) {
arr[i] = arr[i - d];
}
for (int i = 0; i < d; i++) {
arr[i] = temp[i];
}
}
int main() {
int size, d;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements of the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the number of positions to right rotate: ");
scanf("%d", &d);
rightRotate(arr, size, d);
printf("Right rotated array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements of the array:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Original array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
int reversedArray[size];
for (int i = 0; i < size; i++) {
reversedArray[i] = arr[size - i - 1];
}
printf("Reversed array: ");
for (int i = 0; i < size; i++) {
printf("%d ", reversedArray[i]);
}
printf("\n");
return 0;
}