Write a program to read array of integers and print it in reverse order.
#include <stdio.h>int main(){// Initialize arrayint arr[] = {1, 2, 3, 4, 5};// Calculate length of array arrint length = sizeof(arr) / sizeof(arr[0]);printf("Original array: \n");for (int i = 0; i < length; i++){printf("%d ", arr[i]);}printf("\n");printf("Array in reverse order: \n");// Loop through the array in reverse orderfor (int i = length - 1; i >= 0; i--){printf("%d ", arr[i]);}return 0;}
Write a program that adds two 1-dimensional array & store into third array.
#include <stdio.h>int main(){int arr1[5] = {1, 2, 3, 4, 5};int arr2[5] = {6, 7, 8, 9, 10};int result[5];int i;for (i = 0; i < 5; i++){result[i] = arr1[i] + arr2[i];}printf("Resultant array after addition is:\n");for (i = 0; i < 5; i++){printf("%d ", result[i]);}return 0;}
Write a program to sort a given array in ascending order. (use Bubble Sort algorithm)
#include <stdio.h>void bubbleSort(int arr[], int n) {int i, j;for (i = 0; i < n-1; i++) {for (j = 0; j < n-i-1; j++) {if (arr[j] > arr[j+1]) {// swap arr[j] and arr[j+1]int temp = arr[j];arr[j] = arr[j+1];arr[j+1] = temp;}}}}int main() {int arr[] = {5, 2, 9, 1, 5, 6};int n = sizeof(arr)/sizeof(arr[0]);int i;printf("Before sorting: ");for (i = 0; i < n; i++) {printf("%d ", arr[i]);}bubbleSort(arr, n);printf("\nAfter sorting: ");for (i = 0; i < n; i++) {printf("%d ", arr[i]);}return 0;}
0 Comments