What is Armstrong number?
4 * 4 * 4 + 0 * 0 * 0 + 7 * 7 * 7 = 407 is an Armstrong number.
C++ program which takes input a number and check whether it is Armstrong Number or not
Recommended: Change program logic and do experiment with it for fast learning.
Image view of the program click on image to view large
Logic Explanation:
Concept Used: for loop, if-else statement
Dry Running The Armstrong number Code:
A number in which the sum of cube of its individual digits is equal to the number itself is called Armstrong numberFor Example: 1^3 + 5^3 + 3^3 = 153
4 * 4 * 4 + 0 * 0 * 0 + 7 * 7 * 7 = 407 is an Armstrong number.
C++ program which takes input a number and check whether it is Armstrong Number or not
#include<iostream>
|
Note: Program can be coded in more than one ways above program is very simple so any one can understand the logic of program.
Recommended: Change program logic and do experiment with it for fast learning.
Image view of the program click on image to view large
Find Armstrong number in c++ code |
Logic Explanation:
Concept Used: for loop, if-else statement
- To make logic firstly concept about Armstrong number should be very clear
- We took some variables in which we take input, make calculations and produce results
- Program take input number in variable num and store it in check variable
- In for loop we take mod of num with 10 and stores it in variable Armstrong then we divide it with 10(below dry running will make more clear the working)
- Then we take cubes of Armstrong variable and add it into result variable
- When num=0; then for loop break after for loop using if else statement we test if our result equals to check variable then number is Armstrong else not.
Dry Running The Armstrong number Code:
Let input is equal to 153
variable values before for loop
num=153; check=153; Armstrong=0; result=0;
variable values in for loop line by line
for i=1
Armstrong=3;
num=15;
Armstrong=3*3*3=27
result=0+27=27;
for loop condition check: num is not equal to zero loop will run again
for i=2
Armstrong=5;
num=1;
Armstrong=5*5*5=125
result=27+125=152;
for loop condition: num is not equal to zero loop will run again
for i=3
Armstrong=1;
num=0;
Armstrong=1*1*1=1;
result=152+1=153;
for loop condition: num is EQUAL TO ZERO loop will run again
Loop will break and if else condition will be checked as or result=153 and check=153
if condition will true and program will show output
More C++ Programs Examples:
153 is an Armstrong Number
More C++ Programs Examples:
great explanation!
ReplyDeleteI will be Very thankful to you If you will help me for catalan number program in c++
ReplyDeleteSir I have a question why armstrong is equal to 1 when I divide 5 by 10 is equal 0.5?
ReplyDeletefor i=3
Armstrong=1; //why this is equal to one. 5/10 = 0.5
num=0;
Armstrong=1*1*1=1;
result=152+1=153;
when i=3 then Armstrong is equal to 1.53. and since its an "int" not a float, it will be converted to 1. when i=4 though, Armstrong will be 0.153 and that's 0.
Deleteint is used, not float.
Deletevry gd
ReplyDeletevry gd
ReplyDeletevery nice
ReplyDeleteworks only if the number is less than 1000
ReplyDeleteworks only if the number is less than 1000
ReplyDeleteThanx fahad
ReplyDelete