Coding Practice
Showing posts with label C Programming Practice. Show all posts
Showing posts with label C Programming Practice. Show all posts

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

Write a C Code to find total marks from given input...

Write a C Code to find total marks from given input: Attendance, Class test, Quiz test, Assignment, Midterm and Final Exam and find the grade using the following method:

  1. If Total mark >=80 then A+
  2. If Total mark >=70 then A
  3. If Total mark >=60 then A
  4. If Total mark >=50 then B
  5. Else Grade = F

Sample Input

Enter Attendance, Class test, Quiz test, Assignment, Midterm and Final Exam: 2.5 7.5 3.5 7 21.5 33

Sample Output

Total Mark: 75 & Grade: A

Sample Output
Enter Attendance, Class test, Quiz test, Assignment, Midterm and Final Exam: 2.5 7.5 3.5 7 21.5 33
Total Mark: 75.000000 & Grade: A
Source Code
#include<stdio.h>

int main()
{
    float attendance, ct, qt, assignment, mid_mark, final_mark, total_mark;

    printf("Enter Attendance, Class test, Quiz test, Assignment, Midterm and Final Exam: ");
    scanf("%f %f %f %f %f %f", &attendance, &ct, &qt, &assignment, &mid_mark, &final_mark);
    //Input: 2.5 7.5 3.5 7 21.5 33

    total_mark = (attendance + ct + qt + assignment + mid_mark + final_mark);

    if(total_mark >= 80 && total_mark <= 100)
    {
        printf("Total Mark: %f & Grade: A+\n\n", total_mark);
    }
    else if(total_mark >= 70 && total_mark <= 79)
    {
        printf("Total Mark: %f & Grade: A\n\n", total_mark);
    }
    else if(total_mark >= 60 && total_mark <= 69)
    {
        printf("Total Mark: %f & Grade: A-\n\n", total_mark);
    }
    else if(total_mark >= 50 && total_mark <= 59)
    {
        printf("Total Mark: %f & Grade: B\n\n", total_mark);
    }
    else
    {
        printf("Total Mark: %f & Grade: F\n\n", total_mark);
    }

    return 0;
}
Sample Output
Enter Attendance, Class test, Quiz test, Assignment, Midterm and Final Exam: 2.5 7.5 3.5 7 21.5 33
Total Mark: 75.000000 & Grade: A

Write a C Code to calculate D = 𝒃^𝟐 − 𝟒𝐚𝐜 and...

Write a C Code to calculate D = 𝒃^𝟐 − 𝟒𝐚𝐜 and show the result according to following conditions:

a. D>0 then Calculate X1, X2. Where 𝒙𝟏 = (−𝒃+√𝒃^𝟐−𝟒𝒂𝒄)/𝟐𝒂 and 𝒙𝟐 = (−𝒃−√𝒃^𝟐−𝟒𝒂𝒄)/𝟐𝒂

        b. D =0 then calculate X. Where 𝐱 = 𝒃^𝟐/𝟐𝒂

        c. D<0 then print “No Solution”.

Sample Input

        Enter a, b & c: 5 6 1

Sample Output

    X1: -0.2

    X2: -1
Sample Output
Enter a, b, & c: 5 6 1
X1: -0.200
x2: -1.000
Source Code
#include<stdio.h>
#include<math.h>

int main()
{
    float D, b, a, c, x, x1, x2;

    printf("Enter a, b, & c: ");
    scanf("%f %f %f", &a, &b, &c);

    D = ((b*b) - 4*a*c);

    if(D > 0)
    {
        x1 = (-b + sqrt(D))/(2*a);
        x2 = (-b - sqrt(D))/(2*a);

        printf("X1: %.3f\nx2: %.3f\n\n", x1, x2);
    }
    else if(D == 0)
    {
        x = (b*b)/(2*a);

        printf("X: %f\n\n");
    }
    else if(D < 0)
    {
        printf("No Solution\n\n");
    }
    return 0;
}
Sample Output
Enter a, b, & c: 5 6 1
X1: -0.200
x2: -1.000

