Armstrong number generation in C++


29. C++ Program to generate Armstrong number within a limit?
Example for an Armstrong number: 153=13+53+33

Program


#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
            {
            int i,r,n,sum=0,temp;
            clrscr();
            cout<<"Enter the limit :";
            cin>>n;
            cout<<"Armstrong numbers are:\n";
            for(i=1;i<n;i++)
                        {
                        temp=i;
                        sum=0;
                        while(temp!=0)
                                    {
                                    r=temp%10;
                                    sum=sum+(r*r*r);
                                    temp=temp/10;
                                    }
                        if(i==sum)
                                    {
                                    cout<<i<<"\n";
                                    }
                        }
            getch();
            }

Output

Enter the limit :1000
Armstrong numbers are:
1
153
370
371
407

No comments:

Post a Comment