Sunday, January 13, 2019

A decimal number between 0 and 32 exclusive can be expressed in binary system as x4x3x2x1x0, where xi's are either zero or one. Write a C program that accepts (from the terminal) a decimal number in the above range and prints out the equivalent binary representation with leading bit 1.

A decimal number between 0 and 32 exclusive can be expressed in binary system as x4x3x2x1x0, where xi's are either zero or one. Write a C program that accepts (from the terminal) a decimal number in the above range and prints out the equivalent binary representation with leading bit 1.


PROGRAM

#include <stdio.h>
int binary(int n);
main()
{
   int n, x;
   printf("Enter a decimal number between 0 and 32: ");
   scanf("%d", &n);
   while(n>=0 && n<=32)
   {
      
    x = binary(n);
   printf("BINARY EQUIVALENT OF DECIMAL NUMBER %d IS %d", n, x);
   printf("\nEnter a decimal number between 0 and 32: ");
   scanf("%d", &n);
}
   
   n<0||n>32?printf("Out of range"):printf("");
   
}
int binary(int n)
{
    if (n == 0)
    {
        return 0;
    }
    else
    {
        return (n % 2) + 10 * binary(n/ 2);
    }
}
EXPECTED OUTPUT

  
   

FOR ANY QUERY:nrnvcoding@gmail.com




No comments:

Post a Comment

Manhattan Distance Heuristic

#include<stdio.h> #include<stdlib.h> struct node{  int arr[10][10];  int i,j;  int md; }; int a[10][10]; struct node*...