[go: up one dir, main page]

0% found this document useful (0 votes)
26 views3 pages

C Programs Collection

The document contains four C programs: one to find the largest element in an array, another to sort an array in descending order, a third to compute the transpose of a matrix, and a fourth to determine the length of a string. Each program prompts the user for input, processes the data accordingly, and outputs the result. These examples demonstrate basic operations on arrays, matrices, and strings in C programming.
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)
26 views3 pages

C Programs Collection

The document contains four C programs: one to find the largest element in an array, another to sort an array in descending order, a third to compute the transpose of a matrix, and a fourth to determine the length of a string. Each program prompts the user for input, processes the data accordingly, and outputs the result. These examples demonstrate basic operations on arrays, matrices, and strings in C programming.
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
You are on page 1/ 3

Find Largest Element in Array

#include <stdio.h>

int main() {
int n, i, largest;

printf("Enter the number of elements in the array: ");


scanf("%d", &n);

int arr[n];

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


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

largest = arr[0];

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


if(arr[i] > largest) {
largest = arr[i];
}
}

printf("The largest element in the array is: %d\n", largest);

return 0;
}

Sort Array in Descending Order


#include <stdio.h>

int main() {
int n, i, j, temp;

printf("Enter the number of elements in the array: ");


scanf("%d", &n);

int arr[n];

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


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

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


for(j = 0; j < n - i - 1; j++) {
if(arr[j] < arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

printf("Array elements in descending order:\n");


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

return 0;
}

Transpose of a Matrix
#include <stdio.h>

int main() {
int rows, cols, i, j;

printf("Enter number of rows: ");


scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);

int matrix[rows][cols], transpose[cols][rows];

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


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

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


for(j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}

printf("Original Matrix:\n");
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}

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


for(i = 0; i < cols; i++) {
for(j = 0; j < rows; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}

Length of a String (Simple)


#include <stdio.h>

int main() {
char str[100];
int i = 0;

printf("Enter a string: ");


scanf("%s", str);

while(str[i] != '\0') {
i++;
}

printf("Length of the string: %d\n", i);

return 0;
}

You might also like