Write a C program to solve the Drake equation...

Write a C program to solve the Drake equation N = R*fp*ne*fl*fi*fc*L. Where R, ne and L must be discrete value.

Sample Input

    Enter R, F(p), n(e), f(l), f(i), f(c), L: 5 0.25 20 0.2 0.15 0.11 150

Sample Output

    The number of communicative civilizations within the Milky Way today: 12.375

Sample Output
Enter R, F(p), n(e), f(l), f(i), f(c): 5 0.25 20 0.2 0.15 0.11 150
The number of communicative civilizations within the Milky Way today: 12.375
Source Code
#include<stdio.h>
#include<math.h>

int main()
{
    float N, fp, fl, fi, fc;
    int R, ne, L;

    printf("Enter R, F(p), n(e), f(l), f(i), f(c): ");
    scanf("%d %f %d %f %f %f %d", &R, &fp, &ne, &fl, &fi, &fc, &L); //Input: 5 0.25 20 0.2 0.15 0.11 150

    float n = (float)R*ne*L;
    N = n*fp*fl*fi*fc;

    printf("The number of communicative civilizations within the Milky Way today: %.3f\n\n", N);

    return 0;
}
Sample Output
Enter R, F(p), n(e), f(l), f(i), f(c): 5 0.25 20 0.2 0.15 0.11 150
The number of communicative civilizations within the Milky Way today: 12.375

Write a C/JAVA program to implement CRC

Implement CRC

The program will take a sequence of binary data bits, and a divider as input. And gives the resultant data with CRC as output. (Where, divider < binary data bits)

Sample Input:
Data: 10110
Divider: 1101

Sample Output:
Modified data: 10110000
Data Send: 10110101
CRC bit: 101

Sample Output
Enter data: 1001
Enter divisor: 1011

Input Data With Zero(s): 1001000

CRC Bit: 110

Modified Data: 1001110
Source Code
#include<stdio.h>
#include<stdlib.h>

void main()
{
    int i, j, k, l, numOfZero, dataLen;
    char divisor[100], data[100], remainder[100], divisorClone[100], dataClone[100], crc[100];

    printf("Enter data: ");
    scanf("%s", data);
    printf("Enter divisor: ");
    scanf("%s", divisor);

    for(i = 0; data[i] != '\0'; i++);
    dataLen = i;
    for(i = 0; divisor[i] != '\0'; i++);
    numOfZero = i;

    for(i = 0; i < numOfZero-1; i++)
    {
        data[dataLen+i] = '0';
    }
    printf("\nInput Data With Zero(s): %s\n", data);
    strcpy(dataClone, data);

    for(i = 0; i < dataLen; i++)
    {
        strcpy(divisorClone, divisor);
        if(data[i] < divisor[0])
        {
            for(l = 0; l < numOfZero; l++)
            {
                divisor[l] = '0';
            }
            divisor[l] = '\0';
        }
        for(j = i, k = 0; j < numOfZero+i; j++, k++)
        {
            if(data[j] == divisor[k])
            {
                remainder[j] = '0';
            }
            else
            {
                remainder[j] = '1';
            }
        }
        strcpy(divisor, divisorClone);
        remainder[j] = dataClone[j];
        remainder[j+1] = '\0';
        strcpy(data, remainder);
    }
    strcpy(data, remainder);

    for(i = dataLen, j = 0; i < dataLen+numOfZero; i++)
    {
        dataClone[i] = data[i];
        crc[j++] = data[i];
    }
    crc[j-1] = '\0';
    dataClone[i-1] = '\0';

    printf("\nCRC Bit: %s\n\nModified Data: %s\n\n", crc, dataClone);
}
Sample Output
Enter data: 10111011
Enter divisor: 1001

Input Data With Zero(s): 10111011000

CRC Bit: 110

Modified Data: 10111011110

Write a program to find Hamming Distance

