This blog is under construction

Sunday 23 June 2013

C program to print the abundant numbers from 1 to N

What is an abundant number?
If the sum of all divisors(excluding the number itself) of a number is greater than the number itself, then that number is called abundant number.

1, 2, 3, 4, 6 are the divisors of 12.
1 + 2 + 3 + 4 + 6 = 16. (sum of divisors)
16 > 12.  So, 12 is an abundant number.


Write a C program to print the abundant numbers from 1 to N.


  #include <stdio.h>
  int main () {
        int n, i, j, sum = 0;

        /* get the input from the user */
        printf("Enter your input:");
        scanf("%d", &n);

        /* print abundant no from 1 to n */
        for (i = 1; i <= n; i++) {
                for (j = 1; j <= i/2; j++) {
                        if (i % j == 0) {
                                sum = sum + j;
                        }
                }

                /* print abundant number alone */
                if (sum > i)
                        printf("%d ", i);
                sum = 0;
        }
        printf("\n");
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input:100
  12 18 20 24 30 36 40 42 48 54 56 60 66 70 72 78 80 84 88 90 96 100 



No comments:

Post a Comment