C Program To Find Armstrong Numbers Between 1 and 500 using Function

Lets write a C program to find Armstrong number or Narcissistic number from 1 to 500 using function.

Problem Statement
Write a C program to print out all Armstrong numbers or Narcissistic number between 1 and 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number. Hint: Use function / method.

An Armstrong number or Narcissistic number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself.

Example 1:
If number = 153
It has 3 digits: 1, 5 and 3. So n = 3.
result = 13 + 53 + 33 = 1 + 125 + 27 = 153.
So the original number 153 is equal to the result. So it’s an Armstrong Number.

Example 2:
If number = 1634
It has 4 digits: 1, 6, 3 and 4. So n = 4.
result = 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1634.
So the original number 1634 is equal to the result. So it’s an Armstrong Number.

Related Read:
C Program to print Armstrong Numbers between 1 and 500

Video Tutorial: C Program To Find Armstrong Numbers From 1 To 500 using Function


[youtube https://www.youtube.com/watch?v=V0Ws8aS4GwI]

YouTube Link: https://www.youtube.com/watch?v=V0Ws8aS4GwI [Watch the Video In Full Screen.]


To count the digits in a number

    int n = 0, temp;

    temp = num;

    while(temp)
    {
        temp = temp / 10;
        n++;
    }

To Iterate through the digits in the number

    while(num)
    {
        rem = num % 10;
        sum = sum + pow(rem, n);
        num = num / 10;
    }

To know above code logic, please visit and watch the video tutorial present at C Program to Check Armstrong Number.

Full Source Code: C Program To Find Armstrong Numbers Between 1 and 500 using Function

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

float armstrong(int);

int main()
{
    int count;

    for(count = 1; count <= 500; count++)
    {
        if(count == armstrong(count))
        {
            printf("%d is a Armstrong number\n", count);
        }
    }

    return 0;
}

float armstrong(int num)
{
    int rem, n = 0, temp;
    float sum = 0.0;

    temp = num;

    while(temp)
    {
        temp = temp / 10;
        n++;
    }

    while(num)
    {
        rem = num % 10;
        sum = sum + pow(rem, n);
        num = num / 10;
    }

    return(sum);
}

Output:
1 is a Armstrong number
2 is a Armstrong number
3 is a Armstrong number
4 is a Armstrong number
5 is a Armstrong number
6 is a Armstrong number
7 is a Armstrong number
8 is a Armstrong number
9 is a Armstrong number
153 is a Armstrong number
370 is a Armstrong number
371 is a Armstrong number
407 is a Armstrong number

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

float armstrong(int);

int main()
{
    int count;

    for(count = 1; count <= 500; count++)
    {
        if(count == armstrong(count))
        {
            printf("%d is a Armstrong number\n", count);
        }
    }

    return 0;
}

float armstrong(int num)
{
    int rem;
    float sum = 0.0;

    while(num)
    {
        rem = num % 10;
        sum = sum + (rem * rem * rem);
        num = num / 10;
    }

    return(sum);
}

Output:
1 is a Armstrong number
153 is a Armstrong number
370 is a Armstrong number
371 is a Armstrong number
407 is a Armstrong number

Logic To Find Armstrong Numbers Between 1 and 500 using Function

Using for loop(inside main method) we pass value(from 1 to 500) to armstrong function one by one. The value is present in variable count.

Function armstrong checks the number of digits present in the number(we call it as n) and then separates individual digits of the number and multiplies all the individual digits n times and adds them all and finally returns the sum.

If the armstrong function returned number and the value present in count are same then the value present in count is an Armstrong number, else we pass on to check the next number selected by for loop.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program to print Armstrong Numbers Between Two Integers

An Armstrong number or Narcissistic number is an n-digit base b number such that the sum of its (base b) digits raised to the power n is the number itself.

Example 1:
If number = 153
It has 3 digits: 1, 5 and 3. So n = 3.
result = 13 + 53 + 33 = 1 + 125 + 27 = 153.
So the original number 153 is equal to the result. So it’s an Armstrong Number.

Example 2:
If number = 1634
It has 4 digits: 1, 6, 3 and 4. So n = 4.
result = 14 + 64 + 34 + 44 = 1 + 1296 + 81 + 256 = 1634.
So the original number 1634 is equal to the result. So it’s an Armstrong Number.

Related Read:
C Program To Reverse a Number
Basic Arithmetic Operations In C
while loop in C programming
if else statement in C
Calculate Sum of Digits: C Program
C Program to Check Armstrong Number
C Program to print Armstrong Numbers between 1 and 500

C Program to print Armstrong Numbers Between Two User Entered Integers


[youtube https://www.youtube.com/watch?v=lDc4Lx5-Ltc]

YouTube Link: https://www.youtube.com/watch?v=lDc4Lx5-Ltc [Watch the Video In Full Screen.]


To count the digits in a number

        num = temp = count;
        n   = sum  = 0;

        while(temp)
        {
            temp = temp / 10;
            n++;
        }

To Iterate through the digits in the number

        while(num)
        {
            rem = num % 10;
            sum = sum + pow(rem, n);
            num = num / 10;
        }

To know above code logic, please visit and watch the video tutorial present at C Program to Check Armstrong Number.

Full Source Code: C Program to print Armstrong Numbers Between Two User Entered Integers

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

int main()
{
    int count, limit, rem, temp, num, n;
    float sum;

    printf("Enter 2 integer numbers\n");
    scanf("%d%d", &count, &limit);

    printf("\nArmstrong numbers between %d and %d\n", count, limit);

    while(count <= limit)
    {
        num = temp = count;
        n   = sum  = 0;

        while(temp)
        {
            temp = temp / 10;
            n++;
        }

        while(num)
        {
            rem = num % 10;
            sum = sum + pow(rem, n);
            num = num / 10;
        }

        if(count == sum)
        {
            printf("%d is Armstrong number\n", count);
        }

        count++;
    }

    return 0;
}

Output:
Enter 2 integer numbers
1
99999

Armstrong numbers between 1 and 99999
1 is Armstrong number
2 is Armstrong number
3 is Armstrong number
4 is Armstrong number
5 is Armstrong number
6 is Armstrong number
7 is Armstrong number
8 is Armstrong number
9 is Armstrong number
153 is Armstrong number
370 is Armstrong number
371 is Armstrong number
407 is Armstrong number
1634 is Armstrong number
8208 is Armstrong number
9474 is Armstrong number
54748 is Armstrong number
92727 is Armstrong number
93084 is Armstrong number

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program to print Armstrong Numbers between 1 and 500

Write a program to print out all Armstrong numbers or Narcissistic number between 1 and 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number.

Related Read:
Nested While Loop: C Program
C Program to Check Armstrong Number

For Example:
407 = (4*4*4)+(0*0*0)+(7*7*7)
407 = (64) + (0) + (343)
407 = 407
Hence, 407 is a Armstrong number.

Nested While Loop
In this program we are using nested while loop to check for Armstrong numbers from 1 to 500.

Outer while loop loops from 1 to 500, by incrementing value of count by 1. Inner while loop checks every value of count to determine if the value is a Armstrong number or not.

Logic for Finding Armstrong Number

We’ve explained the logic to find Armstrong number in detail, in our previous video tutorial. Kindly visit C Program to Check Armstrong Number

C Program to print Armstrong Numbers between 1 and 500


[youtube https://www.youtube.com/watch?v=PthvSOapNxc]

YouTube Link: https://www.youtube.com/watch?v=PthvSOapNxc [Watch the Video In Full Screen.]


Source Code: C Program to print Armstrong Numbers between 1 and 500

#include < stdio.h >

int main()
{
    int num, count = 1, rem, sum;

    while(count <= 500)
    {
        num = count;
        sum = 0;

        while(num)
        {
            rem = num % 10;
            sum = sum + (rem * rem * rem);
            num = num / 10;
        }

        if(count == sum)
        {
            printf("%d is a Armstrong number\n", count);
        }

        count++;
    }

     return 0;
}

Output:
1 is a Armstrong number
153 is a Armstrong number
370 is a Armstrong number
371 is a Armstrong number
407 is a Armstrong number

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program to Check Armstrong Number

Lets write a C program to check whether user entered number is Armstrong or not.

Armstrong number: is a number that is equal to the sum of cubes of its individual digits.

Example: If user input the number 371. It’s individual digits are 3, 7 and 1. Lets cube each digit: 33 + 73 + 13 = 27 + 343 + 1 = 371.

The user entered number 371 is equal to the sum of cube of its individual digits. So 371 is a Armstrong number.

Logic

If user enters number as 371.
1. Lets use modular division on that number.
371 % 10 = 1;
2. Lets cube the result(reminder) and store it inside a variable sum.
(1*1*1) = 1. So sum = 1.
3. Divide the number(371) by 10.
371 / 10 = 37. (Remember, when you divide a number by integer number, it returns only the integer part of the result).

Now, sum = 1, number = 37.

Lets repeat step 1, 2 and 3.
1. Lets use modular division on that number.
37 % 10 = 7;
2. Lets cube the result(reminder) and store it inside a variable sum.
(7*7*7) = 343. So sum = 1 + 343.
3. Divide the number(37) by 10.
37 / 10 = 3.

Now, sum = 344, number = 3.

Lets repeat the steps 1,2 and 3 one more time.
1. Lets use modular division on that number.
3 % 10 = 3;
2. Lets cube the result(reminder) and store it inside a variable sum.
(3*3*3) = 27. So sum = 344 + 27.
3. Divide the number(3) by 10.
3 / 10 = 0.

Now, sum = 371, number = 0;

Since number is 0, which means false, the while loop exits.

Next using if-else we check if the user entered number is equal to the sum of cubes of its individual digits.

Related Read:
C Program To Reverse a Number
Basic Arithmetic Operations In C
while loop in C programming
if else statement in C
Calculate Sum of Digits: C Program
C Program to print Armstrong Numbers between 1 and 500

C Program to Check Armstrong Number


[youtube https://www.youtube.com/watch?v=wfhGh44CVhw]

YouTube Link: https://www.youtube.com/watch?v=wfhGh44CVhw [Watch the Video In Full Screen.]


Source Code: C Program to Check Armstrong Number

#include < stdio.h >

int main()
{
    int num, rem, sum = 0, temp;

    printf("Enter an integer number\n");
    scanf("%d", &num);

    temp = num;

    while(num)
    {
        rem = num % 10;
        sum = sum + (rem * rem * rem);
        num = num / 10;
    }

    if(temp == sum)
    {
        printf("%d is armstrong number\n", temp);
    }
    else
    {
        printf("%d is not armstrong number\n", temp);
    }

    return 0;
}

Output 1:
Enter an integer number
371
371 is armstrong number

Output 2:
Enter an integer number
563
563 is not armstrong number

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert