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

Polynomial Operations in C

This document contains code for two algorithms: polynomial operations and binary search. The polynomial operations code allows a user to enter coefficients for a polynomial up to a specified order, calculates the value of the polynomial for a given value of x, and prints the polynomial expression. The binary search code implements a recursive binary search algorithm to search for a target value in a sorted array and returns its index if found or -1 if not found. It provides a sample sorted array and target value to test the binary search function.

Uploaded by

Lavanya Sinha
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)
52 views4 pages

Polynomial Operations in C

This document contains code for two algorithms: polynomial operations and binary search. The polynomial operations code allows a user to enter coefficients for a polynomial up to a specified order, calculates the value of the polynomial for a given value of x, and prints the polynomial expression. The binary search code implements a recursive binary search algorithm to search for a target value in a sorted array and returns its index if found or -1 if not found. It provides a sample sorted array and target value to test the binary search function.

Uploaded by

Lavanya Sinha
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/ 4

Name: Lavanya Sinha

PRN: 21070122086
CSB-1

1. INPUT

Polynomial operations

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10

void main()
{
int array[MAXSIZE];
int i, num, power;
float x, polySum;

printf("Enter the order of the polynomial \n");


scanf("%d", &num);
printf("Enter the value of x \n");
scanf("%f", &x);
/* Read the coefficients into an array */
printf("Enter %d coefficients \n", num + 1);
for (i = 0; i <= num; i++)
{
scanf("%d", &array[i]);
}
polySum = array[0];
for (i = 1; i <= num; i++)
{
polySum = polySum * x + array[i];
}
power = num;

printf("Given polynomial is: \n");


for (i = 0; i <= num; i++)
{
if (power < 0)
{
break;
}
/* printing proper polynomial function */
if (array[i] > 0)
printf(" + ");
else if (array[i] < 0)
printf(" - ");
else
printf(" ");
printf("%dx^%d ", abs(array[i]), power--);
}
printf("\n Sum of the polynomial = %6.2f \n", polySum);}
OUTPUT
2. INPUT
Binary search

#include <stdio.h>

int binarySearch(int array[], int x, int low, int high) {


// Repeat until the pointers low and high meet each other
while (low <= high) {
int mid = low + (high - low) / 2;

if (array[mid] == x)
return mid;

if (array[mid] < x)
low = mid + 1;

else
high = mid - 1;
}

return -1;
}

int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array) / sizeof(array[0]);
int x = 4;
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
return 0;
}

OUTPUT

You might also like