Hamming Distance

Hamming distance is a metric for comparing two binary data strings. While comparing two binary strings of equal length, Hamming distance is the number of bit positions in which the two bits are different.

Example: Find the distance between the vectors 01101010 and 11011011.
(01101010
11011011)
They differ in four places, So the Hamming distance d(01101010, 11011011) = 4.

Sample Output
Enter Comp1 data: 111101010
Enter Comp2 data: 010101010

Hamming Distance: 2
C-Source Code
#include<stdio.h>

void main()
{
    int i, comp1Len, comp2Len, hammingDistance = 0;
    char comp1[500], comp2[500];
input:
    printf("Enter Comp1 data: ");
    scanf("%s", comp1);
    printf("Enter Comp2 data: ");
    scanf("%s", comp2);

    for(i = 0; comp1[i] != '\0'; i++);
    comp1Len = i;

    for(i = 0; comp2[i] != '\0'; i++);
    comp2Len = i;

    if(comp1Len != comp2Len)
    {
        printf("\nComp1 and Comp2 length are not same!\n\n");
        goto input;
    }

    for(i = 0; i < comp1Len; i++)
    {
        if(comp1[i] != comp2[i])
        {
            hammingDistance++;
        }
    }
    printf("\nHamming Distance: %d\n\n", hammingDistance);
}
Sample Output
Enter Comp1 data: 01101010
Enter Comp2 data: 11011011

Hamming Distance: 4

Write a program to calculate the MINIMUM HAMMING DISTANCE for a given set of binary data taken as input

Minimum Hamming Distance
Sample Input:
    Number of inputs: 3
    10110
    01110
    11001
    Sample Output:
    Minimum hamming distance, (C1, C2) = 2
Sample Output
Number of inputs: 3
10110
01110
11001

Minimum hamming distance, (10110, 01110) = 2
C-Source Code
/**
I will share the code after 15 April 2021,
If you need urgent, please contact with me.
*/
Sample Output
Number of inputs: 4
1110
0011
0000
10

Data must be same length!
Enter 4 data, whose length 4 bits...
1110
0011
0000
1011

Minimum hamming distance, (0011, 1011) = 1

Write a program that choose Even parity / Odd parity according to user choose. And accordingly performs Even parity / Odd parity process

Parity Bit Checker

Example 1:
Enter 1 for Even parity and 2 for Odd parity.
Input: 1

Even parity:
Input:
Data: 110110
Output:
modified data: 1101100
Parity bit: 0

Example 2:
Enter 1 for Even parity and 2 for Odd parity.
Input: 2

Odd parity:
Input:
Data: 1101101
Output:
Modified data: 11011010
Parity bit: 0

Sample Output
Enter 1 for Even parity and 2 for Odd parity.

Enter your choice: 1

Even parity:
Data: 110110

Modified Data: 1101100
Parity Bit: 0
C-Source Code
#include<stdio.h>
#include<string.h>

void main()
{
    int i, choice, even_parity_len, count = 0, p_bit;
    char parity_bit, even_parity[100];
main:
    printf("Enter 1 for Even parity and 2 for Odd parity.\n\nEnter your choice: ");
    scanf("%d", &choice);

    switch(choice)
    {
    case 1:
        printf("\nEven parity:\nData: ");
        scanf("%s", even_parity);

        for(i = 0; even_parity[i] != '\0'; i++);
        even_parity_len = i;

        for(i = 0; i < even_parity[i]; i++)
        {
            if(even_parity[i] == '1')
            {
                count++;
            }
        }

        if(count%2 == 0)
        {
            even_parity[even_parity_len] = '0';
            p_bit = 0;
        }
        else
        {
            even_parity[even_parity_len] = '1';
            p_bit = 1;
        }
        break;

    case 2:
        printf("\nOdd parity:\nData: ");
        scanf("%s", even_parity);

        for(i = 0; even_parity[i] != '\0'; i++);
        even_parity_len = i;

        for(i = 0; i < even_parity[i]; i++)
        {
            if(even_parity[i] == '1')
            {
                count++;
            }
        }

        if(count%2 == 0)
        {
            even_parity[even_parity_len] = '1';
            p_bit = 1;
        }
        else
        {
            even_parity[even_parity_len] = '0';
            p_bit = 0;
        }
        break;

    default:
        printf("\nInvalid Input! Please enter 1 or 2.\n");
        goto main;
        break;
    }
    even_parity[even_parity_len+1] = '\0';

    printf("\nModified Data: %s\nParity Bit: %d\n\n", even_parity, p_bit);
}
Sample Output
Enter 1 for Even parity and 2 for Odd parity.

