Telegram Group & Telegram Channel
136. Binary search using recursion

#include<stdio.h>

int binary(int arr[], int n, int search, int l, int u);

int main()
{
int arr[10], i, n, search, c, l, u;

printf("Enter the size of an array : ");
scanf("%d", &n);

for (i = 0; i < n; i++)
{
printf("Enter the element %d : ", i+1);
scanf("%d", &arr[i]);
}

printf("Enter the number to be search: ");
scanf("%d", &search);

l = 0, u = n - 1;
c = binary(arr, n, search, l, u);

if (c == 0)
printf("Number not found.");
else
printf("Number found.");

return 0;
}

int binary(int arr[], int n, int search, int l, int u)
{

int mid, c = 0;

if (l <= u)
{
mid = (l + u) / 2;
if (search == arr[mid])
{
c = 1;
}
else if (search < arr[mid])
{
return binary(arr, n, search, l, mid - 1);
}
else
return binary(arr, n, search, mid + 1, u);
}
else
return c;
}
@C_Codings



tg-me.com/C_Codings/198
Create:
Last Update:

136. Binary search using recursion

#include<stdio.h>

int binary(int arr[], int n, int search, int l, int u);

int main()
{
int arr[10], i, n, search, c, l, u;

printf("Enter the size of an array : ");
scanf("%d", &n);

for (i = 0; i < n; i++)
{
printf("Enter the element %d : ", i+1);
scanf("%d", &arr[i]);
}

printf("Enter the number to be search: ");
scanf("%d", &search);

l = 0, u = n - 1;
c = binary(arr, n, search, l, u);

if (c == 0)
printf("Number not found.");
else
printf("Number found.");

return 0;
}

int binary(int arr[], int n, int search, int l, int u)
{

int mid, c = 0;

if (l <= u)
{
mid = (l + u) / 2;
if (search == arr[mid])
{
c = 1;
}
else if (search < arr[mid])
{
return binary(arr, n, search, l, mid - 1);
}
else
return binary(arr, n, search, mid + 1, u);
}
else
return c;
}
@C_Codings

BY C Language πŸ‘¨β€πŸ’»


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/C_Codings/198

View MORE
Open in Telegram


C Language ‍ Telegram | DID YOU KNOW?

Date: |

How Does Bitcoin Mining Work?

Bitcoin mining is the process of adding new transactions to the Bitcoin blockchain. It’s a tough job. People who choose to mine Bitcoin use a process called proof of work, deploying computers in a race to solve mathematical puzzles that verify transactions.To entice miners to keep racing to solve the puzzles and support the overall system, the Bitcoin code rewards miners with new Bitcoins. β€œThis is how new coins are created” and new transactions are added to the blockchain, says Okoro.

How to Invest in Bitcoin?

Like a stock, you can buy and hold Bitcoin as an investment. You can even now do so in special retirement accounts called Bitcoin IRAs. No matter where you choose to hold your Bitcoin, people’s philosophies on how to invest it vary: Some buy and hold long term, some buy and aim to sell after a price rally, and others bet on its price decreasing. Bitcoin’s price over time has experienced big price swings, going as low as $5,165 and as high as $28,990 in 2020 alone. β€œI think in some places, people might be using Bitcoin to pay for things, but the truth is that it’s an asset that looks like it’s going to be increasing in value relatively quickly for some time,” Marquez says. β€œSo why would you sell something that’s going to be worth so much more next year than it is today? The majority of people that hold it are long-term investors.”

C Language ‍ from ar


Telegram C Language πŸ‘¨β€πŸ’»
FROM USA