BINARY SEARCH
#include <stdio.h>
int main() {
int n, target, low, high, mid, found = 0;
// Input the size of the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
// Input the array elements
printf("Enter %d elements in sorted order: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Input the target element to search
printf("Enter the number to search: ");
scanf("%d", &target);
low = 0;
high = n - 1;
// Perform binary search
while (low <= high) {
mid = low + (high - low) / 2;
if (arr[mid] == target) {
printf("Element found at index %d\n", mid);
found = 1;
break;
}
if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
if (!found) {
printf("Element not found in the array.\n");
}
return 0;
}
BUBBLE SORT
#include<stdio.h>
int main(){
int n,temp;
printf("Enter the number of elements:");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for(int i=0;i<n;i++){
scanf("%d", &arr[i]);
}
//for the bubble sort okay naa
for(int i=0; i< n-1; i++){//for number of passes
for(int j=0; j< n-i-1; j++){//for number of internal swapping
if(arr[j]>arr[j+1]){
temp= arr[j];
arr[j]= arr[j + 1];
arr[j+1] = temp;
}
}
}
printf("sorted array: ");
for( int i=0; i<n; i++){
printf(" \n%d \n", arr[i]);
}
return 0;
}
STUDENTS MARKS ANDALL
#include <stdio.h>
struct student {
char usn[10];
char name[10];
int m1, m2, m3;
float avg, total;
};
void main() {
struct student s[20];
int n, i;
// Input the number of students
printf("Enter the number of students: ");
scanf("%d", &n);
// Input student details
for (i = 0; i < n; i++) {
printf("\nEnter details of student %d:\n", i + 1);
printf("Enter USN: ");
scanf("%s", s[i].usn);
printf("Enter Name: ");
scanf("%s", s[i].name);
printf("Enter marks of 3 subjects: ");
scanf("%d %d %d", &s[i].m1, &s[i].m2, &s[i].m3);
// Calculate total and average
s[i].total = s[i].m1 + s[i].m2 + s[i].m3;
s[i].avg = s[i].total / 3.0;
}
// Output results
printf("\nResults:\n");
for (i = 0; i < n; i++) {
if (s[i].avg >= 35) {
printf("%s has scored above average\n", s[i].name);
} else {
printf("%s has scored below average\n", s[i].name);
}
}
}
PATTERN PRINTING
#include <stdio.h>
int main() {
int n = 3;
// First outer loop to iterator through each row
for (int i = 0; i < 2 * n - 1; i++) {
// Assigning values to the comparator according to
// the row number
int comp;
if (i < n) comp = 2 * (n - i) - 1;
else comp = 2 * (i - n + 1) + 1;
// First inner loop to print leading whitespaces
for (int j = 0; j < comp; j++)
printf(" ");
// Second inner loop to print stars *
for (int k = 0; k < 2 * n - comp; k++) {
printf("%d ", k + 1);
}
printf("\n");
}
return 0;
}