Enter your choice: 2

Odd parity:
Data: 1101101

Modified Data: 11011010
Parity Bit: 0

Write a program that construct an Even parity Hamming code

Even parity Hamming code

Example-1:
Input: 10111010100101
Output: 
Mod data: 1111011110101000101
Number of parity bits: 5

Example-2:
Input: 101100
Output: 
Mod data: 0110011000
Number of parity bits: 4

Table Even Parity Hamming Code

Before add parity bit, data will generate like this: 0X1X011X1010100X101

P1 = [3, 5, 7, 9, 11, 13, 15, 17, 19] = 101111011
= X1 = 1 {Odd parity}
P2 = [3, 6, 7, 10, 11, 14, 15, 18, 19] = 111010001
= X2 = 1 {Odd parity}
P4 = [5, 6, 7, 12, 13, 14, 15] = 0110100
= X4 = 1 {Odd parity}
P8 = [9, 10, 11, 12, 13, 14, 15] = 1010100
= X8 = 1 {Odd parity}
P16 = [17, 18, 19] = 101
= X16 = 0 {Even parity}

So, Modified data = 1111011110101000101

Sample Output
Enter data: 101100

Mod data: 0110011000
Number of parity bits: 4
Source Code
#include<stdio.h>
#include<math.h>

void main()
{
    int i, j, k, l, m, n, x, p_bit[50], p_bit_count = -1;
    int count, dataLen, value = 0, temp_even_parity_len;
    char data[500], temp_data[500], modData[500], temp_even_parity[100];

    printf("Enter data: ");
    scanf("%s", data);

    for(i = 0; data[i] != '\0'; i++);
    dataLen = i;

    for(i = 0; value <= (dataLen + i + 1); i++)
    {
        value = pow(2, i);
        p_bit[i] = value;
        p_bit_count++;
    }

    i = 0;
    j = 0;
    k = 0;
    while(i < (dataLen + p_bit_count))
    {
        if(p_bit[k] == i+1)
        {
            modData[i] = 'X';
            k++;
        }
        else
        {
            modData[i] = data[j];
            j++;
        }
        i++;
    }
    modData[i] = '\0';

    for(k = 0; k < p_bit_count; k++)
    {
        l = 0;
        for(m = p_bit[k]; m <= dataLen + p_bit_count;)
        {
            for(n = 0; n < p_bit[k] && m <= dataLen + p_bit_count; n++)
            {
                if(m == p_bit[k])
                {
                    m++;
                }
                else
                {
                    temp_even_parity[l++] = modData[m-1];
                    m++;
                }
            }
            for(n = 0; n < p_bit[k] && m <= dataLen + p_bit_count; n++)
            {
                m++;
            }
        }
        temp_even_parity[l] ='\0';

        for(i = 0; temp_even_parity[i] !='\0'; i++);
        temp_even_parity_len = i;

        for(i = 0, count = 0; i < temp_even_parity_len; i++)
        {
            if(temp_even_parity[i] == '1')
            {
                count++;
            }
        }

        if(count % 2 == 0)
        {
            x = (p_bit[k]-1);
            modData[x] = '0';
        }
        else
        {
            x = (p_bit[k]-1);
            modData[x] = '1';
        }
    }
    printf("\nMod data: %s\n", modData);
    printf("Number of parity bits: %d\n\n", p_bit_count);
}
Sample Output
Enter data: 10111010100101

