Coding Practice

Write a C program to convert Binary to Decimal number system

Convert Binary to Decimal number system
Sample Output
Enter binary number: 1111111

Decimal Number: 127
Source Code
// Convert Binary to Decimal number system

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

int main()
{
    int i, bin_length, power, decimal_number = 0;
    char binary_number[100];

    printf("Enter binary number: ");
    scanf("%s", binary_number);

    // Length of binary_number string
    for(i = 0; binary_number[i] != '\0'; i++);
    bin_length = i;

    /*
    Decimal to Binary
    Example:
    Binary number = 1001

    2^3     2^2     2^1     2^0
    1       0       0       1
    8           +           1

    Decimal number = 9

    */

    for(i = 0, power = (bin_length - 1); i < bin_length; i++, power--)
    {
        if(binary_number[i] == '1')
        {
            decimal_number += pow(2, power);
        }
    }

    printf("\nDecimal Number: %d\n\n", decimal_number);

    return 0;
}
Sample Output
Enter binary number: 01101110

Decimal Number: 110

Write a C program to convert Binary to Octal number system

Convert Binary to Octal number system
Sample Output
Enter binary number: 11010

Octacl Number: 32
Source Code
// Convert Binary to Octal number system

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

int main()
{
    int i, bin_length, dec_length, remainder[100];
    int power, decimal_number = 0, octal_number, temp;
    char binary_number[100];

    printf("Enter binary number: ");
    scanf("%s", binary_number);

    // Length of binary_number string
    for(i = 0; binary_number[i] != '\0'; i++);
    bin_length = i;

    /*
    Decimal to Binary
    Example:
    Binary number = 1001

    2^3     2^2     2^1     2^0
    1       0       0       1
    8           +           1

    Decimal number = 9

    */

    for(i = 0, power = (bin_length - 1); i < bin_length; i++, power--)
    {
        if(binary_number[i] == '1')
        {
            decimal_number += pow(2, power);
        }
    }

    i = 0;
    while(decimal_number != 0)
    {
        remainder[i] = (decimal_number % 8);
        decimal_number /= 8;

        i++;
    }
    dec_length = i;

    for(i = (dec_length - 1); i >= 0; i--)
    {
        octal_number +=  (remainder[i] * pow(10, i));
    }

    printf("\nOctacl Number: %d\n\n", octal_number);

    return 0;
}
Sample Output
Enter binary number: 1111111

Octacl Number: 177

Write a C program to find two's complement of a binary number

Find two's complement of a binary number
Sample Output
Enter binary number: 01101110

One's Complement: 10010001
Two's Complement: 10010010
Source Code
// Find two's complement of a binary number

#include<stdio.h>
#include<string.h>

int main()
{
    int i, bin_length;
    char carry = '1', binary_number[100], ones_comp[100], twos_comp[100];
input:
    printf("Enter binary number: ");
    scanf("%s", binary_number);

    // Length of binary_number string
    for(i = 0; binary_number[i] != '\0'; i++);
    bin_length = i;

    for(i = 0; i < bin_length; i++)
    {
        if(binary_number[i] == '0')
        {
            ones_comp[i] = '1';
        }
        else if(binary_number[i] == '1')
        {
            ones_comp[i] = '0';
        }
        else
        {
            printf("Unexpected character! Please enter a valid input...\n\n");
            goto input;
        }
    }
    ones_comp[i] = '\0'; // End of string

    for(i = (bin_length - 1); i >= 0; i--)
    {
        if(ones_comp[i] == '1' && carry == '1')
        {
            twos_comp[i] = '0';
        }
        else if(ones_comp[i] == '0' && carry == '1')
        {
            twos_comp[i] = '1';
            carry = '0';
        }
        else if(carry == '0')
        {
            twos_comp[i] = ones_comp[i];
        }
    }

    printf("\nOne's Complement: %s\nTwo's Complement: %s\n\n", ones_comp, twos_comp);

    return 0;
}
Sample Output
Enter binary number: 00010100    

One's Complement: 11101011
Two's Complement: 11101100

Write a C program to find one's complement of a binary number

Find one's complement of a binary number
Sample Output
Enter binary number: 110101

Binary Number: 110101
One's Complement: 001010
Source Code
// Find one's complement of a binary number

#include<stdio.h>
#include<string.h>

int main()
{
    int i, bin_length;
    char binary_number[100], ones_comp[100];
input:
    printf("Enter binary number: ");
    scanf("%s", binary_number);

    // Length of binary_number string
    for(i = 0; binary_number[i] != '\0'; i++);
    bin_length = i;

    for(i = 0; i < bin_length; i++)
    {
        if(binary_number[i] == '0')
        {
            ones_comp[i] = '1';
        }
        else if(binary_number[i] == '1')
        {
            ones_comp[i] = '0';
        }
        else
        {
            printf("Unexpected character found! Please enter a valid input...\n\n");
            goto input;
        }
    }
    ones_comp[i] = '\0'; // End of string by creating a NULL value.

    printf("\nBinary Number: %s\nOne's Complement: %s\n\n", binary_number, ones_comp);

    return 0;
}
Sample Output
Enter binary number: 1010

