You are on page 1of 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