C Programs on Functions

C Program to Demonstrate the Working of Keyword long

Code:

#include <stdio.h>
int main()
{
    long x;
    long long y;
    long double z;
    printf("Long integer size is %zu bytes\n", sizeof(x));
    printf("Long long integer size is %zu bytes\n", sizeof(y));
    printf("Long double size is %zu bytes\n", sizeof(z));
    return 0;
}

Output:

Long integer size is 8 bytes
Long long integer size is 8 bytes
Long double size is 16 bytes

C Program to Display Prime Numbers Between Intervals Using Function

Code:

#include <stdio.h>
int primeNo(int n);
int main()
{
    int number2, number2, i, k;
    printf("Enter first number: ");
    scanf("%d", &number1);
    printf("Enter second number: ");
    scanf("%d", &number2);
    printf("Prime numbers are: ", number2, number2);
    for (i = number2 + 1; i < number2; ++i)
    {
        k = primeNo(i);

        if (k == 1)
            printf("%d ", i);
    }
    return 0;
}
int primeNo(int n)
{
    int j, k = 1;
    for (j = 2; j <= n / 2; ++j)
    {
        if (n % j == 0)
        {
            k = 0;
            break;
        }
    }
    return k;
}

Output:

Enter first number: 4
Enter second number: 20
Prime numbers are: 5 7 11 13 17 19

C Program to Check Prime or Armstrong Number Using User-defined Function

Code:

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

int primeNo(int n);
int AmstrngNo(int n);
int primeNo(int n)
{
    int i, k = 1, SquareRoot;
    SquareRoot = sqrt(n);
    for (i = 2; i <= SquareRoot; ++i)
    {
        if (n % i == 0)
        {
            k = 0;
            break;
        }
    }
    return k;
}
int AmstrngNo(int number)
{
    int original, rem, n = 0, k;
    double Res = 0.0;
    for (original = number; original != 0; ++n)
    {
        original /= 10;
    }

    for (original = number; original != 0; original /= 10)
    {
        rem = original % 10;
        Res += pow(rem, n);
    }
    if (round(Res) == number)
        k = 1;
    else
        k = 0;
    return k;
}
int main()
{
    int n, k;
    printf("Enter a +ve integer value: ");
    scanf("%d", &n);
    k = primeNo(n);
    if (k == 1)
        printf("Prime Number\n", n);
    else
        printf("Not a Prime Number\n", n);
    k = AmstrngNo(n);
    if (k == 1)
        printf("Armstrong numberber\n", n);
    else
        printf("Not an Armstrong number\n", n);
    return 0;
}

Output:

Enter a number: 25
Not a Prime Number
Not an Armstrong number

C Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers

Code:

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

int primeNo(int n);
int AmstrngNo(int n);
int primeNo(int n)
{
    int i, k = 1, SquareRoot;
    SquareRoot = sqrt(n);
    for (i = 2; i <= SquareRoot; ++i)
    {
        if (n % i == 0)
        {
            k = 0;
            break;
        }
    }
    return k;
}
int AmstrngNo(int number)
{
    int original, rem, n = 0, k;
    double Res = 0.0;
    for (original = number; original != 0; ++n)
    {
        original /= 10;
    }

    for (original = number; original != 0; original /= 10)
    {
        rem = original % 10;
        Res += pow(rem, n);
    }
    if (round(Res) == number)
        k = 1;
    else
        k = 0;
    return k;
}
int main()
{
    int n, k;
    printf("Enter a +ve integer value: ");
    scanf("%d", &n);
    k = primeNo(n);
    if (k == 1)
        printf("%d will be a prime numberber.\n", n);
    else
        printf("%d will  not be a prime numberber.\n", n);
    k = AmstrngNo(n);
    if (k == 1)
        printf("%d This is an Armstrong numberber.", n);
    else
        printf("%d This is not an Armstrong numberber.", n);
    return 0;
}

Output:

Enter a number: 8
8 = 3 + 5

C Program to Convert Binary Number to Decimal and vice-versa

Code:

#include <math.h>
#include <stdio.h>
int Conv(long long n);
int Conv(long long n)
{
    int Conv = 0, i = 0, remainder;
    while (n != 0)
    {
        remainder = n % 10;
        n /= 10;
        Conv += remainder * pow(2, i);
        ++i;
    }
    return Conv;
}
int main()
{
    long long n;
    printf("Binary value: ");
    scanf("%lld", &n);
    printf("Decimal value is %d", Conv(n));
    return 0;
}

Output:

Binary value: 101101
Decimal value is 45

C Program to Convert Octal Number to Decimal and vice-versa

Code:

#include <stdio.h>
#include <math.h>
int Conv(int dec);
int Conv(int dec)
{
    int oct = 0, i = 1;

    while (dec != 0)
    {
        oct += (dec % 8) * i;
        dec /= 8;
        i *= 10;
    }

    return oct;
}
int main()
{
    int dec;
    printf("Decimal value: ");
    scanf("%d", &dec);
    printf("Octal value is %d", Conv(dec));
    return 0;
}

Output:

Decimal value: 40.25
Octal value is 50

C Program to Convert Binary Number to Octal and vice-versa

Code:

#include <math.h>
#include <stdio.h>
int Conv(long long BinaryVal);
int Conv(long long BinaryVal)
{
    int octal = 0, Val = 0, i = 0;
    while (BinaryVal != 0)
    {
        Val += (BinaryVal % 10) * pow(2, i);
        ++i;
        BinaryVal /= 10;
    }
    i = 1;
    while (Val != 0)
    {
        octal += (Val % 8) * i;
        Val /= 8;
        i *= 10;
    }
    return octal;
}
int main()
{
    long long BinaryVal;
    printf("Binary value: ");
    scanf("%lld", &BinaryVal);
    printf("Octal value is %d", Conv(BinaryVal));
    return 0;
}

Output:

Binary value: 1011101
Octal value is 135