Binary Number: 1010
One's Complement: 0101

Write a C program to print Fibonacci series up to n terms

Print Fibonacci series up to n terms
Sample Output
Enter a limit: 10

Fibonacci series: 0 1 1 2 3 5 8 13 21 34
Source Code
// Print Fibonacci series up to n terms

#include<stdio.h>

int main()
{
    int i, limit, first = 0, second = 1, last;

    /*
    FirstNum + LastNum = LastNum
    Fibonacci series: 0 1 1 2 3 5 8 13. . .
    */

    printf("Enter a limit: ");
    scanf("%d", &limit);

    printf("\nFibonacci series: ");
    for(i = 1; i <= limit; i++)
    {
        printf("%d ", first);

        last = first + second;
        first = second;
        second = last;
    }
    printf("\n\n");

    return 0;
}
Sample Output
Enter a limit: 11

Fibonacci series: 0 1 1 2 3 5 8 13 21 34 55

Write a C program to print all Strong numbers between 1 to n

Print all Strong numbers between 1 to n
Sample Output
Enter a limit for Strong numbers: 150

All Strong numbers between 1 to n: 1 2 145
Source Code
// Print all Strong numbers between 1 to n

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

int main()
{
    int i, j, limit, number, numberClone, digit, sum = 0, factorial = 1;

    printf("Enter a limit for Strong numbers: ");
    scanf("%d", &limit);

    printf("\nAll Strong numbers between 1 to n: ");
    for(i = 1; i <= limit; i++)
    {
        number = numberClone = i;

        while(number != 0)
        {
            digit = (number % 10);
            number = (number / 10);

            // Sum of the factorial of the individual digits
            for(j = 1; j <= digit; j++)
            {
                factorial = (factorial * j);
            }
            sum = (sum + factorial);

            factorial = 1; // Reset value
        }

        if(numberClone == sum)
        {
            printf("%d ", numberClone);
        }

        sum = 0; // Reset value
    }
    printf("\n\n");

    return 0;
}
Sample Output
Enter a limit for Strong numbers: 50000

All Strong numbers between 1 to n: 1 2 145 40585

Write a C program to check whether a number is Strong number or not

Check whether a number is Strong number or not
Sample Output
Enter a number: 145

145 is a Strong number!
Source Code
// Check whether a number is Strong number or not

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

int main()
{
    int i, number, numberClone, digit, sum = 0, factorial = 1;

    printf("Enter a number: ");
    scanf("%d", &number);

    numberClone = number;

    while(number != 0)
    {
        digit = (number % 10);
        number = (number / 10);

        // Sum of the factorial of the individual digits
        for(i = 1; i <= digit; i++)
        {
            factorial = (factorial * i);
        }
        sum = (sum + factorial);

        factorial = 1; // Reset value
    }
    
    if(numberClone == sum)
    {
        printf("\n%d is a Strong number!\n\n", numberClone);
    }
    else
    {
        printf("\n%d is not a Strong number.\n\n", numberClone);
    }

    return 0;
}
Sample Output
Enter a number: 40585

40585 is a Strong number!

Write a C program to print all Armstrong numbers between 1 to n

Print all Armstrong numbers between 1 to n
Sample Output
Enter a limit for armstrong numbers: 900

All armstrong numbers 1 to 900: 1 2 3 4 5 6 7 8 9 153 370 371 407
Source Code
#include<stdio.h>
#include<math.h>

//Print all Armstrong numbers between 1 to n

int main()
{
    int i, limit, number, numberClone, digit, count = 0, newNumber = 0;

    printf("Enter a limit for armstrong numbers: ");
    scanf("%d", &limit);

    printf("\nAll armstrong numbers 1 to %d: ", limit);
    for(i = 1; i <= limit; i++)
    {
        number = i;
        numberClone = number;
        while(number != 0)
        {
            digit = (number%10);
            number /= 10;
            count++;
        }

        number = numberClone;
        while(number != 0)
        {
            digit = (number%10);
            number /= 10;

            newNumber += pow(digit, count);
        }

        if(numberClone == newNumber)
        {
            printf("%d ", newNumber);
        }

        count = 0;
        newNumber = 0;
    }
    printf("\n\n");

    return 0;
}
Sample Output
Enter a limit for armstrong numbers: 900

All armstrong numbers 1 to 900: 0 1 2 3 4 5 6 7 8 9 153 370 371 407
Change Theme
X