The following is my simple attempt at generating Armstrong numbers. But it only outputs "1". What might be wrong?
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
int main()
{
clrscr();
int r;
long int num = 0, i, sum = 0, temp;
cout << "Enter the maximum limit to generate Armstrong number ";
cin >> num;
cout << "Following armstrong numbers are found from 1 to " << num << "\t \n";
for(i=1;i<=num;i++)
{
temp = i;
while( temp != 0 )
{
r = temp%10;
sum = sum + r*r*r;
temp = temp / 10;
}
if ( i == sum ) {
cout << i;
sum = 0;
}
}
getch();
return 0;
}
You need to always set sum = 0
inside the for-i-loop.