Print Armstrong Number Between Two Intervals in C++

Google Advertisements

Prev Tutorial Next Tutorial

C++ Program to Display Armstrong Number Between Two Intervals

An Armstrong Number is a number where the sum of the digits raised to the power of total number of digits is equal to the number. Armstrong numbers are as follows-

Armstrong Number

3 = 3^1
153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
407 = 4^3 + 0^3 + 7^3 = 64 +0 + 343 = 407
1634 = 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634 

To understand below example, you have must knowledge of following C++ programming topics; If Else Statement in C++ and For Loop in C++. In below code we use pow() function for calculate power of any number number.

In below code we receive two value from user first is lower bound and second is upper bound.

Print Armstrong Number Between Two Intervals in C++

#include<iostream.h>
#include<conio.h>
#include<math.h>

void main()
{
   int first,second,lowerbound, upperbound, digitSum, temp, remainderNum, digitNum ;
   lowerbound=1,upperbound=1;
   clrscr();

   cout<<"Enter Frist Number: ";
   cin>>lowerbound;
    if(lowerbound>100)
   {
   cout>>"Please Enter more than 100 first number";
   }
   else
   {
   cout<<"Enter Second Number: ";
   cin>>upperbound;
   cout<<"Armstrong Numbers between "<<lowerbound<<" and "<<upperbound<<" are: ";
   for(int num = lowerbound; num <= upperbound; num++) {
      temp = num;
      digitNum = 0;
      while (temp != 0) {
	 digitNum++;
	 temp = temp/10;
      }
      temp = num;
      digitSum = 0;
      while (temp != 0) {
         remainderNum = temp%10;
         digitSum = digitSum + pow(remainderNum, digitNum);
         temp = temp/10;
      }
      if (num == digitSum)
         cout<<num<<" ";
   }
   }
  getch();
}

Output

Enter First number: 100
Enter Second number: 1000
Armstrong Numbers between 100 and 1000 are: 152, 370, 371, 407

Output

Enter First number: 15
Please Enter more than 100 first number
c++ program to display armstrong number between two interval
Prev Tutorial Next Tutorial

Google Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. If you want to post your article on our website/FB Group/FB Page please contact on [email protected] or 8076671483

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: [email protected] or 8076671483

Path variable is set for providing path for all Java tools like java, javac, javap, javah, jar, appletviewer which are used in java programming. All these tools are available in bin folders so we set path upto bin folders.

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar.

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user.

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects.

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: [email protected] or 8076671483 Try this Keyword C++ Programs