C Program To Convert Decimal To Binary Number using Recursion

A positive integer is entered through the keyboard, write a function to find the Binary equivalent of this number:

(1) Without using recursion.
(2) Using recursion.

Analyze The Problem Statement

We need to convert the user input Decimal number to its equivalent Binary number using iterative logic as well as recursive logic.

In this video tutorial, we’ll write 2 functions. One for iterative logic and another for recursive logic.

Expected Input/Output

Enter a Decimal number
14

Iterative Logic
Binary Equivalent of 14 is 1110.

Recursive Logic
Binary Equivalent of 14 is 11110.

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

Binary Number System

Explanation:

If user enters num = 14

We keep on dividing and modulo dividing the number by 2.

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

So Binary equivalent of 14 is 1110.

Video Tutorial: C Program To Convert Decimal To Binary Number using Recursion


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

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

Source Code: C Program To Convert Decimal To Binary Number using Recursion

#include<stdio.h>

int binary_rec(int);
int binary(int);

int main()
{
    int num;

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

    printf("Binary Equivalent (Iterative) of %d is %d\n", num, binary(num));
    printf("Binary Equivalent (Recursive) of %d is %d\n", num, binary_rec(num));

    return 0;
}

int binary_rec(int num)
{
    if(num == 0)
        return 0;
    else
        return((num % 2) + 10 * binary_rec(num/2));
}

int binary(int num)
{
    int rem, bin = 0, place = 1;
    while(num)
    {
        rem   = num % 2;
        num   = num / 2;
        bin   = bin + (rem * place);
        place = place * 10;
    }
    return(bin);
}

Output 1:
Enter a Decimal number
14

Iterative Logic
Binary Equivalent of 14 is 1110

Recursive Logic
Binary Equivalent of 14 is 1110

Output 2:
Enter a Decimal number
41

Iterative Logic
Binary Equivalent of 41 is 101001

Recursive Logic
Binary Equivalent of 41 is 101001

Logic To Convert Decimal Number To Binary Number using Recursion

For iterative logic, please check the video tutorial C Program To Convert Decimal Number To Binary Number, using While Loop.

Recursive Function Logic
Assume that user inputs num value as 14.

numnum % 2(num % 2) + 10 * binary_rec(num/2)
1414 % 2(0) + 10 * binary_rec(7)
77 % 2(1) + 10 * binary_rec(3)
33 % 2(1) + 10 * binary_rec(1)
11 % 2(1) + 10 * binary_rec(0)

Value Returning – Control Shifting back.

Return ValueToResult
return 0;(1) + 10 * binary_rec(0)(1) + 10 * 0 = 1
1(1) + 10 * binary_rec(1)(1) + 10 * 1 = 11
11(1) + 10 * binary_rec(3)(1) + 10 * 11 = 111
111(0) + 10 * binary_rec(7)(0) + 10 * 111 = 1110

Binary Equivalent of Decimal Number 14 is 1110.

Source Code: C Program To Convert Decimal To Binary Number using Recursion and Ternary or Conditional Operator

#include<stdio.h>

int binary_rec(int);
int binary(int);

int main()
{
    int num;

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

    printf("\nIterative Logic\n");
    printf("Binary Equivalent of %d is %d\n\n", num, binary(num));

    printf("\nRecursive Logic\n");
    printf("Binary Equivalent of %d is %d\n\n", num, binary_rec(num));

    return 0;
}

int binary_rec(int num)
{
    return( (num == 0) ? 0 : (num % 2) + 10 * binary_rec(num / 2));
}

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

    while(num != 0)
    {
        rem   = num % 2;
        num   = num / 2;
        bin   = bin + (rem * place);
        place = place * 10;
    }
    return(bin);
}

Output 1:
Enter a Decimal number
14

Iterative Logic
Binary Equivalent of 14 is 1110

Recursive Logic
Binary Equivalent of 14 is 1110

Output 2:
Enter a Decimal number
41

Iterative Logic
Binary Equivalent of 41 is 101001

Recursive Logic
Binary Equivalent of 41 is 101001

