C

C Program To Convert Decimal To Binary Using For Loop

In this tutorial, we are going to see how to write a C program to convert decimal to binary using for loop. With a decimal number as input, we need to write a program to convert the given decimal number into the equivalent binary number.

The decimal number is a 10 base number because it goes from 0 to 9, there are 10 digits in total, between 0 and 9. Any combination of digits represents a decimal number such as 23, 445, 132, 0, 2, etc.

The binary number is a 2 base number, because 0 or 1. Any combination of 0 and 1 represents a binary number such as 1001, 101, 11111, 101010, etc.
 

 

C Program To Convert Decimal To Binary Using For Loop
#include <stdio.h>  
#include <stdlib.h> 

int main(){
  
  int arr[10], nbr, i;  
  
  printf("Enter the number to convert: ");  
  scanf("%d",&nbr);  
  
  for(i=0; nbr > 0; i++)  
  {  
    arr[i] = nbr%2;  
    nbr = nbr/2;  
  } 
  
  printf("\nThe binary number is = ");
  
  for(i=i-1; i >= 0; i--)  
  {  
    printf("%d",arr[i]);  
  }  
  
  return 0;
}

Output:

Enter the number to convert: 10

The binary number is = 1010

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

One thought on “C Program To Convert Decimal To Binary Using For Loop

Leave a Reply

Your email address will not be published. Required fields are marked *