×

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

Input Iterators in C++

What are input iterators? Input iterators are used in sequence for carrying out input operations where each value is read-only. It is pointed by the iterator and further incremented. All the iterators...

4 minutes read.

Vector resize() in C++

Vector resize() in C++ Vectors are called dynamic arrays and can automatically adjust their size when a component is added or deleted. This container is used for storage. The function modifies the...

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

Ways to Copy a Vector in C++

Vectors in C++ are the same as arrays, along with additional outstanding features than them, like array lists in Java programming language. In Vectors, the size constraint is eliminated, which...

5 minutes read.

Type difference of Character literals in C VS C++

Character literals in C: In C, a character literal is represented by a single character enclosed in single quotes, such as 'a' or 'b'. It is of type int. This means...

5 minutes read.

Binary to Decimal in C++

We must create a software to convert a binary number into an equivalent decimal value given a binary number as input. Example: // C++ program to convert binary to decimal #include < iostream...

3 minutes read.

C++ Void Pointer

A void pointer is a general purpose pointer that can have an address of any data type but is not related to any data type. Void Pointer Syntax: void *ptr;  We can't...

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

Array of sets in C++

Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. A collection of data objects stored in a continuous way is...

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

Private Inheritance in C++

Private inheritance is an inheritance in object-oriented programming (OOP) languages where a subclass derives from a superclass. Still, the derived class does not inherit the public and protected members of...

7 minutes read.

Implementation of a Falling Matrix in C++

In many Hollywood and Bollywood movies, we might have seen a programmer who will be referred to as a hacker all the time for some random reason which the writer...

3 minutes read.

Structured Binding in C++

Structured binding is the new feature of C++ 17. It is used to bind the specified name with an element of the initializer. Structure binding is used to declare multiple...

3 minutes read.

C++ Deque

Definition: Deque or the Doubly ended queue is a data structure or operation performed under queue where insertion and deletion are allowed at both ends. A deque is an ordered collection of...

5 minutes read.

Division in C++

C++ Division Arithmetic Operation In C++ the arithmetic operator / is used for division. This operator takes two operands and returns the result of dividing the left operand by the right...

3 minutes read.

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++ If-else-if

Introduction: If-else-if control statement is an if statement used with an optional else if control statement to check multiple conditions. In this control statement, when any one of the condition returns...

4 minutes read.

Convex hull Algorithm in C++

The intersection of all convex sets containing a certain subset of a Euclidean space, or alternatively, the set of all convex combinations of points in the subset, defines the convex...

4 minutes read.

C++ Program to find the element that occurs once

Write a program to find the element in the array that occurs once. Given that all the numbers in the array are present two times and the array is sorted,...

7 minutes read.

Stack in C++

Stack: The stack is a very popular data structure. It is the form of data structure that follows a particular order called FIFO(First-In-First-Out). In simple words, a stack is an Abstract...

4 minutes read.