C C++ program to convert decimal to binary

C C++ program to convert decimal to binary

Looping और iterative statements के बारे में और अधिक जानने के लिए देखें— Looping statements in C

Procedure Oriented Programming (POP) और Object Oriented Programming (OOP) में अंतर जानने के लिए देखें—POP vs OOP

Flowchart to convert decimal to binary

Decimal to binary conversion flowchart

C program to convert decimal to binary

#include<stdio.h>
int main()
{
    int decimal, binary=0, digit, base=1;
    printf("Enter decimal number: ");
    scanf("%d",&decimal);
    while(decimal!=0)
    {
        digit=decimal%2;
        binary=digit*base+binary;
        base=base*10;
        decimal=decimal/2;
    }
    printf("Binary number = %d",binary);
    return 0;
}

C++ program to convert decimal to binary

#include<iostream>
using namespace std;
int main()
{
    int decimal, binary=0, digit, base=1;
    cout<<"Enter decimal number: ";
    cin>>decimal;
    while(decimal!=0)
    {
        digit=decimal%2;
        binary=digit*base+binary;
        base=base*10;
        decimal=decimal/2;
    }
    cout<<"Binary number = "<<binary;
    return 0;
}

Output of C C++ program to convert decimal to binary

Enter decimal number: 52
Binary number = 110100
Enter decimal number: 78
Binary number = 1001110 

More iterative logic based programs in C C++

  1. Generate series of natural numbers (while, for, do while)
  2. Generate series of natural numbers and their sum
  3. Generate series of odd numbers and find their sum
  4. Generate series of even numbers and find their sum
  5. Generate table of given number
  6. Find factorial of given number
  7. Find power of given number
  8. Generate multiplication table of given number
  9. HCF and LCM of two numbers
  10. Generating Fibonacci series
  11. Checking prime number
  12. Generating series of prime numbers
  13. Sum of digits of given number
  14. Reverse of digits of given number
  15. Checking palindrome number
  16. Checking Armstrong number
  17. Generating series of Armstrong numbers
  18. Convert binary to decimal
Share it to:

Published by

admin

I am a computer teacher, programmer and web developer