To know more about Ternary or Conditional Operator visit:
Ternary Operator / Conditional Operator In C

Source Code: C Program To Convert Decimal To Binary Number using Recursion

Another Method

#include<stdio.h>

void binary_rec(int);
int binary(int);

int main()
{
    int num;

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

    printf("\nIterative Logic\n");
    printf("Binary Equivalent of %d is %d\n\n", num, binary(num));

    printf("\nRecursive Logic\n");
    printf("Binary Equivalent of %d is ", num);
    binary_rec(num);
    
    printf("\n");

    return 0;
}

void binary_rec(int num)
{
    if(num == 1)
        printf("1");
    else
    {
        binary_rec(num/2);
        printf("%d", num%2);
    }
}

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

    while(num != 0)
    {
        rem   = num % 2;
        num   = num / 2;
        bin   = bin + (rem * place);
        place = place * 10;
    }
    return(bin);
}

Output:
Enter a Decimal number
14

Iterative Logic
Binary Equivalent of 14 is 1110

Recursive Logic
Binary Equivalent of 14 is 1110

Here we simply divide the number by 2 and keep passing it as new value of num to binary_rec() function, and we print num%2 once num = 1 and it returns the value 1.

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 Pounds to Kilograms

Lets write a C program to Convert weight from Pounds to Kilograms.

Note: 1 Pound is approximately equal to 0.453592 Kilogram.

pound to kilogram

Formula To Convert Weight From Pounds to Kilograms

kilogram = pound * 0.453592;

OR

From our previous video tutorial C Program To Convert Kilograms to Pounds

pound = kilogram * 2.20462;
pound / 2.20462 = kilogram;
kilogram = pound / 2.20462;

Related Read:
Keywords, Constants, Variables: C
Basic Arithmetic Operations In C

Logic To Convert Pounds to Kilograms

First we ask the user to enter weight / mass in pounds. Then we multiply that value with 0.453592. The result itself is the Kilograms equivalent of user entered weight in pound.

Video Tutorial: C Program To Convert Pounds to Kilograms


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

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

Source Code: C Program To Convert Pounds to Kilograms

#include<stdio.h>

int main()
{
    const float KG = 0.453592;
          float pound;

    printf("Enter weight in pounds\n");
    scanf("%f", &pound);

    printf("Weight in Kilograms is %f\n", (pound * KG) );

    return 0;
}

Output 1:
Enter weight in pounds
11
Weight in Kilograms is 4.989512

Output 2:
Enter weight in pounds
11.04
Weight in Kilograms is 5.007656

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 Kilograms to Pounds

Lets write a C program to Convert weight from Kilograms to Pounds.

Note: 1 Kilogram is approximately equal to 2.20462 Pound.

kilogram to pound

Formula To Convert Weight From Kilograms to Pounds

pound = kilogram * 2.20462;

Related Read:
Keywords, Constants, Variables: C
Basic Arithmetic Operations In C

Logic To Convert Kilograms to Pounds

First we ask the user to enter weight / mass in kilograms. Then we multiply that value with 2.20462. The result itself is the pound equivalent of user entered weight in KG.

Video Tutorial: C Program To Convert Kilograms to Pounds


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

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

Source Code: C Program To Convert Kilograms to Pounds

#include<stdio.h>

int main()
{
    const float POUND = 2.20462;
    float kg;

    printf("Enter weight in Kilograms\n");
    scanf("%f", &kg);

    printf("Weight in Pounds is %f\n", (kg * POUND));

    return 0;
}

Output 1:
Enter weight in Kilograms
5
Weight in Pounds is 11.023099

Output 2:
Enter weight in Kilograms
14
Weight in Pounds is 30.864678

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 (R, G, B) To (C, M, Y, K) Color Format

In digital world colors are specified in Red-Green-Blue (RGB) format, with values of R, G, B varying on an integer scale from 0 to 255. In print publishing the colors are mentioned in Cyan-Magenta-Yellow-Black (CMYK) format, with values of C, M, Y and K varying on a real scale from 0.0 to 1.0. Write a C program that converts RGB color to CMYK color as per the following formulae:

