This is a C++ program that converts a decimal number to its binary equivalent. Let's go through the program step-by-step:
Overall, the program correctly converts the decimal number to its binary equivalent and prints the result to the console. However, the variable m is initialized to 1 but is not necessary for the conversion. The program could instead initialize bin to 0 and multiply it by 2 instead of m in step 8. Additionally, the program assumes that the user will enter a non-negative integer, so it may not work correctly for negative numbers or non-integer inputs. It's a good practice to add error checking to ensure that the program behaves correctly for all possible inputs.
#include<iostream> using namespace std; int main() { int n,r,m=1,bin=0; cout<<"\nEnter the Number : "; cin>>n; while(n!=0) { r=n%2; bin=bin+(r*m); m=m*10; n=n/2; } cout<<"\nBinary Value : "<<bin; return 0; }To download raw file Click Here
Enter the Number : 12234 Binary Value : 757986226
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions