Translate

Ad-Horizontal

Search This Blog

Thursday 8 November 2018

How to write a C++ program (Turbo C++) to find and display all the Armstrong Numbers between 0 and n(a term to be entered by the user).

We are gonna see how to write a C++ program (Turbo C++) to find and display all the Armstrong Numbers between 0 and n(a term to be entered by the user).


The following codes have been run and compiled in C++(Turbo C++).

The writing in the right-hand side of this "//" in the codes are called comments of the code. They are not a necessary part of the codes and can be omitted in the codes. They are just mentioned so as to tell you the use/purpose of writing the left-hand side of the "//" or the codes.   In some special cases depending upon the Fond & Size of the text on your device the comments may entered the right hand-side of the "//"(next line) and come in between the main codes. so as to, make it easier for you to understand/distinguish the comments from the main codes; we have written the main code and comments in different sizes.
Example: Main codes: "  #include<iostream.h>  "
                 Comments: "   //It is a header file and used to call the input-output classes(Pre-defined library in C++).  " .


The Variables are user initialized with zero, so that they won't store any garbage value and hamper the final output of the program.


For more accurate, users may replace "int" and "float" with "long" and "double" respectively.


An Armstrong number is an integer such that the sum of the cubes of its digits is equal to the number itself.


The code are as follows:


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr()
{
int lim=0,num=0,n=0,rem=0,sum=0,count=0;
cout<<"Enter the Last Term for checking:"<<"\n";
cin>>lim;
cout<<"The Armstrong Numbers from 0 to "<<lim<<" are : "<<"\n";
for(n=0;n<=lim;n++)
{
num=n;
while(num!=0)
{
rem=num%10;
sum=sum+(rem*rem*rem);
num=num/10;
}
if(sum==n)
{
cout<<n<<"\n";
count=count+1;
}
sum=0;
}
if(count==0)
cout<<"There is no Armstrong numbers from 0 to "<<lim<<" . "<<"\n";
else
cout<<"The total Number of Armstrong Numbers from 0 to "<<lim<<" are = "<<count<<"\n";



Program:




The 9th line should be = " cout<<"The Armstrong Numbers from 0 to "<<lim<<" are : "<<"\n"; ".




The 5th line from ending should be = " cout<<"There is no Armstrong numbers from 0 to "<<lim<<" . "<<"\n"; "




The 5th line from ending should be = " cout<<"There is no Armstrong numbers from 0 to "<<lim<<" . "<<"\n"; "






Output:




















Hope that  the Blog have helped you in your work. 


Do visit again to see more Coding related blogs.

No comments:

Post a Comment