Explanation
The program prints Armstrong numbers between two numbers entered by the user. An Armstrong number is a number where the sum of the cubes of its digits is equal to the number itself.
Header
The code begins with including the standard input-output header files using #include <stdio.h>
and #include <math.h>
for mathematical functions.
Program Description
The program prints a description message for the user to understand its functionality and explains what an Armstrong number is.
User Input
The user is prompted to enter two numbers between which they want to find and print Armstrong numbers. The input is read using scanf("%d %d", &d, &m);
.
Armstrong Number Calculation
The program finds and prints all Armstrong numbers between d
and m
using a for
loop:
for (j = d; j <= m; j++)
: Loops through each number fromd
tom
.int o, n, e = 0, s = 0;
: Declares and initializes variables.o
is used to extract digits,n
holds each digit,e
counts the number of digits, ands
sums the cubes of the digits.o = j;
: Stores the current number ino
.while (o != 0)
: Counts the number of digits in the current number by dividingo
by 10 untilo
is 0.e += 1;
: Increments the digit count.o /= 10;
: Divideso
by 10 to remove the last digit.for (o = j; o > 0; o = o / 10)
: Extracts each digit of the current number and sums the cubes of the digits.n = o % 10;
: Extracts the last digit ofo
.s += pow(n, e);
: Adds the cube of the digit to the sums
.if (j == s)
: Checks if the sum of the cubes of the digits is equal to the original number.printf("\t %d ", j);
: Prints the current number if it is an Armstrong number.
Program End
The program ends with printf("\n");
to print a new line and return 0;
to indicate successful execution.
Code
/*
* -----------------------------------------------------------
* Logic Building with Computer Programming (CSU1128)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <stdio.h>
#include <math.h>
int main()
{
int d, m, j;
printf("\n\n Program to print the armstrong number. An armstrong number is ");
printf("the number where the sum of cube of each digit is equal to the number itself. ");
printf("Example 370 = 27 + 343 + 0 = 370 \n\n");
printf("Enter two number between which you want to print armstrong numbers ");
printf("(Example: 21 1200) - and press enter: ");
scanf("%d %d", &d, &m);
printf("\n Armstrong numbers between %d and %d are: \n", d, m);
for (j = d; j <= m; j++)
{
int o, n, e = 0, s = 0;
o = j;
while (o != 0)
{
e += 1;
o /= 10;
}
for (o = j; o > 0; o = o / 10)
{
n = o % 10;
s += pow(n, e);
if (j == s)
printf("\t %d ", j);
}
}
printf("\n");
return 0;
}
Output
Program to print the armstrong number. An armstrong number is the number where the sum of cube of each digit is equal to the number itself. Example 370 = 27 + 343 + 0 = 370
Enter two number between which you want to print armstrong numbers (Example: 21 1200) - and press
enter: 100
300
Armstrong numbers between 100 and 300 are: 125 153 216