Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, below is the code about perfect number programmed with C++. Nowi need help w

ID: 3610369 • Letter: H

Question

Hi, below is the code about perfect number programmed with C++. Nowi need help with C language for same program below. Just changecode to "C" only.

#include<iostream>          
using namespace std;
int perfect(int num){    
                       
   if(num==1)         //bydefinition
    {
        cout<<"1 is aperfect number"<<endl;
                                          
        return 1;
    }

    int sum=0;
    cout<<"Factors of"<<num<<" are: ";
    for(int i=1; i<num; i++)
    {
        if( (num % i) ==0)       //a number i is a factor ofa number num
       {                        //if i divides into num with no remainder
           sum +=i;           //if its a factor add it with the other factors
           cout<<i<<"    ";
        }
    }
    cout<<endl;

    if(sum ==num)              //if the sum of the factors is num
    {
       cout<<num<<" is a perfectnumber."<<endl;        //it is perfect
    }
   else                                                 //otherwise
    {
       cout<<num<<" is not a perfectnumber."<<endl;      //it's notperfect
    }

    return sum;
}

int main(){

    int input;
    cout<<"Enter a number (Enter -1 to exit):"; //
    cin>>input;
    while(input!=-1)
       {perfect(input);
        cout<<" Enter anumber (Enter -1 to exit): ";
        cin>>input;
       }     
    system("pause");
    return 0;
}

Explanation / Answer

#include<stdio.h>
#include<conio.h>        
using namespace std;
int perfect(int num){    
                       
   if(num==1)         //bydefinition
    {
        printf("1 is a perfectnumber ");
                                          
        return 1;
    }

    intsum=0;
    printf("Factors of %d num are: ",num);
    for(int i=1; i<num; i++)
    {
        if( (num % i) ==0)       //a number i is a factor ofa number num
       {                        //if i divides into num with no remainder
           sum +=i;           //if its a factor add it with the other factors
           printf("%d ",i);
        }
    }
    printf(" ");

    if(sum==num)              //if the sum of the factors is num
    {
        printf("%d is a perfectnumber. ",num);        //it is perfect
    }
   else                                                 //otherwise
    {
        printf("%d is not aperfect number. ",num);      //it's notperfect
    }

    returnsum;
}

int main(){

    intinput;
    printf("Enter a number (Enter -1 to exit):");
    scanf("%d",&input);
    while(input!=-1)
       {perfect(input);
        printf(" Enter a number(Enter -1 to exit): ");
       scanf("%d",&input);
       }      
    getch();
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote