0% found this document useful (0 votes)
13 views6 pages

C Lab 1

Uploaded by

Tanishq Kaushik
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)
13 views6 pages

C Lab 1

Uploaded by

Tanishq Kaushik
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/ 6

Q1- Sorting an array in ascending order.

#include <stdio.h>
int main() {
int n, i, j, temp = 0;
printf("enter the size of array:");
scanf("%d", &n);
int arr[n];
for (i=0;i<n;i++) {
printf("enter the elements of array:");
scanf("%d", &arr[i]);
}
printf("\narray before sorting:");
for (i=0;i<n;i++){
printf("%d", arr[i]);
}
for (int i = 0; i < n-1; i++) {

for (int 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("\narray after sorting:");
for (i=0;i<n;i++) {
printf("%d", arr[i]);
}
return 0;
}

Q2- Sorting an array in descending order.


#include <stdio.h>
int main() {

Tanisqh Kaushik A2305223381 CSIT124


int n, i, j, temp = 0;
printf("enter the size of array:");
scanf("%d", &n);
int arr[n];
for (i=0;i<n;i++) {
printf("enter the elements of array:");
scanf("%d", &arr[i]);
}
printf("\narray before sorting:");
for (i=0;i<n;i++){
printf("%d", arr[i]);
}
for (int i = 0; i < n-1; i++) {

for (int 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("\narray after sorting:");
for (i=0;i<n;i++) {
printf("%d", arr[i]);
}
return 0;
}

Q3- Find max value.

#include <stdio.h>

int main() {

int n, i, j, temp = 0;

Tanisqh Kaushik A2305223381 CSIT124


printf("enter the size of array:");

scanf("%d", &n);

int arr[n];

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

printf("enter the elements of array:");

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

printf("\narray before sorting:");

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

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

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

for (int 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("\narray after sorting:");

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

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

printf("\nmax value is %d", arr[n-1]);

return 0;

Tanisqh Kaushik A2305223381 CSIT124


}

Q4- Find min value

#include <stdio.h>

int main() {

int n, i, j, temp = 0;

printf("enter the size of array:");

scanf("%d", &n);

int arr[n];

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

printf("enter the elements of array:");

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

printf("\narray before sorting:");

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

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

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

for (int 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;

Tanisqh Kaushik A2305223381 CSIT124


printf("\narray after sorting:");

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

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

printf("\nmin value is %d", arr[0]);

return 0;

Q5- Sorting array and finding middle value

#include <stdio.h>

int main() {

int n, i, j, temp = 0;

printf("enter the size of array:");

scanf("%d", &n);

int arr[n];

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

printf("enter the elements of array:");

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

printf("\narray before sorting:");

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

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

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

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

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

Tanisqh Kaushik A2305223381 CSIT124


temp = arr[j];

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

arr[j+1] = temp;

printf("\narray after sorting:");

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

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

printf("\nmiddle value is %d", arr[n/2]);

return 0;

Tanisqh Kaushik A2305223381 CSIT124

You might also like