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

A positive integer is said to be a perfect number if it equals the sum of its po

ID: 3789077 • Letter: A

Question

A positive integer is said to be a perfect number if it equals the sum of its positive divisors (excluding the number itself). As an example, 6 is a perfect number because its divisors, 1, 2, and 3 sum up to 6. The first four perfect numbers are 6, 28, 496, 8128. Write a C program that asks the user to enter a number and checks if the number is perfect. Your program should run interactively until the user quits. Try to minimize the program execution time by using the least number of iterations for finding the divisors of the user's input. Record and report the number of iterations executed for checking if a number is perfect. Sample Code Execution: Red text indicates information entered by the user Enter a perfect number: 1 Number 1 is not perfect Number of iterations: 0 Do you want to continue (y/n)?y Enter a perfect number: 6 Number 6 is perfect Number of iterations: 3 Do you want to continue (y/n)?y Enter a perfect number: 67 Number 67 is not perfect Number of iterations: 33 Do you want to continue (y/n)?y Enter a perfect number: 496 Number 496 is perfect Number of iterations: 248 Do you want to continue (y/n)?n Goodbye

Explanation / Answer

//

// main.c

// chegg_c

//

// Created by shreya desai on 08/02/17.

// Copyright © 2017 smita desai. All rights reserved.

//

#include <stdio.h>

#include<string.h>

int main(int argc, const char * argv[]) {

  

     // variable declaration

  

  int number,sum=0,i;

  char choice[]= "y";

  

     //loop as long as user input is y or Y

     while (strcmp(choice,"y")==0 || strcmp(choice,"Y")==0)

   {

   sum = 0;

     printf(" Enter a perfect number : ");

     scanf("%d",&number);

  

     // accept only positive number

  while(number<=0)

{

  printf(" Please enter a positive number : ");

  scanf("%d",&number);

}

  

     // loop to find the divisors of number excluding itself

     for(i=1;i<number;i++)

{

  if(number%i==0)

{

sum += i;

}

}

  

     // check if the sum of divisors of a number excluding itself is a number itself(perfect number)

  if (number == sum)

{

  printf(" Number %d is a perfect number",number);

  printf(" Number of iterations are %d",i/2);

}

  else

{

  printf(" Number %d is not perfect",number);

  printf(" Number of iterations are %d",i/2);

}

  

     printf(" Do you want to continue(y/n)?" );

  scanf("%s",choice);

   }

     if (strcmp(choice,"n")==0 || strcmp(choice,"N")==0)

   {

     printf(" Good Bye ");

   }

     return 0;

  

}

output :

Enter a perfect number : 1

Number 1 is not perfect

Number of iterations are 0

Do you want to continue(y/n)?y

Enter a perfect number : 6

Number 6 is a perfect number

Number of iterations are 3

Do you want to continue(y/n)?y

Enter a perfect number : 67

Number 67 is not perfect

Number of iterations are 33

Do you want to continue(y/n)?y

Enter a perfect number : 496

Number 496 is a perfect number

Number of iterations are 248

Do you want to continue(y/n)?n

Good Bye

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