Mod data: 1111011110101000101
Number of parity bits: 5

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

Check whether a number is Armstrong number or not
Sample Output
Check whether a number is Armstrong number or not.

Enter a number: 370

370 is an Armstrong number!
Source Code
#include<stdio.h>
#include<math.h>

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

    printf("Check whether a number is Armstrong number or not.\n\n");
    printf("Enter a number: %d", number);
    scanf("%d", &number);

    numberClone = number;

    while(number != 0)
    {
        digit = (number % 10);
        //newNumber = newNumber;
        number /= 10; /// Or, number = number / 10;
        count++;
    }

    number = numberClone;
    while(number != 0)
    {
        digit = (number % 10);
        temp = pow(digit, count);
        newNumber += temp;
        number /= 10; /// Or, number = number / 10;
    }

    number = numberClone;
    if(newNumber == number)
    {
        printf("\n%d is an Armstrong number!\n\n", number);
    }
    else
    {
        printf("\n%d is not an Armstrong number.\n\n", number);
    }
}
Sample Output
Check whether a number is Armstrong number or not.

Enter a number: 372

372 is not an Armstrong number.

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

Print all Perfect numbers between 1 to n
Sample Output
Print all Perfect numbers between 1 to n.

Enter number: 100

6

28
Source Code
#include<stdio.h>

int main()
{
    int i, j, num, newNumber;
    
    printf("Print all Perfect numbers between 1 to n.\n\n");
    printf("Enter number: ");
    scanf("%d", &num);

    for(i = 1; i < num; i++)
    {
        newNumber = 0;

        for(j = 1; j < i; j++)
        {
            if(i % j == 0)
            {
                newNumber = newNumber + j; //Or, newNumber += j;
            }
        }
        if(i == newNumber)
        {
            printf("\n%d\n", i);
        }
    }
    return 0;
}
Sample Output
Print all Perfect numbers between 1 to n.

Enter number: 10000

6

28

496

8128

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

Check whether a number is Perfect number or not
Sample Output
Check whether a number is Perfect number or not.

Enter a number: 6

6 is a perfect number.
Source Code
#include<stdio.h>

int main()
{
    int n, num, newNumber = 0;
    printf("Check whether a number is Perfect number or not.\n\n");
    printf("Enter a number: ");
    scanf("%d", &num);

    for(n = 1; n < num; n++)
    {
        if(num % n == 0)
        {
            newNumber = newNumber + n; //Or, newNumber += n;
        }
    }
    if(num == newNumber)
    {
        printf("\n%d is a perfect number.\n", num);
    }
    else
    {
        printf("\n%d is not a perfect number.\n", num);
    }
    return 0;
}
Sample Output
Check whether a number is Perfect number or not.

Enter a number: 5

5 is not a perfect number.

Write a C program to find all prime factors of a number

Find all prime factors of a number
Sample Output
Find all prime factors of a number.

Enter a number: 10
Prime factors of 10: 2 5
Source Code
#include<stdio.h>

int main()
{
    int i, j, number, isPrime;

    printf("Find all prime factors of a number.\n\n");
    printf("Enter a number: ");
    scanf("%d", &number);

    printf("Prime factors of %d: ", number);
    for(i = 1; i <= number; i++)
    {
        if(number % i == 0)
        {
            int isPrime = 1;
            for(j = 2; j < i; j++)
            {
                if(i % j == 0)
                {
                    isPrime = 0;
                }
            }
            if(isPrime == 1)
            {
                if(i == 1)
                {
                    /**
                    * 1 is not a prime number
                    */
                }
                else
                {
                    printf("%d ", i);
                }
            }
        }
    }
    printf("\n\n");

    return 0;
}
Sample Output
Find all prime factors of a number.

Enter a number: 15
Prime factors of 15: 3 5
Change Theme
X