0% found this document useful (0 votes)
8 views

C_Array_Prgs

The document contains multiple C programs that perform various operations on arrays and matrices, including inserting an element at a specific position, removing duplicates, deleting an element, checking for palindromes, finding maximum and minimum elements, counting even and odd elements, and performing matrix addition, subtraction, and multiplication. Each program includes user input for array elements and outputs the result of the operation. The programs utilize standard input/output functions and basic control structures.
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)
8 views

C_Array_Prgs

The document contains multiple C programs that perform various operations on arrays and matrices, including inserting an element at a specific position, removing duplicates, deleting an element, checking for palindromes, finding maximum and minimum elements, counting even and odd elements, and performing matrix addition, subtraction, and multiplication. Each program includes user input for array elements and outputs the result of the operation. The programs utilize standard input/output functions and basic control structures.
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/ 13

Insert an Element at a Specific Position

#include <stdio.h>
#include <conio.h>

void main() {
int arr[100], n, i, pos, value;
clrscr();

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);

printf("Enter position and value to insert: ");


scanf("%d %d", &pos, &value);

for (i = n; i >= pos; i--)


arr[i] = arr[i - 1];

arr[pos - 1] = value;
n++;

printf("Array after insertion:\n");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);

getch();
}
Remove Duplicate Items in an Array

#include <stdio.h>
#include <conio.h>

void main() {
int arr[100], n, i, j, k;
clrscr();

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter array elements:\n");


for (i = 0; i < n; i++)
scanf("%d", &arr[i]);

for (i = 0; i < n; i++) {


for (j = i + 1; j < n; ) {
if (arr[i] == arr[j]) {
for (k = j; k < n - 1; k++)
arr[k] = arr[k + 1];
n--;
} else {
j++;
}
}
}
printf("Array after removing duplicates:\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
getch();}
3. Delete Element from Specific Position

#include <stdio.h>
#include <conio.h>

void main() {
int arr[100], n, pos, i;
clrscr();

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);

printf("Enter position to delete: ");


scanf("%d", &pos);

for (i = pos - 1; i < n - 1; i++)


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

printf("Array after deletion:\n");


for (i = 0; i < n; i++)
printf("%d ", arr[i]);

getch();
}
Check if String is Palindrome (Using for Loop)

#include <stdio.h>
#include <string.h>
#include <conio.h>

void main() {
char str[100];
int i, len, flag = 1;
clrscr();

printf("Enter a string: ");


gets(str);

len = strlen(str);
for (i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1]) {
flag = 0;
break;
}
}

if (flag)
printf("Palindrome");
else
printf("Not Palindrome");

getch();
}
Print All Negative Elements

#include <stdio.h>
#include <conio.h>

void main() {
int arr[100], n, i;
clrscr();

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter array elements:\n");


for (i = 0; i < n; i++)
scanf("%d", &arr[i]);

printf("Negative elements:\n");
for (i = 0; i < n; i++) {
if (arr[i] < 0)
printf("%d ", arr[i]);
}

getch();
}

Find Maximum and Minimum Element

#include <stdio.h>
#include <conio.h>

void main() {
int arr[100], n, i, max, min;
clrscr();

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter array elements:\n");


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("Maximum = %d, Minimum = %d", max, min);

getch();
}

Second Largest Element

#include <stdio.h>
#include <conio.h>

void main() {
int arr[100], n, i, first, second;
clrscr();
printf("Enter number of elements: ");
scanf("%d", &n);

printf("Enter array:\n");
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);

first = second = -32768;


for (i = 0; i < n; i++) {
if (arr[i] > first) {
second = first;
first = arr[i];
} else if (arr[i] > second && arr[i] != first) {
second = arr[i];
}
}

if (second == -32768)
printf("No second largest element");
else
printf("Second largest = %d", second);

getch();
}

Count Even and Odd Elements

#include <stdio.h>
#include <conio.h>

void main() {
int arr[100], n, i, even = 0, odd = 0;
clrscr();

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter array elements:\n");


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
if (arr[i] % 2 == 0)
even++;
else
odd++;
}

printf("Even = %d, Odd = %d", even, odd);


getch();
}

Count Negative Elements

#include <stdio.h>
#include <conio.h>

void main() {
int arr[100], n, i, count = 0;
clrscr();

printf("Enter number of elements: ");


scanf("%d", &n);
printf("Enter array elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
if (arr[i] < 0)
count++;
}

printf("Total negative elements: %d", count);


getch();
}

Search Element in Array

#include <stdio.h>
#include <conio.h>

void main() {
int arr[100], n, i, key, found = 0;
clrscr();

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter array elements:\n");


for (i = 0; i < n; i++)
scanf("%d", &arr[i]);

printf("Enter element to search: ");


scanf("%d", &key);
for (i = 0; i < n; i++) {
if (arr[i] == key) {
found = 1;
break;
}
}

if (found)
printf("Element found at position %d", i + 1);
else
printf("Element not found");

getch();
}

Add Two Matrices

#include <stdio.h>
#include <conio.h>

void main() {
int a[10][10], b[10][10], sum[10][10], i, j, r, c;
clrscr();

printf("Enter rows and columns: ");


scanf("%d%d", &r, &c);

printf("Enter first matrix:\n");


for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
scanf("%d", &a[i][j]);
printf("Enter second matrix:\n");
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
scanf("%d", &b[i][j]);

printf("Sum matrix:\n");
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
sum[i][j] = a[i][j] + b[i][j];
printf("%d ", sum[i][j]);
}
printf("\n");
}

getch();
}

Subtract Two Matrices

Change sum[i][j] = a[i][j] + b[i][j]; to a[i][j] - b[i][j];.

Multiply Two Matrices

c
CopyEdit
#include <stdio.h>
#include <conio.h>

void main() {
int a[10][10], b[10][10], mul[10][10], i, j, k, r1, c1, r2, c2;
clrscr();

printf("Enter rows and columns of first matrix: ");


scanf("%d %d", &r1, &c1);

printf("Enter rows and columns of second matrix: ");


scanf("%d %d", &r2, &c2);

if (c1 != r2) {
printf("Multiplication not possible");
getch();
return;
}

printf("Enter first matrix:\n");


for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
scanf("%d", &a[i][j]);

printf("Enter second matrix:\n");


for (i = 0; i < r2; i++)
for (j = 0; j < c2; j++)
scanf("%d", &b[i][j]);

for (i = 0; i < r1; i++)


for (j = 0; j < c2; j++) {
mul[i][j] = 0;
for (k = 0; k < c1; k++)
mul[i][j] += a[i][k] * b[k][j];
}
printf("Product matrix:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++)
printf("%d ", mul[i][j]);
printf("\n");
}

getch();
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy