×

Armstrong number using for loop in C++

What is For Loop?

A for loop is a repetitive control structure that allows you to create a loop to execute a specific number of times efficiently.

The syntax that can be used for the for loop in the C++ programming language is given below:

for (initialization statement; termination condition; increment or decrement statement (used for modifying value for further evaluation))
{
    /* main body of the “for” loop */
}

Working of For Loop in C++

In for loop, the initialization command is implemented only once. After that, it checks the test expression of the loop and finds whether the test expression is false or true. And, for loop is dismissed if the test expression is false.   
If the test expression of the 'F' loop becomes true, then the program line provided in the F loop’s body is executed, and the update expression is changed accordingly.
Again, the entire process of evaluation is to be done. The loop executes its body until and unless the value of the test expression in for loop becomes false.

The loop automatically terminates as soon as the test expression result becomes false.

What are Armstrong Numbers?

Armstrong numbers are those natural positive numbers which are equal to the sum of cubes of each digit.
For example

a b c d... = a*n + b*n + c*n + d*n

Following is the For loop program which test a number entered by the user as an input is Armstrong or not:

#include<iostream>
using namespace std;
int main ()
{
    int n, c, a=0, b; 
/* declaring various variables and initialization the value of 'a' as 0 */ 
      cout<<"input any natural number that you want to check as Armstrong or  not: ";
/* here we let user to enter a number via this program line */ 
    cin>>n;
/* assigning the value entered by the user to variable 'n' */
    for (b=n; n>0; n=n/10)
/* for (initialization statement; test expression or termination condition; increment or decrement statement) */
 {
         c=n % 10;
/* initializing assignment operator and assigning the value ‘n % 10’ to ‘c’ */
         a=a+(c*c*c); 
/* initializing assignment operator and assigning the value of ‘c*c*c’ to ‘a’ */
    }
    if(a==b) 
/* initializing an ‘equal to’ operator and putting the condition to check if the value of a is equal to b or not */
         cout<<"The number %d, that you have entered is an Armstrong number.\n"<< b;
/* printing the value of result as an output */
    else // else statement
         cout<<"The number %d, that you have entered is not an Armstrong number.\n"<< b;
/* printing the value of result as an output */
}

OUTPUT

Input any natural number that you want to check as Armstrong or not : 153
The number 153, that you have entered is an Armstrong number

EXPLANATION

Initially, we have declared four variables – n, a, b, c. Then, we initialized the value of variable ‘a’ as 0, it means the initial value of variable ‘a’ will be 0 so that we can avoid the value of variable ‘a’ from going into negative.

After that, we wrote a statement which will allow the user to enter the value for the variable ‘n’ with the ‘scanf’ command. The value in variable n holds that value which we want to test.

In the following line we initialized a ‘for’ loop with the test expression or termination condition (n>0) which means the value of the variable ‘n’ must always be more than 0.

After the for loop, we initialized an if else statement and put the condition (a==n). It means that, if the value of ‘n’ is equal to ‘c’, then the statement in the IF part execute. And, if the condition false, then the statement in the else part will print.

Here, you can see how the above code is execute:

1. n>0 (153>0) here you can see the ‘for’ loop test expression is true,

b=n%10           (b=153%10)     So,  the remainder of n%10 is 3.

a=a +b*b*b     (a = 0 +3*3*3) So, the value of a is 27.

n=n/10            (n=153/10)        So, the value of n is 15.

2. n>0 (15>0) here you can see the for loop test expression is true,

b=n%10            (b=15%10)               So, the remainder of n%10 is 5.

a=a +b*b*b      (a = 27 +5*5*5)      So the value of a is now 152.

n=n/10             (n=15/10)                  So the value of n is now 1.

3. n>0 (1>0) here you can see the for loop test expression is true,

b=n%10            (b=1%10)                 So, the remainder of n%10 is 1.

a=a +b*b*b      (a = 152 +1*1*1)     So, the value of a is now 153.

n=n/10              (n=1/10)                   So, the value of n is now 0.

4. n>0 (0>0) here you can see the for loop test expression becomes false. And, the program execution ends.


Related Topics

Function overloading in C++

Function overloading in C++ As we know that C++ works on the OOP Concepts, that are abstraction, encapsulation, and data hiding, it also uses the other important feature of OOP, which...

8 minutes read.

Free vs delete() in C++

Free vs delete() in C++ In this section, we will learn about the free() function and also create a C ++ program of the delete operator. What is free() Function in C++? In...

4 minutes read.

C++ | C Plus Plus While loop

In this article, we will discuss the C++ while loop with its syntax, use, key features, key points, pseudo code, and examples. What is the While Loop? The “while loop” is a...

4 minutes read.

Divide by Zero Exception in C++

We use exception handling method to handle the divide by zero exception. Dividing a number with zero is generally mathematical error. We have to exception handling method to overcome this...

2 minutes read.

C++ Fork

A new process known as a "child process" is created with the fork system function and runs concurrently with the process that invoked fork() (parent process). Both processes will carry...

4 minutes read.

C++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.

Roadmap to C++ Programming

Introduction There are so many programming languages available in the market, but among them, C++ is something that never lost its charm. It has a powerful impact on the programming world....

4 minutes read.

How to create a stack in C++

A stack is a data structure, which is of linear type. A specific order has to be followed while inserting and deleting the elements from the stack. Generally, stack follows...

5 minutes read.

C++ File Handling

File handling is a mechanism that manipulates the data stored in files. File handling store output data from the program to external file and read file data to the program. There...

3 minutes read.

Desired Capabilities in Selenium Web Driver in C++

A class called Desired Capabilities is used to specify a set of fundamental requirements, such as browser, operating system, and version combinations.to carry out automated testing of a web application...

7 minutes read.

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

Top 5 IDEs for C++ That You Should Try Once

In the past decades, creating an application or interface from the very basic idea, the developer has to struggle a lot for it. Because an application is a combination of...

3 minutes read.

Object Slicing in C++

In this article, we will learn about Object slicing. When an object from a derived class is assigned to an object from a base class in C++, these extra attributes...

3 minutes read.

Find the Size of Array in C/C++ without using sizeof() function

We know that arrays in C/C++ are the most essential data structures as they have the ability to hold the data in a continuous manner line where the address of...

3 minutes read.

C++ Math Functions

C++ Math Functions: Like other programming languages, C++ offers plenty of mathematical functions needed for various purposes. These functions are defined mainly in the math library in C++. Let us...

4 minutes read.

C++ References

C++ References An alias is a reference variable that is another name for a variable already in existence. If a relation is initialized with a variable, it is possible to use...

3 minutes read.

C++ String

A string is a collection of characters. C++ programming language supports both C string as well as standard C++ library string. In C++, string is an object of std::string class. C Style String The C style...

5 minutes read.

Random Number Generator in C++

In programming, we need to frequently create the randomly. For example, a dice game, handing out cards to players, apps for rearranging tunes, etc. T There are two tools available in...

4 minutes read.

C++ Try-Catch

Every useful program will eventually encounter unexpected outcomes. By entering data that are incorrect, users might create mistakes. Sometimes the program's creator didn't consider all of the options or was...

7 minutes read.

Virtual Function Vs Pure Virtual Function

Virtual activity is a member function defined in the foundation phase that can be redefined by acquired classes. Let's have a look at an example: #include <iostream>   #include <bits/stdc++.h> #include <stdlib> using namespace std;   class Base   {    ...

5 minutes read.