mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
decimal to binary without function use
This commit is contained in:
parent
ebc7892f01
commit
ebd939864e
@ -1,38 +1,25 @@
|
|||||||
/*********decimal number to binary number conversion*****************/
|
#include<stdio.h>
|
||||||
#include <stdio.h>
|
int main()
|
||||||
void decimal2Binary(long num);
|
{
|
||||||
|
int n,re,a[10000],j;
|
||||||
int main(){
|
printf("\nenter the no ");
|
||||||
|
scanf("%d",&n);
|
||||||
long num;
|
int i=0;
|
||||||
|
while(n>0)
|
||||||
printf("Enter a decimal integer \n");
|
|
||||||
scanf("%ld", &num);
|
|
||||||
decimal2Binary(num);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/***function for convert decimal numbers to binary numbers****************/
|
|
||||||
void decimal2Binary(long num){
|
|
||||||
|
|
||||||
long decimal_num, remainder, base, binary, no_of_1s;
|
|
||||||
|
|
||||||
base = 1;
|
|
||||||
binary = 0;
|
|
||||||
no_of_1s = 0;
|
|
||||||
|
|
||||||
while (num > 0)
|
|
||||||
{
|
{
|
||||||
remainder = num % 2;
|
|
||||||
|
re=n%2;
|
||||||
if (remainder == 1)
|
a[i]=re;
|
||||||
{
|
n=n/2;
|
||||||
no_of_1s++;
|
i++;
|
||||||
}
|
}
|
||||||
binary = binary + remainder * base;
|
int k;
|
||||||
num = num / 2;
|
k=i-1;
|
||||||
base = base * 10;}
|
printf("\n the number in binary is: ");
|
||||||
|
for(j=k;j>=0;j--)
|
||||||
printf("Its binary equivalent is = %ld\n", binary);
|
{
|
||||||
|
printf("%d",a[j]);
|
||||||
|
}
|
||||||
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user