0% found this document useful (0 votes)
6 views4 pages

C Programs: Sorting, Factorial, Search

The document contains three C programs: the first implements a bubble sort algorithm with a flag to optimize sorting, the second calculates the factorial of a given number, and the third performs a linear search to count occurrences of a target value in an array. Each program includes user input prompts and outputs results along with the author's name and identification. The programs demonstrate fundamental programming concepts in C such as loops, conditionals, and array manipulation.

Uploaded by

anshulnarware5
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)
6 views4 pages

C Programs: Sorting, Factorial, Search

The document contains three C programs: the first implements a bubble sort algorithm with a flag to optimize sorting, the second calculates the factorial of a given number, and the third performs a linear search to count occurrences of a target value in an array. Each program includes user input prompts and outputs results along with the author's name and identification. The programs demonstrate fundamental programming concepts in C such as loops, conditionals, and array manipulation.

Uploaded by

anshulnarware5
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

C Programs - Practical File

Program 1: Bubble Sort (With Flag)

#include <stdio.h>
int main()
{
int n;
printf("Enter the size of array : ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of array : ");
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

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


int swapped = 0; // Flag to check if swapping happened
for(int j = 0; j < n - 1 - i; j++) {
if(arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = 1;
}
}
if(swapped == 0) {
break; // No swapping means array is already sorted
}
}

printf("The sorted array : ");


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

printf("\nDhruv Verma \n00514815624 \nCSE-AIML");


return 0;
}
Output:
Enter the size of array : 5
Enter the elements of array : 9 3 5 1 7
The sorted array : 1 3 5 7 9
Dhruv Verma
00514815624
CSE-AIML

Program 2: Finding the Factorial

#include <stdio.h>

int main()
{
int num, fac = 1;
printf("Enter the number : ");
scanf("%d", &num);

if(num < 0) {
printf("Factorial is not defined for the negative numbers");
} else {
for(int i = num; i > 0; i--) {
fac *= i;
}
printf("The Factorial of %d is %d", num, fac);
}

printf("\nDhruv Verma \n00514815624 \nCSE-AIML");


return 0;
}

Output:
Enter the number : 5
The Factorial of 5 is 120
Dhruv Verma
00514815624
CSE-AIML
Program 3: Implement a Linear Search

#include <stdio.h>
int main()
{
int n;
printf("Enter the size of array : ");
scanf("%d", &n);
int arr[n];
int target;
int count = 0;
printf("Enter the array elements : ");
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the value of target : ");


scanf("%d", &target);

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


if(arr[i] == target) {
count++;
}
}

if(count > 0) {
printf("%d occurs %d times in the array", target, count);
} else {
printf("Element not found");
}

printf("\nDhruv Verma \n00514815624 \nCSE-AIML");


return 0;
}

Output:
Enter the size of array : 6
Enter the array elements : 4 2 3 4 1 4
Enter the value of target : 4
4 occurs 3 times in the array
Dhruv Verma
00514815624
CSE-AIML

You might also like