Showing posts with label Number Checking. Show all posts
Showing posts with label Number Checking. Show all posts

Wednesday, 27 April 2016

Number Swapping

/*
 * C Program to swap two numbers.
 */

// Includes
#include <stdio.h>

/*
 * Description : Function to swap 2 integers using call by reference.
 * Parameters  : iPtr_num1, iPtr_num2 - pointer to numbers to be swapped.
 * Returns     : Nothing. Swapped numbers are directly reflected in calling function.
 */
void swap(int *iPtr_num1, int *iPtr_num2)
{
    float temp;
    
    // Check if both are pointing to same location.
    // No need to swap in such scenario.
    if (iPtr_num1 == iPtr_num2)
    {
        return;
    }
    
    // Uses temporary variable to swap 2 numbers.
    temp = *iPtr_num1;
    *iPtr_num1 = *iPtr_num2;
    *iPtr_num2 = temp;
}

/*
 * Description : Function to swap 2 integers using addition and subtraction.
 * Parameters  : iPtr_num1, iPtr_num2 - pointer to numbers to be swapped.
 * Returns     : Nothing. Swapped numbers are directly reflected in calling function.
 */
void swapUsingAddSub(int *iPtr_num1, int *iPtr_num2)
{
    // Check if both are pointing to same location.
    // No need to swap in such scenario.
    if (iPtr_num1 == iPtr_num2)
    {
        return;
    }
    
    // Swap using addition and subtraction method
    *iPtr_num1 = *iPtr_num1 + *iPtr_num2;
    *iPtr_num2 = *iPtr_num1 - *iPtr_num2;
    *iPtr_num1 = *iPtr_num1 - *iPtr_num2;
}

/* swapUsingBitwise
 * Description : Function to swap 2 integers using bitwise operators.
 * Parameters  : iPtr_num1, iPtr_num2 - pointer to numbers to be swapped.
 * Returns     : Nothing. Swapped numbers are directly reflected in calling function.
 */
void swapUsingBitwise(int *iPtr_num1, int *iPtr_num2)
{
    // Check if both are pointing to same location.
    // No need to swap in such scenario.
    if (iPtr_num1 == iPtr_num2)
    {
        return;
    }
    
    // Swap using bitwise operators
    *iPtr_num1 = *iPtr_num1 ^ *iPtr_num2;
    *iPtr_num2 = *iPtr_num1 ^ *iPtr_num2;
    *iPtr_num1 = *iPtr_num1 ^ *iPtr_num2;
}

/* swapUsingMultDiv
 * Description : Function to swap 2 integers using multiplication and division.
 * Parameters  : iPtr_num1, iPtr_num2 - numbers to be swapped.
 * Returns     : Nothing. Swapped numbwes are directly reflected in calling function.
 */
void swapUsingMultDiv(float *iPtr_num1, float *iPtr_num2)
{
    // Check if both are pointing to same location.
    // No need to swap in such scenario.
    if (iPtr_num1 == iPtr_num2)
    {
        return;
    }
    
    // Swap using multiplication and division method
    *iPtr_num1 = (*iPtr_num1) * (*iPtr_num2);
    *iPtr_num2 = (*iPtr_num1) / (*iPtr_num2);
    *iPtr_num1 = (*iPtr_num1) / (*iPtr_num2);
}

int main()
{
    int i_num1, i_num2;
    float f_num1, f_num2;
    
    printf("Enter numbwes to be swapped : ");
    scanf("%d %d",&i_num1, &i_num2);
    
    // Swap 2 numbers and print
    swap(&i_num1, &i_num2);
    printf("Swapped                   (%d,%d)\n",i_num1, i_num2);
    
    swapUsingAddSub(&i_num1, &i_num2);
    printf("Swapped using add-sub     (%d,%d)\n",i_num1, i_num2);
    
    swapUsingBitwise(&i_num1, &i_num2);
    printf("Swapped using bitwise xor (%d,%d)\n",i_num1, i_num2);
    
    // For multiplication-division method, arguments needs to be float
    f_num1 = i_num1;    // implicit type casting will take place.
    f_num2 = i_num2;    // implicit type casting will take place.
    swapUsingMultDiv(&f_num1, &f_num2);
    printf("Swapped using mult-div    (%g,%g)\n",f_num1, f_num2);

}

Thursday, 1 October 2015

Check For Number Palindrome

Program Logic

  • A palindrome number is the one which is same as the number when the digits in number are reversed.
  • We keep a copy of original number.
  • Try to create reverse number as follows :
    1. Let us say number is 121. Reverse number is initially 0.
    2. We take the last digit i.e. 1.
       Iteration 1 - last digit = 1
       Iteration 2 - last digit = 2
       Iteration 3 - last digit = 1
    3. Multiply reverse number by 10 shifting current digits to right.
       Iteration 1 - reverse number = 0*10 = 0
       Iteration 2 - reverse number = 1*10 = 10
       Iteration 3 - reverse number = 12*10 = 120
    4. Now add last digit to reverse number.
       Iteration 1 - reverse number = 0 + 1 = 1
       Iteration 2 - reverse number = 10 + 2 = 12
       Iteration 3 - reverse number = 120 + 1
    5. Remove the last digit from original number
       Iteration 1 - Original number 12
       Iteration 2 - Original number 1
       Iteration 3 - Original number 0
    5. Repeat steps 2-4 until original number becomes 0.
  • Now check if reverse number and copy of original number are the same. If yes, we have a palindrome number :D

