Saturday, March 7, 2015

Calculating the HCF and LCM of two numbers

Here is the program to calculate the Highest Common Factor (HCF -- also known as Greatest Common Divisor) and Lowest Common Multiple (LCM) of two numbers. The code is extremely simple.

Code

#include <stdio.h>

int main()
{
    unsigned int a, b, i, hcf;
   
    printf("Enter two positive integers > 0: ");
    scanf("%d%d", &a, &b);
   
    for(i=1; i<=(a<b? a: b); i++)
    {
        if(!(a%i) && !(b%i))
        {
            hcf = i;
        }
    }
   
    printf("\nThe HCF of %d and %d is %d\n", a, b, hcf);
    printf("The LCM of %d and %d is %d\n", a, b, ((a*b)/hcf));
   
    return 0;
}



Explanation

HCF is obtained by dividing both numbers with all natural numbers less than the smallest one and choosing the last common factor (which will be the highest) encountered in the for loop. [The expression (a<b? a: b) chooses the smallest of a and b. The special operator ?: is technically known as conditional operator.]
LCM is calculated using the formula LCM = (a*b)/HCF.

Output

Enter two positive integers > 0: 4 6

The HCF of 4 and 6 is 2
The LCM of 4 and 6 is 12

Friday, March 6, 2015

Number/Currency to Words Conversion

This program helps us to convert a number (<1000) into words. That is, you give 912 and it says 'nine hundred and twelve!'

Code

/* 25 February, 2015 */
/* Updated on 21 October, 2015 */

#include <stdio.h>

int main()
{
char ones[][10] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
char tenAbove[][10] = {"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
char tens[][10] = {"ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

int amt;

printf("Enter the amount (<10,000): ");
scanf("%d", &amt);

if(!amt)
{
printf("zero\n");
return 0;
}

if(amt/1000)
{
printf("%s thousand", ones[(amt/1000)-1]);
amt %= 1000;

if(amt) { printf(" "); }
}

if(amt/100)
{
printf("%s hundred", ones[(amt/100)-1]);
amt-=((amt/100)*100);

if(amt) { printf(" and "); }
}

if(amt/10)
{
if(((amt/10)==1) && (amt%10)) /* eleven to nineteen */
{
printf("%s\n", tenAbove[amt-11]);
return 0;
}

printf("%s", tens[(amt/10)-1]);
amt-=((amt/10)*10);

if(amt)
{
printf(" ");
}
}

if(amt)
{
printf("%s", ones[amt-1]);
}

printf("\n");

return 0;
}

Output

Enter the amount (<10,000): 1914
one thousand nine hundred and fourteen

Thursday, February 12, 2015

Solution of Quadratic Equation

Here is the code for calculating the root(s) of a quadratic equation.

Code

/* Note: compile with -lm option to link with the math library (for sqrt())
   e.g.: gcc quadratic.c -lm
*/
#include <stdio.h>
#include <math.h>

int main()
{
    int a, b, c, disc;

    printf("Enter the values for a, b, and c: ");
    scanf("%d%d%d", &a, &b, &c);

    disc = (b*b)-(4*a*c); /* discriminant */

    if(disc>0) /* Two real roots */
    {
        printf("The solutions are %f and %f\n", (-b+sqrt(disc))/(2*a), (-b-sqrt(disc))/(2*a));
    }
    else if(disc<0) /* Two imaginary roots */
    {
        printf("The solutions are %d+%f i and %d-%f i\n", -b/2, sqrt(-disc)/(2*a), -b/2, sqrt(-disc)/(2*a));
    }
    else /* Single real root */
    {
        printf("The solution is %f\n", (float)(-b)/(2*a));
    }

    return 0;
}

Output

Enter the values for a, b, and c: 1 -10 34
The solutions are 5+3.000000 i and 5-3.000000 i

Monday, February 9, 2015

Currency Denomination

You will wonder once you realise how powerful this simple code is! Given an amount, this program will print the denomination for it. The reason this code became really compact is, it makes use of array.

Code

#include <stdio.h>

int main()
{
    int currencies[] = {1000, 500, 100, 50, 10, 5, 2, 1};
    long int amount;
    int buf, pos = 0;

    printf("Enter the amount: ");
    scanf("%ld", &amount);

    while(amount > 0)
    {
        buf = amount/currencies[pos];
        if(buf > 0)
            printf("%d X %d\n", currencies[pos], buf);
        amount -= currencies[pos]*buf;
        pos++;
    }

    return 0;
}

Output

Enter the amount: 2313
1000 X 2
100 X 3
10 X 1
2 X 1
1 X 1

Swap Two Numbers Using Pointers

Here is the code for swapping two numbers using a function which is based on pointers. The basic concept used is pass by reference.

Code

#include <stdio.h>

void swap(int *x, int *y)
{
    int tmp;

    tmp  = *y;
    *y   = *x;
    *x   = tmp;
}

int main()
{
    int a, b;

    printf("Enter two numbers: ");
    scanf("%d%d", &a, &b);

    printf("\nBefore swapping: a = %d and b = %d\n", a, b);
    swap(&a, &b);
    printf("After swapping:  a = %d and b = %d\n", a, b);

    return 0;
}



Output

Enter two numbers: 10 20

Before swapping: a = 10 and b = 20
After swapping:  a = 20 and b = 10