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

Binary Search

The document contains four C programs: a binary search algorithm to find an element in a sorted array, a bubble sort algorithm to sort an array of integers, a program to input and calculate the average marks of students, and a pattern printing program that generates a specific star pattern based on input. Each program includes user prompts for input and displays results accordingly. The code snippets demonstrate basic programming concepts and data structures in C.

Uploaded by

24ug1byai046
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)
7 views4 pages

Binary Search

The document contains four C programs: a binary search algorithm to find an element in a sorted array, a bubble sort algorithm to sort an array of integers, a program to input and calculate the average marks of students, and a pattern printing program that generates a specific star pattern based on input. Each program includes user prompts for input and displays results accordingly. The code snippets demonstrate basic programming concepts and data structures in C.

Uploaded by

24ug1byai046
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/ 4

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;
}

You might also like