×

Armstrong Number using While Loop in C++

What is while Loop?

A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the condition no longer met.

The syntax that can be used for the “while” loop in the C++ programming language is following:

while(condition)
     {
      statement(x);
     }

Working of While loop in C++

The test expression which is to be entered into the "while" loop is written between parentheses.

There are currently two options of test expression. If the expression is true, then the "while" loop will be executed automatically and the evaluation procedure will be repeated until the test expression is true. And, if the test expression becomes false then the "while" loop will be terminated and the entire "while" loop procedure will be abandoned.

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 While 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, b, a = 0;
/* declaring various variables and initialization the value of 'a' as 0 */ 
    cout<<"Input any natural number that you want to check whether it is an Armstrong number or not: "<<; 
/* here we let user to enter a number via this program line */ 
Cin>>c;
/* assigning the value entered by the user to variable 'n' */
n=c;
/* initializing assignment operator and assigning the value ‘c’ to ‘n’ */
/* Loop to calculate the sum of the cubes of its digits */
while(n>0) /* while (test expression or termination condition) */
{
b=n % 10;
/* initializing assignment operator and assigning the value of ‘n %10’ to ‘b’ */
a=a +b*b*b;
/* initializing assignment operator and assigning the value of ‘a +b*b*b’ to ‘a’ */
n=n/10;
}
/* if else condition to print Armstrong or Not */
if (a ==c)
/* 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"<<c;
else // else statement
cout<<"The number %d, that you have entered is not an Armstrong number.\n"<<c;
/* printing the value of result as an output */
getch ();
}

Output

Input any natural number that you want to check whether it is an Armstrong number or not: 407
The number 407, that you have entered is an Armstrong number.

Explanation of above code:

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.

In the following line we initialized a ‘while’ 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 while loop, we initialized an if else statement and put the condition (n=c). 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 (407>0) here you can see the ‘while’ loop test expression is true,

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

a=a +b*b*b     (a = 0 +7*7*7) So, the value of a is 343.

n=n/10            (n=407/10)        So, the value of n is 40.

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

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

a=a +b*b*b      (a = 343 +0*0*0)      So the value of a is now 343.

n=n/10             (n=40/10)                  So the value of n is now 4.

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

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

a=a +b*b*b      (a = 343 +4*4*4)     So, the value of a is now 407.

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

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


Related Topics

C++ STL Components

C++ STL Components In today’s article, we are going to learn about all the points things that is related to STL in C++ so stay connected because you are going to...

6 minutes read.

C++ Program to find the largest number formed from an array

Given an array, write a program to find the largest number that will be formed from the elements of the array. Arrangement should be done in such a way that...

4 minutes read.

Hello World Program in C++

The steps for “Hello World” C++ program are as follows: Write a C++ code given below in an editor. Save the file with .cpp Compile the code using C++ compiler or using online...

3 minutes read.

fscanf() Function in the C++

In the C++ programming language, the fscanf() method can be used to read data from a file stream. Syntax: The syntax for the fscanf() function in the C++ programming language is as...

3 minutes read.

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++ 1/2 decimal equal is 0.555555555555555555555 .... An indefinite number of lengths will require the storage...

4 minutes read.

Boost split in C++ library

Boost::split in C++ library Boost offers strong tools for adding mature, well-tested libraries to the C++ standard library. The boost: split function, which is a component of the Boost string algorithm...

2 minutes read.

C++ Dijkstra Algorithm Using the Priority Queue

In this article we will be finding the shortest routes from a source vertex in a graph to all vertices in the graph, given a graph and a source vertex...

5 minutes read.

Stringstream in C++ and its applications

In this tutorial, we will explore what the stringstream in C++ is. We will also learn its application. What is stringstream? With the aid of a stringstream, user can read from a...

2 minutes read.

How is multiset implemented in C++

Similar to sets, multisets are an associative container type where several items may share the same values. Associative containers implement instantly searchable sorted data structures with O(log n) complexity. In a multiset,...

5 minutes read.

Fast Input and Output in C++

In competitive programming, it's critical to read input as quickly as possible in order to save time. "Warning: Big I / O data, be aware of certain languages (but most...

3 minutes read.

System() function in C++

As a part of the c/c+ standard library, the system() function passes commands to be executed by the operating system’s command processor or terminal and returns the completed command. We...

2 minutes read.

Programs to Print Pyramid Patterns in C++

We will explore how to use code to print a variety of patterns utilizing stars (*), numbers (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,.…)  and alphabets...

7 minutes read.

Diamond Pattern Using Do-While loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

5 minutes read.

How to Reverse a String in C++ using Do-While Loop

Strings In C++, a string is an object that represents a group (or sequence) of various characters. Strings are part of the standard string class in C++ (std::string). The characters of...

4 minutes read.

C++ Mutable keyword

C++ mutable keyword Mutable class storage specifier in C++ Storage class specifiers in C are auto, register, static, and external. Typedef is also considered as a specifier of the storage...

4 minutes read.

C++ program to read string using cin.getline()

C++ program to read string using cin.getline() C++ getline() is a standard library feature for reading a string or a line from an input source. A getline() function gets characters from...

3 minutes read.

Sizeof() Operators in C++

sizeof() Operators in C++ The sizeof() operator in C++ defines the size of variables, constants, or data types. It is a unique operator that manipulates other operators and returns the size...

4 minutes read.

Reverse a String using Stack C/C++

Reverse the given string using stack. To turn "tutorialandexample" into "elpmaxednalairotut," for instance. Here is a straightforward stack-based technique for reversing strings. Algorithm: 1) Make a stack that is empty. 2) Push each character...

3 minutes read.

Backtracking Time Complexity in C++

Introduction to Backtracking Backtracking is an important part of programming languages, and with the help of backtracking, we can do many operations in an advanced data structure. For doing major operations,...

4 minutes read.

C++ Comments

Comment/Remark A Comment or a Remark is text that is ignored by the compiler yet is beneficial to programmers. Code is usually annotated with comments for future reference. They are treated...

3 minutes read.