0% found this document useful (0 votes)
5 views20 pages

mad project

The document contains multiple C programs that demonstrate various array and string manipulations, including inserting and removing elements, finding duplicates, sorting using selection and bubble sort, swapping strings and arrays, and checking if two arrays or matrices are equal. It also includes programs for printing specific patterns and calculating factorials and prime numbers. Each program is accompanied by explanations and examples of usage.

Uploaded by

sukrutkokate123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views20 pages

mad project

The document contains multiple C programs that demonstrate various array and string manipulations, including inserting and removing elements, finding duplicates, sorting using selection and bubble sort, swapping strings and arrays, and checking if two arrays or matrices are equal. It also includes programs for printing specific patterns and calculating factorials and prime numbers. Each program is accompanied by explanations and examples of usage.

Uploaded by

sukrutkokate123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

 Write a C program to insert element in an array on given specific position.

#include <stdio.h>

void insertElement(int arr[], int *size, int position, int element) {

if (position < 0 || position > *size) {

printf("Invalid position.\n");

return;

Shift elements to the right to make space for the new element

for (int i = *size - 1; i >= position; i--) {

arr[i + 1] = arr[i];

// Insert the element at the specified position

arr[position] = element;

// Increment the size of the array

(*size)++;

int main() {

int array[20] = {1, 3, 5, 7, 9};

int size = 5; // Current size of the array

int newPosition = 2; // Position where the element will be inserted

int newElement = 4; // Element to be inserted

printf("Original Array: ");

for (int i = 0; i < size; i++) {

printf("%d ", array[i]);

printf("\n");

// Insert the new element at the specified position

insertElement(array, &size, newPosition, newElement);

printf("Array after insertion: ");

for (int i = 0; i < size; i++) {

printf("%d ", array[i]);

printf("\n");

return 0;

 Write a C program to find duplicate element in an array

#include <stdio.h>

void findDuplicates(int arr[], int size) {

printf("Duplicate elements in the array: ");


for (int i = 0; i < size; i++) {

for (int j = i + 1; j < size; j++) {

// If a duplicate is found

if (arr[i] == arr[j]) {

printf("%d ", arr[i]);

break; // Breaks to avoid printing the same duplicate multiple times

printf("\n");

int main() {

int array[] = {1, 2, 3, 4, 2, 7, 8, 8, 3};

int size = sizeof(array) / sizeof(array[0]);

printf("Original Array: ");

for (int i = 0; i < size; i++) {

printf("%d ", array[i]);

printf("\n");

findDuplicates(array, size);

return 0;

Write a C program to search element in given list using linear search . also find largest element in given array.
#include <stdio.h>

void findDuplicates(int arr[], int size) {

printf("Duplicate elements in the array: ");

for (int i = 0; i < size; i++) {

for (int j = i + 1; j < size; j++) {

// If a duplicate is found

if (arr[i] == arr[j]) {

printf("%d ", arr[i]);

break; // Breaks to avoid printing the same duplicate multiple times

printf("\n");
}

int main() {

int array[] = {1, 2, 3, 4, 2, 7, 8, 8, 3};

int size = sizeof(array) / sizeof(array[0]);

printf("Original Array: ");

for (int i = 0; i < size; i++) {

printf("%d ", array[i]);

printf("\n");

findDuplicates(array, size);

return 0;

Write a C program for sorting list of elements using


selection sort
#include <stdio.h>

// Function to perform selection sort

void selectionSort(int arr[], int size) {

int i, j, minIndex, temp;

for (i = 0; i < size - 1; i++) {

// Find the minimum element in the unsorted part of the array

minIndex = i;

for (j = i + 1; j < size; j++) {

if (arr[j] < arr[minIndex]) {

minIndex = j;

// Swap the found minimum element with the first element

temp = arr[minIndex];

arr[minIndex] = arr[i];

arr[i] = temp;

int main() {
int array[] = {64, 25, 12, 22, 11};

int size = sizeof(array) / sizeof(array[0]);

printf("Original Array: ");

for (int i = 0; i < size; i++) {

printf("%d ", array[i]);

printf("\n");

// Perform selection sort

selectionSort(array, size);

printf("Sorted Array: ");

for (int i = 0; i < size; i++) {

printf("%d ", array[i]);

printf("\n");

return 0;

Write a C program for sorting list of elements using


bubble sort.
#include <stdio.h>

// Function to perform bubble sort

void bubbleSort(int arr[], int size) {

int i, j, temp;

for (i = 0; i < size - 1; i++) {

for (j = 0; j < size - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

// Swap if the elements are in the wrong order

temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

int main() {

int array[] = {64, 25, 12, 22, 11};


int size = sizeof(array) / sizeof(array[0]);

printf("Original Array: ");

for (int i = 0; i < size; i++) {

printf("%d ", array[i]);

printf("\n");

// Perform bubble sort

bubbleSort(array, size);

printf("Sorted Array: ");

for (int i = 0; i < size; i++) {

printf("%d ", array[i]);

printf("\n");

return 0;

Write a C program for swapping of two string


#include <stdio.h>

#include <string.h>

// Function to swap two strings

void swapStrings(char **str1, char **str2) {

char *temp = *str1;

*str1 = *str2;

*str2 = temp;

int main() {

char *string1 = "Hello";

char *string2 = "World";

printf("Before swapping:\n");

printf("String 1: %s\n", string1);

printf("String 2: %s\n", string2);

// Swapping strings

swapStrings(&string1, &string2);

printf("\nAfter swapping:\n");
printf("String 1: %s\n", string1);

printf("String 2: %s\n", string2);

return 0;

Write a c program for swapping of two arrays using


function and check if both arrays are equal or not.
Limits and numbers of both arrays must be accepted
from user at run time.
#include <stdio.h>

// Function to swap two arrays

void swapArrays(int arr1[], int arr2[], int size) {

for (int i = 0; i < size; i++) {

// Swapping elements of both arrays

int temp = arr1[i];

arr1[i] = arr2[i];

arr2[i] = temp;

// Function to check if two arrays are equal

int areEqual(int arr1[], int arr2[], int size) {

for (int i = 0; i < size; i++) {

if (arr1[i] != arr2[i]) {

return 0; // Arrays are not equal

return 1; // Arrays are equal

}
int main() {

int size;

printf("Enter the size of arrays: ");

scanf("%d", &size);

int array1[size], array2[size];

printf("Enter elements for array 1: ");

for (int i = 0; i < size; i++) {

scanf("%d", &array1[i]);

printf("Enter elements for array 2: ");

for (int i = 0; i < size; i++) {

scanf("%d", &array2[i]);

printf("\nBefore swapping:\n");

printf("Array 1: ");

for (int i = 0; i < size; i++) {

printf("%d ", array1[i]);

printf("\nArray 2: ");

for (int i = 0; i < size; i++) {

printf("%d ", array2[i]);

// Swap arrays

swapArrays(array1, array2, size);

printf("\n\nAfter swapping:\n");

printf("Array 1: ");
for (int i = 0; i < size; i++) {

printf("%d ", array1[i]);

printf("\nArray 2: ");

for (int i = 0; i < size; i++) {

printf("%d ", array2[i]);

// Check if arrays are equal after swapping

if (areEqual(array1, array2, size)) {

printf("\n\nBoth arrays are equal after swapping.\n");

} else {

printf("\n\nArrays are not equal after swapping.\n");

return 0;

1. Write a C program to insert a given number in the


array at given position.
2. Write a C program to remove a number in the array
from a given position.

#include <stdio.h>

#define MAX_SIZE 100

void insertElement(int arr[], int *size, int position, int element) {

if (position < 0 || position > *size) {

printf("Invalid position.\n");

return;

}
// Shift elements to the right to make space for the new element

for (int i = *size - 1; i >= position; i--) {

arr[i + 1] = arr[i];

// Insert the element at the specified position

arr[position] = element;

// Increment the size of the array

(*size)++;

int main() {

int array[MAX_SIZE] = {1, 3, 5, 7, 9};

int size = 5; // Current size of the array

int newPosition = 2; // Position where the element will be inserted

int newElement = 4; // Element to be inserted

printf("Original Array: ");

for (int i = 0; i < size; i++) {

printf("%d ", array[i]);

printf("\n");

// Insert the new element at the specified position

insertElement(array, &size, newPosition, newElement);

printf("Array after insertion: ");

for (int i = 0; i < size; i++) {

printf("%d ", array[i]);

}
printf("\n");

return 0;

#include <stdio.h>

#define MAX_SIZE 100

void removeElement(int arr[], int *size, int position) {

if (position < 0 || position >= *size) {

printf("Invalid position.\n");

return;

// Shift elements to the left to remove the element at the specified position

for (int i = position; i < *size - 1; i++) {

arr[i] = arr[i + 1];

// Decrement the size of the array

(*size)--;

int main() {

int array[MAX_SIZE] = {1, 3, 5, 7, 9};

int size = 5; // Current size of the array

int removePosition = 2; // Position from which the element will be removed

printf("Original Array: ");

for (int i = 0; i < size; i++) {

printf("%d ", array[i]);


}

printf("\n");

// Remove the element at the specified position

removeElement(array, &size, removePosition);

printf("Array after removal: ");

for (int i = 0; i < size; i++) {

printf("%d ", array[i]);

printf("\n");

return 0;

Write a C program to accept two matrices and check if


they are equal or not. Order of both matrices must be
accepted from user at run time.
#include <stdio.h>

#define MAX_SIZE 10

// Function to accept matrix elements from the user

void inputMatrix(int matrix[MAX_SIZE][MAX_SIZE], int rows, int columns) {

printf("Enter elements for the matrix:\n");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

printf("Enter element at position (%d, %d): ", i + 1, j + 1);

scanf("%d", &matrix[i][j]);

}
}

// Function to check if two matrices are equal

int areMatricesEqual(int matrix1[MAX_SIZE][MAX_SIZE], int matrix2[MAX_SIZE][MAX_SIZE], int rows, int columns) {

for (int i = 0; i < rows; i++) {

for (int j = 0; j < columns; j++) {

if (matrix1[i][j] != matrix2[i][j]) {

return 0; // Matrices are not equal

return 1; // Matrices are equal

int main() {

int rows, columns;

printf("Enter the number of rows for both matrices: ");

scanf("%d", &rows);

printf("Enter the number of columns for both matrices: ");

scanf("%d", &columns);

int matrix1[MAX_SIZE][MAX_SIZE], matrix2[MAX_SIZE][MAX_SIZE];

printf("\nEnter elements for matrix 1:\n");

inputMatrix(matrix1, rows, columns);

printf("\nEnter elements for matrix 2:\n");

inputMatrix(matrix2, rows, columns);

// Check if matrices are equal


if (areMatricesEqual(matrix1, matrix2, rows, columns)) {

printf("\nBoth matrices are equal.\n");

} else {

printf("\nMatrices are not equal.\n");

return 0;

1. Write a Program to print the output like:

A
A B
A B C
A B C D
A B C D E
A B C D
A B C
A B
A
2. Write a program to print factorial of 1 to 10 numbers.

#include <stdio.h>

void printPattern(int n) {

for (int i = 1; i <= n * 2 - 1; i++) {

int limit = i <= n ? i : n * 2 - i;

char c = 'A';

for (int j = 1; j <= limit; j++) {

printf("%c ", c++);

printf("\n");

int main() {

int n = 5; // Number of rows for the pattern


printPattern(n);

return 0;

#include <stdio.h>

// Function to calculate factorial of a number

unsigned long long factorial(int n) {

if (n == 0 || n == 1) {

return 1;

} else {

return n * factorial(n - 1);

int main() {

int i;

printf("Factorials of numbers from 1 to 10:\n");

for (i = 1; i <= 10; i++) {

printf("Factorial of %d = %llu\n", i, factorial(i));

return 0;

1. Write a Program to print the output like:

1
12
123
12345
E D C B A
E D C B
E D C
E D
E
2. Write a program to print prime numbers between two
numbers given by user.
#include <stdio.h>

void printPattern(int n) {

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= i; j++) {

printf("%d ", j);

printf("\n");

for (int i = n; i >= 1; i--) {

for (int j = 1; j <= i; j++) {

printf("%c ", 'E' - j);

printf("\n");

int main() {

int n = 5; // Number of rows for the pattern

printPattern(n);

return 0;

#include <stdio.h>
#include <stdbool.h>

bool isPrime(int num) {

if (num <= 1) {

return false;

for (int i = 2; i * i <= num; i++) {

if (num % i == 0) {

return false;

return true;

void printPrimes(int start, int end) {

printf("Prime numbers between %d and %d are:\n", start, end);

for (int i = start; i <= end; i++) {

if (isPrime(i)) {

printf("%d ", i);

printf("\n");

int main() {

int start, end;

printf("Enter the range (start and end): ");

scanf("%d %d", &start, &end);

printPrimes(start, end);

return 0;
}

Write a C program to count the number of Vowels and Consonants.

Test Cases: String: C is a programming language.

Vowels: 9

Consonants: 14

#include <stdio.h>

#include <ctype.h>

void countVowelsConsonants(char *string, int *vowels, int *consonants) {

*vowels = 0;

*consonants = 0;

while (*string != '\0') {

char ch = toupper(*string);

if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {

(*vowels)++;

} else if (ch >= 'A' && ch <= 'Z') {

(*consonants)++;

string++;

int main() {
char inputString[] = "C is a programming language.";

int vowels, consonants;

countVowelsConsonants(inputString, &vowels, &consonants);

printf("String: %s\n", inputString);

printf("Vowels: %d\n", vowels);

printf("Consonants: %d\n", consonants);

return 0;

Write a program to convert decimal to binary number.


Test Cases:
Input : 5
Output: 101

#include <stdio.h>

void decimalToBinary(int decimal) {

int binary[32]; // Array to store binary digits

int i = 0;

while (decimal > 0) {

binary[i] = decimal % 2;

decimal = decimal / 2;

i++;

printf("Binary representation: ");

for (int j = i - 1; j >= 0; j--) {

printf("%d", binary[j]);

printf("\n");
}

int main() {

int decimalNumber;

printf("Enter a decimal number: ");

scanf("%d", &decimalNumber);

decimalToBinary(decimalNumber);

return 0;

Write a C program to reverse an array using


pointers.
#include <stdio.h>

void reverseArray(int *arr, int size) {


int *start = arr; // Pointer to the start of the array
int *end = arr + size - 1; // Pointer to the end of the array

// Swap elements using pointers


while (start < end) {
int temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}

int main() {
int array[] = {1, 2, 3, 4, 5};
int size = sizeof(array) / sizeof(array[0]);

printf("Original Array: ");


for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");

// Reverse the array using pointers


reverseArray(array, size);

printf("Reversed Array: ");


for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");

return 0;
}

You might also like