Advertisement
Guest User

Untitled

a guest
Sep 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3. Online C Compiler.
  4. Code, Compile, Run and Debug C program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8. #define _CRT_SECURE_NO_WARNINGS
  9. #include <stdio.h>
  10. #include <math.h>
  11.  
  12. int main()
  13. {
  14. int num;
  15. int sum = 0;
  16. int i; //Loop variable
  17. char keepGoing;
  18.  
  19. // prompts user to enter a number
  20. printf("Enter a number between 100 and 10,000\n");
  21. scanf("%d", &num);
  22.  
  23. //if number isnt within range
  24. while (num < 100 || num>10000) {
  25. printf("This number is outside the accepted range.\n");
  26. printf("Enter a number between 100 and 10,000\n");
  27. scanf("%d", &num);
  28. }
  29.  
  30. keepGoing = 'y';
  31. sum = 0;
  32. //if number is within range
  33. if (num >= 100 && num <= 10000) {
  34.  
  35. while (keepGoing == 'y') {
  36.  
  37.  
  38.  
  39. for (i = 1; i < num; i++) {
  40.  
  41.  
  42. if (num%i == 0) {
  43. sum = i + sum;
  44. }
  45.  
  46.  
  47.  
  48. if (num == sum) {
  49. printf("Number %d is perfect.\n", num);
  50. printf("Number of iterations are %d.\n", i);
  51. }
  52. else {
  53. printf("Number %d is not a perfect number.\n", num);
  54. printf("Number of iterations are %d.\n", i);
  55. }
  56.  
  57.  
  58. }
  59. printf("Do you want to continue? (y/n) ");
  60. scanf("%s", &keepGoing);
  61.  
  62. if (keepGoing == 'n') {
  63. printf("Goodbye.\n");
  64. }
  65. else {
  66. printf("Enter a perfect number \n");
  67. scanf("%d", &num);
  68. }
  69.  
  70. }
  71. }
  72.  
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement