用于生成Armstrong数字的简单C++代码

Irf*_*fan 1 c++ sequences

以下是我生成阿姆斯壮数字的简单尝试.但它只输出"1".可能有什么问题?

#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; 
}
Run Code Online (Sandbox Code Playgroud)

Hen*_*rik 5

您需要始终sum = 0在for-i-loop中设置.