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

Program To Convert A Decimal Number Into Binary Number:: Stdio.h Main

The document contains code for several C programs that perform common data structure and algorithm tasks: 1) Converting between decimal, binary, and octal number systems. 2) Implementing linear search to find an element in an array. 3) Implementing binary search, an efficient search algorithm. 4) Implementing bubble sort to sort elements in an array.

Uploaded by

Quick Tornado
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

Program To Convert A Decimal Number Into Binary Number:: Stdio.h Main

The document contains code for several C programs that perform common data structure and algorithm tasks: 1) Converting between decimal, binary, and octal number systems. 2) Implementing linear search to find an element in an array. 3) Implementing binary search, an efficient search algorithm. 4) Implementing bubble sort to sort elements in an array.

Uploaded by

Quick Tornado
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Program to convert a decimal number into binary number:

/*
* C program to accept a decimal number and convert it to binary
* and count the number of 1's in the binary number
*/

#include<stdio.h>
int main()
{
//for initialize a variables
long number, dec_num, rem, base = 1, bin = 0, count = 0;
//To insert a number
printf("Insert a decimal num \n");
scanf("%ld", &number);
dec_num = number;
while(number > 0)
{
rem = number % 2;
/* To count no.of 1s */
if (rem == 1)
{
count++;
}

bin = bin + rem * base;


//number/=2;
number = number / 2;
base = base * 10;
}
//display
printf("Input num is = %d\n", dec_num);
printf("Its binary equivalent is = %ld\n", bin);
printf("Num of 1's in the binary num is = %d\n", count);
return 0;
}
Program to convert binary number to decimal number:

/** C program to convert the given binary number into decimal**/

#include<stdio.h>

int main()
{
int num, binary_val, decimal_val = 0, base = 1, rem;

printf("Insert a binary num (1s and 0s) \n");


scanf("%d", &num); /* maximum five digits */

binary_val = num;
while (num > 0)
{
rem = num % 10;
decimal_val = decimal_val + rem * base;
//num/=10;
num = num / 10 ;
//base*=2;
base = base * 2;
}
//display binary number
printf("The Binary num is = %d \n", binary_val);
//display decimal number
printf("Its decimal equivalent is = %d \n", decimal_val);
return 0;
}
C Program for Binary to Octal Conversion

/** C Program to Convert Binary to Octal*/

#include<stdio.h>

int main()

{
//For initialize variables
long int binary_num, octal_num = 0, j = 1, rem;

//Inserting the binary number from the user

printf("Enter a binary number: ");


scanf("%ld", &binary_num);

// while loop for number conversion

while(binary_num != 0)
{

rem = binary_num % 10;


octal_num = octal_num + rem * j;
//j*=2
j = j * 2;
//binary_num/10;
binary_num = binary_num / 10;

printf("Equivalent octal value: %ld", octal_num);

return 0;
}
C program for Octal to Decimal Conversion

/** C Program to Convert Octal to Decimal */

#include<stdio.h>
#include<math.h>

int main()
{
//Variable Initialization
long int oct, dec = 0;
int i = 0;
//Taking input from user
printf("Enter an octal number: ");
scanf("%ld", &oct);
//Conversion of octal to decimal
while(oct != 0)
{
dec = dec +(oct % 10)* pow(8, i++);
//oct/=10;
oct = oct / 10;
}
//display
printf("Equivalent decimal number: %ld",dec);
return 0;
}
C Program for finding number in Linear search
#include <stdio.h> //header files
int main () //main function
{
int a[10] = {10, 23, 40, 1, 2, 0, 14, 13, 50, 9};
int item, i, flag;

printf("\nEnter Item which is to be find\n");


scanf("%d",&item);

//looping logic
for (i = 0; i< 10; i++)
{
// if else condition
if(a[i] == item)
{
flag = i+1;
break;
}
else
flag = 0;
}

if(flag != 0)
{
//printing the element which is to find
printf("\nItem found at location %d\n",flag);
}
else
{
printf("\nItem not found\n");
}
return 0;
}
Binary Search in C
#include<stdio.h> //header files

int binarySearch(int[], int, int, int);

int main() //main function


{
int arr[10] = {9, 26, 33, 47, 53, 60, 75, 80, 86, 99};
int item, location = -1;

printf("Enter the item which you want to find ");


scanf("%d",&item);

location = binarySearch(arr, 0, 9, item);

if(location != -1) //looping logic


{
printf("Item found at location %d",location); //printing the element
}
else
{
printf("Item not found");
}
return 0;
}

int binarySearch(int a[], int start, int last, int item)


{
int mid;
if(last >= start)
{
mid = (start + last)/2;
if(a[mid] == item){
return mid+1;
}
else if(a[mid] < item){
return binarySearch(a,mid+1,last,item);
}
else{
return binarySearch(a,start,mid-1,item);
}
}
return -1;
}
Bubble Sort in C
#include <stdio.h>

/* Function to print array */


void display(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Main function to run the program


int main()
{
int array[] = {5, 3, 1, 9, 8, 2, 4,7};
int size = sizeof(array)/sizeof(array[0]);

printf("Before bubble sort: \n");


display(array, size);

int i, j, temp;
for (i = 0; i < size-1; i++){

// Since, after each iteration righmost i elements are sorted


for (j = 0; j < size-i-1; j++)
if (array[j] > array[j+1])
{
temp = array[j]; // swap the element
array[j] = array[j+1];
array[j+1] = temp;
}
}
printf("After bubble sort: \n");
display(array, size);
return 0;
}

You might also like