Friday, May 3, 2019

Binary from decimal

  • Problem Description

    Lydia learn number conversion concept in C. Her teacher gave a homework to convert the number from decimal to binary. Help her to convert the number from decimal to binary. 

    Input should be from 0 to 127

    64 32 16 8 4 2 1
  • Test Case 1

    Input (stdin)
    1

    Expected Output
    0000001
  • Test Case 2

    Input (stdin)
    121

    Expected Output
    1111001
Solution

#include <stdio.h>

int main()
{
    long long decimal, tempDecimal, binary;
    int rem, place = 1;
    binary = 0;
    scanf("%lld", &decimal);
    tempDecimal = decimal;
    while(tempDecimal > 0)
    {
        rem = tempDecimal % 2;

        binary = (rem * place) + binary;

        tempDecimal /= 2;

        place *= 10;
    }
    printf("%lld", binary);

    return 0;
}

No comments:

Post a Comment

Parity

Problem Description Ram and Sita playing the parity game. Two types of parity are there. One is odd parity and next is even parity. Ram will...