You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include<stdio.h>#include<math.h>// func prototypeintconvert(long long);
intmain()
{
long longn;
printf("Enter a binary number: ");
scanf_s("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return0;
}
// function definitionintconvert(long longn)
{
intdec=0, i=0, rem;
while (n!=0)
{
// get remainder of n divided by 10rem=n % 10;
// divide n by 10n /= 10;
// multiply rem by (2 ^ i)// add the product to decdec+=rem*pow(2, i);
// increment i++i;
}
returndec;
}
convert decimal to binary - exercise-40-2
#include<stdio.h>#include<math.h>// function prototypelong longconvert(int);
intmain()
{
intn;
long longbin;
printf("Enter a decimal number: ");
scanf_s("%d", &n);
// convert to binary using the convert() functionbin=convert(n);
printf("%d in decimal = %lld in binary", n, bin);
return0;
}
// function to convert decimal to binarylong longconvert(intn)
{
// variable to store the resultlong longbin=0;
intrem, i=1;
// loop to convert to binarywhile (n!=0)
{
// get remainder of n divided by 2rem=n % 2;
// divide n by 2n /= 2;
// multiply remainder by i// add the product to binbin+=rem*i;
// multiply i by 10i *= 10;
}
returnbin;
}