0

ユーザーが指定した特定の範囲で「完全数」をチェックしてプログラムを作成する必要があります。特定の数が完全数かどうかを調べるユーザー定義関数を作成しました。そうである場合は 1 が返され、そうでない場合は 0 が返されます。次に、メイン プログラムで if ステートメントを使用して完全数を出力します。私が得ている問題は、浮動小数点例外エラーです。私は今何をすべきか分かりません。私はいくつかの助けに本当に感謝します. :)。

#include <iostream>
using namespace std;

int isPerfect(int n);

int main(){
    int num=0,perfectCheck=0;
    cout<<"Enter a value for N: ";
    cin>>num;
    cout<<"Perect numbers between 1 and "<<num<<" are : "<<endl;
    for(int i;i<=num;i++){
        perfectCheck=isPerfect(i);
        if(perfectCheck==1){
              cout<<i<<endl;
        }   
    }
    return 0;
}

int isPerfect(int n){
    int sum,mod;
    for(int i;i<n;i++){
        mod=n%i;
        if(mod==0){
            sum=sum+i;
        }
    }
    if(sum==n){
         return 1;
    }
    else{
        return 0;
    }
}
4

1 に答える 1