write a code in c to find decimal to binary




Here is a C program that converts a decimal number to its binary equivalent:

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

int main() { int decimal, binary = 0, i = 1; printf("Enter a decimal number: "); scanf("%d", &decimal);

while (decimal != 0) { binary += (decimal % 2) * i; decimal /= 2; i *= 10; } printf("Binary equivalent: %d\n", binary); return 0;

}

This program prompts the user to enter a decimal number, and then it converts the decimal number to its binary equivalent. The binary equivalent is stored in the "binary" variable and is printed to the screen.

This program works by using a loop to repeatedly divide the decimal number by 2 and storing the remainder in the "binary" variable. The loop continues until the decimal number becomes 0, at which point the binary equivalent has been computed.

I hope this helps! Let me know if you have any questions or need further assistance.



#include <stdio.h> int main(void) { int decimal; printf("Enter a decimal number: "); scanf("%d", &decimal); int binary[32]; int i = 0; while (decimal > 0) { binary[i] = decimal % 2; decimal /= 2; i++; } printf("Binary representation: "); for (int j = i - 1; j >= 0; j--) { printf("%d", binary[j]); } printf("\n"); return 0; }


ABHISHEK ANAND

Open Source Contributor | MERN Stack Developer | JavaScript, MongoDB, Express, React, Node | Learned C & Web development | After that- C++,DSA,Java,Golang | Learner

एक टिप्पणी भेजें

और नया पुराने