×

Armstrong Number 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 at a minimum once before the condition is evaluated. This loop checks the given condition after the execution of looping statements.

The loop actions or statements will be repeated infinitely as long as the test requirements are met.

Note: DO-WHILE LOOP means “first do it, then check”.

Syntax:

The Syntax of the DO-WHILE loop is:

Do
           { 
            statement(s);
           } 
         while(expression);

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*

Following is Do-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 */
do{
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;
} while(n>0); /* while (test expression or termination condition) */


/* if-else condition to print Armstrong or Not */
if (a ==c)
/* initialising 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 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 the 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.

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

Now, we initialized a ‘do-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 do-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’, the statement of the IF part will print. And, if the condition false, then the statement of 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 ‘do-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 do-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 do-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 do-while loop test expression becomes false. And, the program execution ends.


Related Topics

C++ vardiac() function

In the C++ programming language, the flexibility feature is provided by the variadic function. To understand more about flexibility, let's see the following syntax. Syntax: If we have to add two numbers,...

3 minutes read.

C++ Functions

In other programming languages, a function is referred to as a process or a subroutine. We can design functions to execute any task. A function can be used repeatedly. It...

5 minutes read.

Type conversion in C++

Type conversion is the process of changing the type of variables in a program. The ultimate goal of type conversions is to allow variables of a single data type operate with...

7 minutes read.

Bitwise Operator vs Logical Operator

Bitwise Operator  Bitwise operators perform operations bit by bit on bits.The value is converted to abinary during operations like addition, subtraction, division, and so on. These operations are carried out at the...

3 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.

Priority Queue in C++

Priority Queue in C++ Introduction: We have already come across Queues by concluding that they are linear data structures that follow FIFO (First-In-First-Out) approach. We had also discussed the syntax of queues...

4 minutes read.

Array of Vectors in C++ STL

Prerequisites: C++ STL Arrays and C++ STL Vector. A group of items kept in consecutive memory region is known as an array. It is to group similar objects of the same...

4 minutes read.

C++ storage classes

Storage Classes are used to characterize a variable's or function's characteristics. These characteristics include scope, visibility, and life-time, which allow us to track the presence of a variable over the...

5 minutes read.

Pointer to Object in C++

What is a pointer? A pointer in C++ is used to point the variable by storing the address of the variable. In C++, to print the address of the variable, we...

4 minutes read.

Single dimension array

C++ Array An array is a collection of data (elements) of the same data types. The elements of an array are allocated in contiguous memory allocation. Elements of the array are accessed through...

1 minute read.

Method overriding in C++

What is method overriding? Using the same function in derived class as their base class is referred to as function/method overriding in c++. Method overriding is an example of polymorphism. With...

2 minutes read.

Multiset in C++

Introduction Multisets are part of the C++ STL, or Standard Template Library. In C++, a multiset is a set of associative containers that hold ordered items. Items in a multiset can...

10 minutes read.

How to find the length of the vector in C++

Like dynamic arrays, vectors can automatically adjust their size when an element is added or removed, and the container manages its storage. Because vector items are stored in contiguous storage, iterators...

3 minutes read.

New Operator in C++

Dynamic memory allocation in C++ means manually allocating the memory by the developer duing run-time. The dynamic memory is allocated in the heap section of the RAM, whereas the static...

3 minutes read.

Memset in C++

Memset () is a function in the C++ programming language that fills memory blocks. The value of "ch" is first converted to an unsigned character. In this case, "ch" denotes the...

3 minutes read.

Decltype type Specifier in C++

The primary use of C++ decltype is to inspect the declaration type of an entity in an expression. The auto keyword can declare a particular type of variable, whereas the...

4 minutes read.

Print Table Using Do while Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

How to Reverse a String in C++ using For Loop

For Loop: We may loop through a certain section of C++ code repeatedly using the for loop. A for loop is carried out if the test expression yields a true result. The...

4 minutes read.

Constructor Vs Destructor in C++

C++ Constructor A function Object () { [native code] } is a member function that shares the same name as the class. It is called automatically whenever a class object is...

3 minutes read.

Do-While Loop Examples in C++

Before we move on the examples of Do-While loop, let’s learn little bit about the Do-While loop in C++ language. Do While loop An iterative loop that checks the condition at the...

5 minutes read.