Ads 468x60px

Friday 16 December 2011

C and C++ program to accept an integer and convert it into an equivalent binary

In this post I am going to publish a C++ program which will accept an integer from the user and print it's binay on the output screen. The coding of the program goes as follows :

// Program to convert an integer number into its equivalent binary #include "iostream.h" #include "conio.h" #include "stdlib.h" #include "stdio.h" #include "string.h" // Function to convert an integer number into binary number void integer_to_binary(int num) { long binary = 0; int decimal, X[10], d1, d2, q, r; // Convert the number into decimal d1 = num; int i = 0; while (d1 >= 2) { q = d1 / 2; r = d1 % 2; X[i] = r; d1 = q; if (d1 < 2) { i++; X[i] = d1; } else i++; } int j = i; for (i = j; i >= 0; i--) { cout << X[i]; } } main() { int num; clrscr(); cout << "Enter any integer number "; cin >> num; integer_to_binary(num); return 0; } Source: www.LearnCandCpp.com

No comments:

Post a Comment