0% found this document useful (0 votes)
3 views8 pages

day 4

The document contains multiple C programs that demonstrate various programming tasks, including finding the largest and smallest elements in an integer array, retrieving the 5th character from a string, transposing a 4x4 matrix, sorting an array in ascending and descending order, and generating the first 10 elements of a Fibonacci series. Each program is provided with its respective code and comments explaining its functionality. Additionally, a flowchart is included for the descending order sorting program.

Uploaded by

linconclay02
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)
3 views8 pages

day 4

The document contains multiple C programs that demonstrate various programming tasks, including finding the largest and smallest elements in an integer array, retrieving the 5th character from a string, transposing a 4x4 matrix, sorting an array in ascending and descending order, and generating the first 10 elements of a Fibonacci series. Each program is provided with its respective code and comments explaining its functionality. Additionally, a flowchart is included for the descending order sorting program.

Uploaded by

linconclay02
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/ 8

1.

Write a C program to find the largest element from


an integer array

#include<stdio.h>
#include<limits.h>
int main(){
int arr[]={2,4,6,8,10};
int size = sizeof(arr)/sizeof(arr[0]);
int max = INT_MIN;
for(int i=0; i<size; i++){
if(max<arr[i]){
max = arr[i];
}
}
printf("Max element in the array is %d",max);
return 0;
}
2. Write a C program to find the 5th character from a
15-length character string

#include<stdio.h>
#include<limits.h>
int main(){
char string[15];
printf("Enter any string of 15 characters : ");
scanf("%s",string);
printf("The 5th element of the string is %c",string[4]);
return 0;
}
3. Write a C program to find the transpose of a 4X4
Matrix
#include<stdio.h>
int main(){
int arr[4][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
int trans[4][4];
printf("Matrix is ->\n");
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
printf("%d ",arr[i][j]);
}
printf("\n"); }
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
trans[j][i]=arr[i][j];
} }
printf("Transpose Matrix is ->\n");
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
printf("%d ",trans[i][j]);
}
printf("\n"); }
return 0;
}
4. Write a c program to sort the elements of an integer
array in ascending order

#include<stdio.h>
int main(){
int arr[]={45,6,73,23,10,55};
int size = sizeof(arr)/sizeof(arr[0]);
printf("The inputted array is : \n");
for(int i=0;i<size;i++){
printf("%d\t",arr[i]);
}
printf("\n");
for(int i=0; i<size-1; i++){
for(int j=0; j<size-i-1; j++){
if(arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("The sorted array is : \n");
for(int i=0;i<size;i++){
printf("%d\t",arr[i]);
}
return 0;
}
5. Write a C program to Generate & print first 10
elements of a Fibonacci series

#include<stdio.h>
int fib(int n ){
if(n == 1){
return 0;
}
if(n==2){
return 1;
}
int fib_N1=fib(n-1);
int fib_N2=fib(n-2);
int fib_N = fib_N1 + fib_N2;
return fib_N;
}
int main(){
for(int i=1;i<=10;i++){
printf("%d\t",fib(i));
}
return 0;
}
7. Write a C program to find the smallest element
from an integer array

#include<stdio.h>
#include<limits.h>
int main(){
int arr[]={30,22,7,15,18};
int size = sizeof(arr)/sizeof(arr[0]);
int min = INT_MAX;
for(int i=0; i<size; i++){
if(min>arr[i]){
min = arr[i];
}
}
printf("Smallest element in the array is %d",min);
return 0;
}
8. Write a C program with flowchart to sort an integer
array in descending order

#include<stdio.h>
int main(){
int arr[]={24,50,15,38,44,17,30};
int size = sizeof(arr)/sizeof(arr[0]);
printf("The inputted array is : \n");
for(int i=0;i<size;i++){
printf("%d\t",arr[i]);
}
printf("\n");
for(int i=0; i<size-1; i++){
for(int j=0; j<size-i-1; j++){
if(arr[j]<arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("The sorted array in descending order is : \n");
for(int i=0;i<size;i++){
printf("%d\t",arr[i]);
}
return 0;
}
START

int arr[size],
i=0, j=0

i++

False
True
i < size-1 j < size-i-1 j++

False True

False
arr[j] < arr[j+1]

True

int temp = arr[j]


END arr[j] = arr[j+1]
arr[j+1] = temp

You might also like