C program to print all Armstrong numbers between given interval using function

Write a function to print all Armstrong numbers between a given interval in C programming. How to print all Armstrong numbers between a given range using functions in C programming. C function to print all Armstrong numbers from 1 to n.

Example:
Input lower limit: 1
Input upper limit: 1000
Output armstrong numbers: 1, 153, 370, 371, 407

Required knowledge

Basic C programming, Function

Logic to print Armstrong numbers using function

We have already seen in our previous posts about what is Armstrong numbers, how to check whether a number is Armstrong number or not and how to print all Armstrong numbers in given interval using loops. Here we will use the above logic to put into a function. Basically printing Armstrong numbers in a given range includes basic three steps:
  1. Find last digit of the given number.
  2. Cube the last digit just found and add it to some variable sum. Repeat the above two steps till the number becomes 0.
  3. Check if the sum equals to original number then the given number is Armstrong otherwise not.
Hence to code down all three steps into function we will define two functions namely isArmstrong and printArmstrong which will check for Armstrong number and print all Armstrong numbers in range respectively.

Program to print Armstrong numbers using function

/**
 * C program to print all armstrong numbers between a given range
 */
 
#include <stdio.h>

/* Function declarations */ 
int isArmstrong(int num);
void printArmstrong(int start, int end);



int main()
{
    int start, end;
    
    /* Reads lowe and upper limit to of armstrong numbers */
    printf("Enter lower limit to print armstrong numbers: ");
    scanf("%d", &start);
    printf("Enter upper limit to print armstrong numbers: ");
    scanf("%d", &end);
    
    printf("All armstrong numbers between %d to %d are: \n", start, end);
    printArmstrong(start, end);
    
    return 0;
}



/**
 * Checks whether the given number is armstrong number or not.
 * Returns 1 if the number is armstrong otherwise 0.
 */
int isArmstrong(int num)
{
    int temp, lastDigit, sum;
    
    temp = num;
    sum = 0;
    
    /* Finds sum of cube of digits */
    while(temp != 0)
    {
        lastDigit = temp % 10;
        sum += lastDigit * lastDigit * lastDigit;
        temp /= 10;
    }
    
    /* Checks if sum of cube of digits equals
     * to original number.
     */
    if(num == sum)
        return 1;
    else 
        return 0;
}



/**
 * Prints all armstrong numbers between start and end. 
 */
void printArmstrong(int start, int end)
{
    /* Iterates from start to end and print the current number 
     * if it is armstrong
     */
    while(start <= end)
    {
        if(isArmstrong(start)) 
        {
            printf("%d, ", start);
        }
        
        start++;
    }
}


Output
Enter lower limit to print armstrong numbers: 1
Enter upper limit to print armstrong numbers: 1000
All armstrong numbers between 1 to 1000 are:
1, 153, 370, 371, 407,

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment