Computer Science‎ > ‎

C Program: Check for Armstrong Numbers

C Program to check for Armstrong Numbers

/*The following program checks whether a number is armstrong number or not and also displays all armstrong numbers
from 1 to 1000. Armstrong numbers are such numbers whose sum of the cubes of digits is equal to the number itself.
For Example
371 is an Armstrong number
371=3*3*3+7*7*7+1*1*1
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int numcheck;
int choice;
int rem;
int sum;
int temp;
printf("Enter Choice 1 to check whether a number is armstrong or not and Choice 2 to display all armstrong numbers from 1 to 1000\n");
scanf("%d",&choice);//It's a menu driven program and we work as per user's choice
switch(choice)
{
case 1:
    printf("Enter the number\n");
    scanf("%d",&numcheck);
    temp=numcheck;/*We create a temporary copy of the number we will require to divide the number zero which changes it
    and we need the actual number to compare after the loop operations.*/
    sum=0;//variable to hold the sum of cubes of digits
    while(temp>0)
    {
        rem=temp%10;//We find the digit at last place by finding the remainder after division by 10
        sum=sum+rem*rem*rem;
        temp=temp/10;
    }
    if(sum==numcheck)//Finally we check if the sum of the cubes of the digits equals the number or not
        printf("The Number is an armstrong number\n");
    else
        printf("The Number is not an armstrong number\n");
    break;
case 2:
    for(numcheck=1;numcheck<=1000;numcheck++)
    {//A loop which allows us to move through all numbers from 1 to 1000
    //Now we perform the same operations on all numbers
    temp=numcheck;
    sum=0;
    while(temp>0)
    {
        rem=temp%10;
        sum=sum+rem*rem*rem;
        temp=temp/10;
    }
    if(sum==numcheck)
        printf("The Number %d is an armstrong number\n",numcheck);

    }
break;
}
getch();
return 0;
}
/*A sample run of the program was carried out and it was found to give result as
Enter Choice 1 to check whether a number is armstrong or not and Choice 2 to display all armstrong numbers from 1 to 1000
2
The Number 1 is an armstrong number
The Number 153 is an armstrong number
The Number 370 is an armstrong number
The Number 371 is an armstrong number
The Number 407 is an armstrong number
*/

Other Programs which you might find useful:


C Program to Reverse A String

C Program: Building an Expression Evaluator
C Program: Check for Armstrong Numbers
C Program: Check whether a string is a Palindrome or not
C Program: Common Operations on Sets - Union, Intersection, Difference, Cardinal Product
C Program: Computing exp(x), sin(x), cos(x), tan(x) using series expansions
C Program: Computing the Area of a Circle
C Program: Computing the Upper Triangular Matrix and Lower Triangular Matrix
C Program: Demonstrating File Handling Functions
C Program: Demonstrating Operations on Matrices - Addition, Subtraction, Multiplication, Inversion, Finding Determinants
C Program: Demonstrating the use of Bitwise Operators
C Program: Displaying a Histogram of word frequencies (unigram)
C Program: Distance Vector Routing Algorithm using Bellman Ford's Algorithm
C Program: Numerical Computing - The Gaussian Elimination Method
C Program: Numerical Computing - Implementing the Newton Raphson Method
C Program: Numerical Computing - the Bisection Method
C Program: Numerical Computing - The Gaussian Elimination Technique from Linear Algebra
C Program: Numerical Computing - the Jacobi Method
C Program: Printing the Pascal Triangle
C Program: Reversing the order of words in a sentence
C Program: Solving Simultaneous Equations in Two Variables
C Program: Source Code for computing the GCD(HFC) of two numbers
C Program: Source Code for Solving Quadratic Equations
C Program: Source code to solve the Josephus Problem
C Program: Sudoku Solver
C Program: The Usage of Command Line Arguments
C Program: Using the Sieve of Eratosthenes to print Prime Numbers


 






C Program to Reverse A String

C Program: Building an Expression Evaluator
C Program: Check for Armstrong Numbers
C Program: Check whether a string is a Palindrome or not
C Program: Common Operations on Sets - Union, Intersection, Difference, Cardinal Product
C Program: Computing exp(x), sin(x), cos(x), tan(x) using series expansions
C Program: Computing the Area of a Circle
C Program: Computing the Upper Triangular Matrix and Lower Triangular Matrix
C Program: Demonstrating File Handling Functions
C Program: Demonstrating Operations on Matrices - Addition, Subtraction, Multiplication, Inversion, Finding Determinants
C Program: Demonstrating the use of Bitwise Operators
C Program: Displaying a Histogram of word frequencies (unigram)
C Program: Distance Vector Routing Algorithm using Bellman Ford's Algorithm
C Program: Numerical Computing - The Gaussian Elimination Method
C Program: Numerical Computing - Implementing the Newton Raphson Method
C Program: Numerical Computing - the Bisection Method
C Program: Numerical Computing - The Gaussian Elimination Technique from Linear Algebra
C Program: Numerical Computing - the Jacobi Method
C Program: Printing the Pascal Triangle
C Program: Reversing the order of words in a sentence
C Program: Solving Simultaneous Equations in Two Variables
C Program: Source Code for computing the GCD(HFC) of two numbers
C Program: Source Code for Solving Quadratic Equations
C Program: Source code to solve the Josephus Problem
C Program: Sudoku Solver
C Program: The Usage of Command Line Arguments
C Program: Using the Sieve of Eratosthenes to print Prime Numbers