Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Data Structure Problem Solution

The document contains code snippets for 7 algorithms: 1) A program to find the maximum and minimum value in an array using a for loop. 2) Linear search on an array to find a target number. 3) Binary search on a sorted array to find a target number. 4) A Fibonacci series program using recursion. 5) A factorial program using recursion. 6) Bubble sort to sort elements in an array. 7) Blank 8) Blank
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)
37 views4 pages

Data Structure Problem Solution

The document contains code snippets for 7 algorithms: 1) A program to find the maximum and minimum value in an array using a for loop. 2) Linear search on an array to find a target number. 3) Binary search on a sorted array to find a target number. 4) A Fibonacci series program using recursion. 5) A factorial program using recursion. 6) Bubble sort to sort elements in an array. 7) Blank 8) Blank
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

1.

Write a program to find the maximum and minimum from array


Code:

#include<stdio.h>

int main()
{
int arr[12]= {24,36,7,87,8,86,82,63,49,8,2,73};
int i, max, min;

max=arr[0];
min=arr[0];

for(i=1;i<12;i++)
{
if(arr[i]>max)
{
max=arr[i];
}
if(arr[i]<min)
{
min=arr[i];
}
}
printf("The max is = %d\n", max);
printf("The min is = %d", min);

return 0;

}
2. Linear search
#include<stdio.h>
int main()
{
int arr[10], i, num, loc;
printf("Enter any 10 Numbers: ");
for(i=0; i<10; i++)
{
scanf("%d", &arr[i]);
}
printf("\nEnter a Number to Search: ");
scanf("%d", &num);
for(i=0; i<10; i++)
{
if(arr[i]==num)

{
loc=i;
break;

}
}
printf("\nFound at Index No.%d", loc);

return 0;
}

3. Binary search
#include <stdio.h>
int main()
{
int i, low, high, mid, n, item, array[100];
printf("Enter number of elementsn:");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to find:");
scanf("%d", &item);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high)
{
if(array[mid] < item)
low = mid + 1;
else if (array[mid] == item)
{
printf("%d found at location %d.n", item, mid+1);
break;

}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", item);
return 0;
}
4. Fibonacci series
#include<stdio.h>
int Fibonacci(int);
int main()
{
int n, i = 0, j;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Fibonacci series\n");
for ( j = 1 ; j <= n ; j++ )
{
printf("%d\n", Fibonacci(i));
i++;
}
return 0;

}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else return ( Fibonacci(n-1) + Fibonacci(n-2) );
}

5. Factorial
#include<stdio.h>
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
int main()
{
int number;
long fact;
printf("Enter a number: ");
scanf("%d", &number);
fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
return 0;
}
6. Bubble sort
#include<stdio.h>
int main()
{
int a[10],i,j,temp,n;
printf("\n Enter the max no.of Elements to Sort: \n");
scanf("%d",&n);
printf("\n Enter the Elements : \n");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
for(j=i+1; j<n; j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
for(i=0; i<n; i++)
{
printf("%d\t",a[i]);
}
return 0;
}
7. Klkja
8.

You might also like