White = Max(Red/255, Green/255, Blue/255);
Cyan = (White – Red/255) / White;
Magenta = (White – Green/255) / White;
Yellow = ( White – Blue/255) / White;
Black = 1-White

Note that if the RGB values are all 0, then the CMY values are all 0 and the K value is 1.

Related Read:
if else statement in C
Relational Operators In C
Logical Operators In C

Expected Output for the Input

User Input:
Enter values for Red, Green and Blue(RGB) in Range 0 – 255
41
14
41

Output:
CMYK = (0.000000, 0.658537, 0.000000, 0.839216)

Video Tutorial: C Program To Convert (R, G, B) To (C, M, Y, K) Color Format


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

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

Source Code: C Program To Convert (R, G, B) To (C, M, Y, K) Color Format

#include < stdio.h >

int main()
{
    float red, green, blue;
    float white, cyan, magenta, yellow, black;
    float max;

    printf("Enter values for Red, Green and Blue(RGB) in Range 0 - 255\n");
    scanf("%f%f%f", &red, &green, &blue);

    if(red == 0 && green == 0 && blue == 0)
    {
        cyan   = magenta = yellow = 0;
        black  = 1;
        black  = 1;
    }
    else
    {
        red    = red   / 255;
        green  = green / 255;
        blue   = blue  / 255;

        max    = red;
        if(green > max)
        {
            max = green;
        }

        if(blue > max)
        {
            max = blue;
        }

        white   = max;

        cyan    = (white - red)  / white;
        magenta = (white - green)/ white;
        yellow  = (white - blue) / white;

        black   = 1 - white;

    }

    printf("CMYK = (%f, %f, %f, %f)\n",
            cyan, magenta, yellow, black);

    return 0;
}

Output 1:
Enter values for Red, Green and Blue(RGB) in Range 0 – 255
255
255
255
CMYK = (0.000000, 0.000000, 0.000000, 0.000000)

Output 2:
Enter values for Red, Green and Blue(RGB) in Range 0 – 255
0
0
0
CMYK = (0.000000, 0.000000, 0.000000, 1.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

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

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

In this C program we first convert the user entered number from Octal number system to Decimal number system. Then we take the result(which is in Decimal number system) and convert to Binary number system.

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.

Related Read:
while loop in C programming
C Program To Convert Octal Number To Decimal Number, using While Loop
C Program To Convert Decimal Number To Binary Number, using While Loop

Note:
In this C program we are dealing with Octal Number System, Decimal Number System and Binary Number System.

Octal Number System
Octal number system has base 8 and digits 0, 1, 2, 3, 4, 5, 6, 7.
octal number system
Example:
etc .. 84, 83, 82, 81, 80

etc .. 84 = ‭4,096‬, 83 = 512, 82 = 64, 81 = 8, 80 = 1.

Binary Number System
Binary number system has base 2 and digits 0 and 1.
binary decimal number system
Example:
etc .. 24, 23, 22, 21, 20

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

Expected Output for the Input

User Input:
Enter an Octal Number
14

Output:
Binary Equivalent of Octal no 14 is 1100.

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


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

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

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

To get complete logic to this program kindly watch these two video tutorials without fail.

1. C Program To Convert Octal Number To Decimal Number, using While Loop
2. C Program To Convert Decimal Number To Binary Number, using While Loop

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

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

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

    printf("Enter an Octal Number\n");
    scanf("%d", &num);

    printf("Binary Equivalent of Octal no %d is ", num);
    while(num)
    {
        rem = num % 10;
        dec = dec + rem * pow(8, place);
        num = num / 10;
        place++;
    }

    place = 1;
    rem   = 0;
    while(dec)
    {
        rem   = dec % 2;
        bin   = bin + (rem * place);
        dec   = dec / 2;
        place = place * 10;
    }
    printf("%ld.\n", bin);

    return 0;
}

Output 1:
Enter an Octal Number
71
Binary Equivalent of Octal no 71 is 111001.

Output 2:
Enter an Octal Number
11
Binary Equivalent of Octal no 11 is 1001.

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