/*
 * C Program to check if a number is palindrome or not.
 */

// Includes
#include <stdio.h>

// Declarations
/* isPalindrome
 * Description : A function to check if given number is palindrome or not.
 * Parameters  : i_number - number which needs to be checked for palindrome.
 * Returns     : unsigned short 0 - if a number is not palindrome.
 *                              1 - if a number is palindrome.
 */
unsigned short isPalindrome(int i_number);

// Definitions
int main()
{
    int i_number;
    printf("Enter numbwe that needs to be checked for palindrome : ");
    scanf("%d",&i_number);
    
    // Check for palindrome.
    if (isPalindrome(i_number))
    {
        printf("Entered number %d is palindrome.\n",i_number);
    }
    else
    {
        printf("Entered number %d is not palindrome.\n",i_number);
    }
}

unsigned short isPalindrome(int i_number)
{
    int i_reverseNumber = 0;     // to store reverse number.
    int i_copyNumber = i_number; // to store number copy for modification.
    int i_lastDigit;
    
    while (i_copyNumber)         // unless number becomes 0
    {
        // Take the last digit.
        i_lastDigit = i_copyNumber %10;
        
        // Shift digits to right by 1 place.
        i_reverseNumber *= 10;
        
        // Add last digit on right
        i_reverseNumber += i_lastDigit;
        
        // Remove last digit from original number.
        i_copyNumber /= 10;
    }
    
    return i_reverseNumber == i_number;

}

Saturday, 8 August 2015

Armstrong Number Checking

What is an armstrong number ?
A number n is said to be an armstrong number of order o (o is number of digits in the number n) if it meets following condition.

d1d2d3d4d5d6d7 d....do = d1o + d2o + d3o + d4o + d5o + d6o + .... doo  = n
where d1,d2,d3,d4,d1, ... are digits in a number.
           o is number of digits in a number.
           n is the number that needs to be checked if it is an armstrong number.

Example :
1 = 11 = 1
2 = 21 = 2
.
9 = 21 = 9
153 = 13 + 53 + 33  = 1 + 125 + 27 = 153
371 = 33 + 73 + 13  = 27 + 343 + 1 = 371
.
1634 = 14 + 64 + 34  + 44 = 1 + 1296 + 81 + 256 = 1634

Algorithm : 
The steps involved in finding is a number is an armstrong or not are as follows :
1. Find the number of digits of the number - this will be our order, say O.
2. Take the last digit.
3. Calculate power O of this digit - use any function from here.
4. Add this to some predeclared sum.
5. Remove the last digit from the number.
6. Repeat steps 2 to 5 until number becomes 0.
7. Compare sum and the number - if both are same, it is armstrong number.

/*
 * C Program to check if a number is armstrong or not.
 */

// Includes
#include <stdio.h>

// Declarations
/* isArmstrong
 * Description : A function to check if given number is armstrong or not.
 * Parameters  : i_number is a number to be checked for armstrong.
 * Returns     : unsigned short 0 - for non armstrong number,
                                1 - for armstrong number.
 */
unsigned short isArmstrong(int i_number);

// Definitions
int main()
{
    int i_number;
    printf("Enter number to be checkd for armstrong : ");
    scanf("%d",&i_number);
    
    // Check if the number is armstrong
    if (isArmstrong(i_number))
    {
        printf("Entered number %d is armstrong.",i_number);
    }
    else
    {
        printf("Entered number %d is not armstrong.",i_number);
    }
}

unsigned short isArmstrong(int i_number)
{
    // numbers copy, so that when we remove last digit every time,
    // we don't loose the original number.
    int i_numberCopy = i_number;

    // stores the sum of powers if digits of a number.
    unsigned int sum = 0;
    
    unsigned int   i_numberOfDigits;
    unsigned short us_lastDigit;
    
    // Negative numbers are not armstrong numbers.
    if (i_number > 0)
    {
        i_numberOfDigits = getNumberOfDigits(i_number);
        if (i_numberOfDigits > 0)
        {
            while (0 != i_numberCopy)
            {
                // Take last digit
                us_lastDigit = i_numberCopy%10;
                
                // Calculate the power and add it in sum
                sum += bitwisePower(us_lastDigit, i_numberOfDigits);
                
                // remove the last digit
                i_numberCopy /= 10;
            }
            
            // Check if number is same as that of sum of powers of digit.
            if (i_number == sum)
            {
                return 1;
            }
        }
    }
   
    return 0 ;
}




Thursday, 6 August 2015

Find Number Of Digits In A Number

/*
 * C Program to find number of digits in a number.
 * Pre-Condition : Program needs to be written without using any library function.
 * Example       : If number is 1234 - output will be 4.
 */

// Include
#include <stdio.h>

// Declarations
/* getNumberOfDigits
 * Description : A function that gives number of digits in a given number.
 * Parameters  : i_number is the number of which number of digits needs to be found.
 * Return      : unsigned int data which is number of digits.
 */
unsigned int getNumberOfDigits(int i_number);

// Definitions
int main()
{
    int i_number;
    printf("Enter number of which number of digits needs to find : ");
    scanf("%d",&i_number);
    printf("Number of digits in %d : %u\n",i_number,getNumberOfDigits(i_number));
}

unsigned int getNumberOfDigits(int i_number)
{
    unsigned int count = 0;
    // As long as i_number does not become 0 - repeat
    while (i_number)
    {
        count++;
        i_number/=10;   // reduce the number by 1 digit
    }
    
    return count;
}