0% found this document useful (0 votes)
4 views

Code

The document contains three C programs. The first program finds the largest number in a user-defined list of integers, the second calculates the average marks of students, and the third implements linear and binary search algorithms along with sorting an array. Each program prompts the user for input and displays the results accordingly.

Uploaded by

antillucifer765
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Code

The document contains three C programs. The first program finds the largest number in a user-defined list of integers, the second calculates the average marks of students, and the third implements linear and binary search algorithms along with sorting an array. Each program prompts the user for input and displays the results accordingly.

Uploaded by

antillucifer765
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Code

#include <stdio.h>

int main() {

int n, i;

printf("Enter the number of components in your list: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d components:\n", n);

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

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

int largest = arr[0];

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

if(arr[i] > largest) {

largest = arr[i];

printf("Largest number in array is: %d\n", largest);

return 0;

}
Code
#include <stdio.h>

int main() {

int n, i;

printf("Enter the number of STUDENTS:\n");

scanf("%d", &n);

int arr[n];

printf("Enter %d MARKS of students:\n", n);

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

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

float sum = 0;

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

sum = sum + arr[i];

sum = sum / n;

printf("Average of students' marks is: %.4f\n", sum);

return 0;

}
Code
#include <stdio.h>

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

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

if (arr[i] == target)

return i;

return -1;

int binarySearch(int arr[], int n, int target) {

int low = 0, high = n - 1;

while (low <= high) {

int mid = (low + high) / 2;

if (arr[mid] == target)

return mid;

else if (arr[mid] < target)

low = mid + 1;

else

high = mid - 1;

return -1;
}

void sortArray(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;

int main() {

int arr[100], n, target;

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

scanf("%d", &n);

printf("Enter %d elements:\n", n);

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

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

printf("Enter element to search: ");

scanf("%d", &target);
int linIndex = linearSearch(arr, n, target);

if (linIndex != -1)

printf("Linear Search: Element found at index %d\n", linIndex);

else

printf("Linear Search: Element not found\n");

sortArray(arr, n);

int binIndex = binarySearch(arr, n, target);

if (binIndex != -1)

printf("Binary Search: Element found at index %d (in sorted


array)\n", binIndex);

else

printf("Binary Search: Element not found\n");

return 0;

You might also like