Practical:-01: Aim: Software Used: Description

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Varun Kumar 2323031

Practical:-01
Aim: Write a program to find the factorial of a number.
Software used: CodeBlocks.
Description: This program calculates the factorial of a non-negative integer nnn by
multiplying all integers from 1 up to nnn. It uses a loop to perform this multiplication. If the
user enters a negative number, the program outputs a message that factorials are not defined
for negative numbers.
Source Code:
#include <stdio.h>
int factorial(int n) {
if (n < 0)
return -1; // Return -1 to indicate an error for negative numbers
unsigned long long result = 1;
for (int i = 1; i <= n; ++i) {
result *= i;
}
return result;
}int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number < 0)
printf("Factorial does not exist for negative numbers.\n");
else
printf("Factorial of %d is %llu\n", number, factorial(number));
return 0;
}
Output:
Varun Kumar 2323031

Practical:-02
Aim: Write a program to check the number is prime or not.
Description: A prime number is a natural number greater than 1 that has no divisors other
than 1 and itself. This program checks if a given number is prime by iterating from 2 to the
square root of the number and checking for any divisors.
Software Used: CodeBlocks.
Source Code:
#include <stdio.h>
int isPrime(int n) {
if (n <= 1)
return 0; // Numbers less than or equal to 1 are not prime
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0)
return 0; // If divisible by any number other than 1 and itself, it's not prime
}
return 1; // No divisors found, so it's prime
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (isPrime(number))
printf("%d is a prime number.\n", number);
else
printf("%d is not a prime number.\n", number);
return 0;
}
Output:
Varun Kumar 2323031

Practical:-03
Aim: To find the given element in an array using LinearSearch.
Description: In linear search, each element of the array is sequentially checked until the
desired element is found or the end of the array is reached. This program takes an array and a
target number as input, then searches for the target in the array.
Software Used: CodeBlocks
Source Code:
#include <stdio.h>

int linearSearch(int arr[], int size, int target) {

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


if (arr[i] == target)
return i; // Return the index if target is found
}

return -1; // Return -1 if target is not found


}
int main() {
int n, target;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the number to search for: ");


scanf("%d", &target);

int result = linearSearch(arr, n, target);

if (result != -1)
printf("Number %d found at index %d.\n", target, result);
else
printf("Number %d not found in the array.\n", target);

return 0;
Varun Kumar 2323031

}
Output:
Varun Kumar 2323031

Practical:-04
Aim: Write a program to find the element in the array using BinarySearch.
Description: Binary search is a fast search algorithm that works only on sorted arrays. The
array is divided in half repeatedly, comparing the target with the middle element each time. If
the middle element is not the target, the algorithm discards the half where the target cannot
be located, making the search more efficient.
Software Used: CodeBlocks
Source Code:
#include <stdio.h>
int binarySearch(int arr[], int size, int target) {
int left = 0, right = size - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
// Check if the target is at the middle
if (arr[mid] == target)
return mid;
// If the target is greater, ignore the left half
if (arr[mid] < target)
left = mid + 1;
else // If the target is smaller, ignore the right half
right = mid - 1;
}
return -1; // Target not found
}
int main() {
int n, target;
printf("Enter the number of elements in the sorted array: ");
scanf("%d", &n);

int arr[n];
printf("Enter %d sorted elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the number to search for: ");
scanf("%d", &target);
int result = binarySearch(arr, n, target);
if (result != -1)
printf("Number %d found at index %d.\n", target, result);
Varun Kumar 2323031

else {
printf("Number %d not found in the array.\n", target);
return 0;
}
Output:
Varun Kumar 2323031

Practical:-05
Aim: Write a program for insertion sort, selection sort and bubble sort.
Description:
 Insertion Sort:
In Insertion Sort, elements are picked from an unsorted part and placed in the
correct position in the sorted part.
 Selection Sort:
In Selection Sort, the array is divided into a sorted and an unsorted part. The
minimum element from the unsorted part is selected and swapped with the first
element of the unsorted part.
 Bubble Sort:
In Bubble Sort, adjacent elements are repeatedly swapped if they are in the
wrong order, and this process is repeated for every element.
Software Used: CodeBlocks.
Source Code:

Insertion Sort:-
#include <stdio.h>
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printf("Sorted array using Insertion Sort: ");
Varun Kumar 2323031

printArray(arr, n);
return 0;
}

Output:

Selection Sort:-
#include <stdio.h>

void selectionSort(int arr[], int n) {


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

void printArray(int arr[], int n) {


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

int main() {
Varun Kumar 2323031

int arr[] = {64, 25, 12, 22, 11};


int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array using Selection Sort: ");
printArray(arr, n);
return 0;
}
Output:

Bubble Sort:
#include <stdio.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
Varun Kumar 2323031

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array using Bubble Sort: ");
printArray(arr, n);
return 0;
}

Output:

You might also like