0% found this document useful (0 votes)
7 views7 pages

C Programming: Sorting, Searching, and Math

The document contains multiple C programming examples demonstrating various algorithms and functions, including Bubble Sort, matrix transposition, factorial calculation using recursion, binary search, string concatenation and comparison, and statistical calculations (sum, mean, standard deviation) using pointers. Each example includes user input for data and outputs the results accordingly. The code snippets illustrate fundamental programming concepts and operations in C.

Uploaded by

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

C Programming: Sorting, Searching, and Math

The document contains multiple C programming examples demonstrating various algorithms and functions, including Bubble Sort, matrix transposition, factorial calculation using recursion, binary search, string concatenation and comparison, and statistical calculations (sum, mean, standard deviation) using pointers. Each example includes user input for data and outputs the results accordingly. The code snippets illustrate fundamental programming concepts and operations in C.

Uploaded by

someshshilewad22
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Bubble Sort (Passing Array to Function)

#include <stdio.h>

void bubbleSort(int a[], int n) {


int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}

int main() {
int a[50], n, i;

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter elements: ");


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

bubbleSort(a, n);

printf("Sorted array: ");


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

return 0;
}
Transpose of a 3x3 Matrix
#include <stdio.h>

int main() {
int a[3][3], t[3][3];
int i, j;

printf("Enter 3x3 matrix elements:\n");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}

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


for (j = 0; j < 3; j++) {
t[j][i] = a[i][j];
}
}

printf("Transpose of the matrix:\n");


for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", t[i][j]);
}
printf("\n");
}

return 0;
}
Factorial Using Recursion
#include <stdio.h>

int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}

int main() {
int num;
printf("Enter a positive number: ");
scanf("%d", &num);

printf("Factorial of %d = %d", num, factorial(num));

return 0;
}
Binary Search
#include <stdio.h>

int main() {
int a[50], n, key, low, high, mid, i;

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter elements in ascending order: ");


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

printf("Enter element to search: ");


scanf("%d", &key);

low = 0;
high = n - 1;

while (low <= high) {


mid = (low + high) / 2;

if (a[mid] == key) {
printf("Element found at position %d", mid + 1);
return 0;
}
else if (key < a[mid]) {
high = mid - 1;
}
else {
low = mid + 1;
}
}

printf("Element not found");


return 0;
}
Concatenate Two Strings Without strcat()
#include <stdio.h>

int main() {
char str1[100], str2[100];
int i = 0, j = 0;

printf("Enter first string: ");


gets(str1);

printf("Enter second string: ");


gets(str2);

while (str1[i] != '\0') {


i++;
}

while (str2[j] != '\0') {


str1[i] = str2[j];
i++;
j++;
}

str1[i] = '\0';

printf("Concatenated string: %s", str1);

return 0;
}
Compare Two Strings Without strcmp()
#include <stdio.h>

int main() {
char str1[100], str2[100];
int i = 0, flag = 0;

printf("Enter first string: ");


gets(str1);

printf("Enter second string: ");


gets(str2);

while (str1[i] != '\0' && str2[i] != '\0') {


if (str1[i] != str2[i]) {
flag = 1;
break;
}
i++;
}

if (flag == 0 && str1[i] == '\0' && str2[i] == '\0') {


printf("Strings are equal");
}
else {
printf("Strings are not equal");
}

return 0;
}
Sum, Mean, and Standard Deviation Using Pointers
#include <stdio.h>
#include <math.h>

int main() {
float a[100], sum = 0.0, mean, stddev, variance = 0.0;
float *p;
int n, i;

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter %d real numbers:\n", n);


p = a;
for (i = 0; i < n; i++) {
scanf("%f", p + i);
sum += *(p + i);
}

mean = sum / n;

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


variance += pow(*(p + i) - mean, 2);
}
variance = variance / n;
stddev = sqrt(variance);

printf("Sum = %.2f\n", sum);


printf("Mean = %.2f\n", mean);
printf("Standard Deviation = %.2f\n", stddev);

return 0;
}

You might also like