×

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

Bitmasking in C++

Bitmasking is a technique that is used to access a particular bit within the data bytes. When you apply the iterative approach, this phenomenon is used. A bitmask is described...

6 minutes read.

Data Hiding in C++

C++ : High-performance apps can be made using the cross-platform language C++. Bjarne Stroustrup created C++ as an addition to the C language. Programmers have extensive control over memory and system...

3 minutes read.

How to initialize a dynamic array in C++

Regular arrays or static arrays have a predetermined size or fixed size. Change in the size of regular arrays is not possible. The memory size for static arrays determines at compile...

4 minutes read.

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

4 minutes read.

Design Patterns in C++

A design pattern offers an all-encompassing, repeatable answer to the typical issues that arise in software design. Usually, the pattern demonstrates the connections and interactions between several classes or objects....

10 minutes read.

Constructor Overloading in C++

A Constructor is a class member function that is used to initialize the class's objects. Constructors have no return type and are called automatically when an object is formed. Constructor Characteristics Constructors...

3 minutes read.

How to build a program in C++

Building a program is all about creating the program and executing it successfully. There are some steps  precisely, which must be followed to make the program. Step 1: Get an IDE...

4 minutes read.

C++ Do while loop

In this article, we will discuss the C++ Do-While loop with its syntax, working, key features, algorithm, and examples. Do-While Loop: The do-while loop constitutes a specific style of looping construct in...

5 minutes read.

C++ array of Pointers

Array of Pointers: In high-level programming languages like C++, the array's name is its pointer. The name of an array contains an address which is the address of an element. In...

4 minutes read.

Accumulate() and partial_sum() in C++ STL Numeric header

The C++ STL's numeric library includes the numeric header. This library provides efficient numeric arrays, support for random number generation, and fundamental mathematical operations and types. Several of the numeric...

3 minutes read.

C++ Switch

In C++, an expression or variable can be tested against a range of constant values using a switch statement, which is a control flow statement. It offers a productive method...

4 minutes read.

C++ Program to move all zeros to the end of the array

Write a program to move all the zeros in the arr[] to the end. The order of the non-zero elements should not be altered and all the zeros should be...

3 minutes read.

Snake Code in C++

Snake is a popular game that can be played on almost any device and runs on any operating system. In this game, snakes can move in any direction, including left,...

4 minutes read.

C++ Date and Time

The date and time formats in C++ will be covered in this article. Because C++ lacks a proper date and time format, we must rely on the c language. The...

7 minutes read.

C++ operator

In this article, we will discuss about the operators in C++ with their types and examples. An operator is specially a symbol that tells compiler to perform specific manipulation. C++ contains...

5 minutes read.

C++ Overriding

C++ Function Overriding When the base and derive class both contain the same function name and calling the function through derived object invokes derived class function called function overriding. Function overloading is...

1 minute read.

Char Array to String in C++

Regardless of the programming language you use, data structure is critical to the success of your project. Although each programming language has its own collection of data structures, C++ contains a...

4 minutes read.

Hospital Management Project in C++

The following capabilities are required to build a hospital management project: These are as follows: hospitals' names, contact information, and lists of doctors and patients. Activities Supported Hospital Data Print Patients' data to...

4 minutes read.

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

4 minutes read.

C++ this pointer

'this' is a pointer that points to the object for which this function was called. The 'this' pointer holds the memory address of the current object. The 'this' pointer is implicitly passed to...

2 minutes read.