C Program To Convert Binary Number To Decimal Number, using While Loop

Lets write a C program to convert a number from Binary number system(base 2) to Decimal number system(base 10), using while loop.

Related Read:
while loop in C programming
Calculate Sum of Digits: C Program
C Program To Reverse a Number

Note: Binary number system can be derived by base 2 to the power of whole numbers.

Example:
etc .. 24, 23, 22, 21, 20

etc .. 24 = 16, 23 = 8, 22 = 4, 21 = 2, 20 = 1.

binary decimal number system

Expected Output for the Input

User Input:
Enter a Binary Number
1110

Output:
Decimal Equivalent of 1110 is 14

Explanation:

(23 x 1) + (22 x 1) + (21 x 1) + (20 x 0 )
= 8 + 4 + 2
= 14.

Video Tutorial: C Program To Convert Binary Number To Decimal Number, using While Loop


[youtube https://www.youtube.com/watch?v=UZMuZX_gtMs]

YouTube Link: https://www.youtube.com/watch?v=UZMuZX_gtMs [Watch the Video In Full Screen.]

Logic To Convert Binary Number To Decimal Number, using While Loop

In this C program we ask the user to enter / input a binary number. Using while loop we fetch digits of that user entered number one by one. If the fetched value is 1, then we check the place value and use pow() method to place it properly in the Binary number system. i.e., if fetched digit is from position 3, then we use pow(2, 3) to calculate its decimal equivalent value i.e., 2 x 2 x 2 = 8. And then add this value to previous value of variable dec.

Source Code: C Program To Convert Binary Number To Decimal Number, using While Loop

#include < stdio.h >
#include < math.h >

int main()
{
    long num;
    int  dec = 0, rem = 0, place = 0;

    printf("Enter a Binary Number\n");
    scanf("%ld", &num);

    printf("Decimal Equivalent of %ld is ", num);
    while(num)
    {
        rem = num % 10;
        if(rem)
        {
            dec = dec + (pow(2, place) );            
        }
        num = num / 10;
        place++;
    }
    printf("%d\n", dec);

    return 0;
}

Output 1:
Enter a Binary Number
1000
Decimal Equivalent of 1000 is 8

Output 2:
Enter a Binary Number
1111
Decimal Equivalent of 1111 is 15

We can even write the program like this:

#include < stdio.h >
#include < math.h >
int main()
{
    long num;
    int  dec = 0, rem = 0, place = 0;

    printf("Enter a Binary Number\n");
    scanf("%ld", &num);

    printf("Decimal Equivalent of %ld is ", num);
    while(num)
    {
        rem = num % 10;
        dec = dec + rem * ( pow(2, place) );
        num = num / 10;
        place++;
    }
    printf("%d\n", dec);

    return 0;
}

Output 1:
Enter a Binary Number
1011
Decimal Equivalent of 1011 is 11

Output 2:
Enter a Binary Number
10011
Decimal Equivalent of 10011 is 19

Number Systems

number systems

1. Binary Number System uses base 2 and digits 01.
2. Octal Number System uses base 8 and digits 01234567.
3. Decimal Number System uses base 10 and digits 0123456789.
4. Hexadecimal Number System uses base 16 and digits 0123456789ABCDEF.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Convert Decimal Number To Binary Number, using While Loop

Lets write a C program to convert a number from Decimal number system(base 10) to Binary number system(base 2), using while loop.

Related Read:
while loop in C programming
Calculate Sum of Digits: C Program
C Program To Reverse a Number

Note: Binary number system can be derived by base 2 to the power of whole numbers.

Example:
etc .. 24, 23, 22, 21, 20

etc .. 24 = 16, 23 = 8, 22 = 4, 21 = 2, 20 = 1.

binary decimal number system

Expected Output for the Input

User Input:
Enter a decimal number
14

Output:
Binary equivalent of 14 is 1110

Explanation:
If user enters num = 14

We keep on dividing the number 14 by 2.

14 / 2 = 7, reminder 0.
07 / 2 = 3, reminder 1.
03 / 2 = 1, reminder 1.

Decimal to binary

So Binary equivalent of 14 is 1110.

OR

(23 x 1) + (22 x 1) + (21 x 1) + (20 x 0 )
= 8 + 4 + 2
= 14.

Video Tutorial: C Program To Convert Decimal Number To Binary Number, using While Loop


[youtube https://www.youtube.com/watch?v=lSn6E5pv7pc]

YouTube Link: https://www.youtube.com/watch?v=lSn6E5pv7pc [Watch the Video In Full Screen.]

Logic To Convert Decimal Number To Binary Number, using While Loop

In this C program we ask the user to enter / input a decimal number. Using while loop we calculate the reminder and add it to variable bin. We make use of variable place to position the reminder based on number system – unit, ten, hundred, thousand, ten thousand etc.

Source Code: C Program To Convert Decimal Number To Binary Number, using While Loop

#include < stdio.h >

int main()
{
    int num, bin = 0, rem = 0, place = 1;

    printf("Enter a decimal number\n");
    scanf("%d", &num);

    printf("\nBinary equivalent of %d is ", num);
    while(num)
    {
        rem   = num % 2;
        num   = num / 2;
        bin   = bin + (rem * place);
        place = place * 10;
    }
    printf("%d\n", bin);

    return 0;
}

Output 1:
Enter a decimal number
14

Binary equivalent of 14 is 1110

Output 2:
Enter a decimal number
15

Binary equivalent of 15 is 1111

Output 3:
Enter a decimal number
19

Binary equivalent of 19 is 10011

Output 4:
Enter a decimal number
8

Binary equivalent of 8 is 1000

Output 5:
Enter a decimal number
41

Binary equivalent of 41 is 101001

Number Systems

number systems

1. Binary Number System uses base 2 and digits 01.
2. Octal Number System uses base 8 and digits 01234567.
3. Decimal Number System uses base 10 and digits 0123456789.
4. Hexadecimal Number System uses base 16 and digits 0123456789ABCDEF.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Convert Fahrenheit To Degree Celsius: C Program

Temperature of a city in Fahrenheit degree is input through the keyboard. Write a program to convert this temperature into Degree Celsius / Centigrade.

Related Read:
Basic Arithmetic Operations In C
Convert Degree Celsius To Fahrenheit: C Program

Formula to Convert Fahrenheit To Celsius.

From our previous video tutorial Convert Degree Celsius To Fahrenheit: C Program we already know the formula to convert temperature from Celsius to Fahrenheit. We will use the same formula and convert it to calculate Degree celsius from Fahrenheit.

Fahrenheit = (Centigrade * 1.8) + 32;
(Fahrenheit – 31) = (Centigrade * 1.8);
(Fahrenheit – 31) / 1.8 = (Centigrade);

So we’ll use this formula Centigrade = (Fahrenheit – 31) / 1.8; to calculate Degree Centigrade by getting Fahrenheit value from the user.

Convert Fahrenheit To Degree Celsius: C Program


[youtube https://www.youtube.com/watch?v=jBR9hco2O-w]

YouTube Link: https://www.youtube.com/watch?v=jBR9hco2O-w [Watch the Video In Full Screen.]


Source Code: Convert Fahrenheit To Degree Celsius: C Program

#include < stdio.h >

int main()
{
    float c, fh;

    printf("Enter temperature in Fahrenheit\n");
    scanf("%f", &fh);

    c = (fh - 32) / 1.8;

    printf("Temperature in Degree Celius is %f\n", c);

    return 0;
}

Output 1:
Enter temperature in Fahrenheit
212
Temperature in Degree Celius is 100.000000

Output 2:
Enter temperature in Fahrenheit
113
Temperature in Degree Celius is 45.000000

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Convert Degree Celsius To Fahrenheit: C Program

Temperature of a city in Degree Celsius / Centigrade degree is input through the keyboard. Write a program to convert this temperature into Fahrenheit.

Related Read:
Basic Arithmetic Operations In C
Division of 2 Numbers: C
Convert Fahrenheit To Degree Celsius: C Program

Formula to Convert Celsius To Fahrenheit

Fahrenheit = (Centigrade * (9/5)) + 32;
But in C programming, any number divided by an integer number will return integer value. So 9/5 will give 1 and not 1.8

So we need to be careful while doing division operation in C. To solve this issue we can write following formula in C program to convert degree celsius to Fahrenheit.

Fahrenheit = (Centigrade * (9/5.0)) + 32;
OR
Fahrenheit = (Centigrade * 1.8) + 32;

Convert Degree Celsius To Fahrenheit: C Program


[youtube https://www.youtube.com/watch?v=UFtA-OVypHs]

YouTube Link: https://www.youtube.com/watch?v=UFtA-OVypHs [Watch the Video In Full Screen.]


Source Code: Convert Degree Celsius To Fahrenheit: C Program

#include < stdio.h >

int main()
{
    float c, fh;

    printf("Enter temperature in Centigrade\n");
    scanf("%f", &c);

    fh = (c * 1.8) + 32;

    printf("Temperature in Fahrenheit is %f\n", fh);

    return 0;
}

Output 1:
Enter temperature in Centigrade
100
Temperature in Fahrenheit is 212.000000

Output 2:
Enter temperature in Centigrade
45
Temperature in Fahrenheit is 113.000000

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert