This blog is under construction

Sunday 23 June 2013

C program to print non-abundant numbers from 1 to N

Write a C program to print non-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);

        for (i = 1; i <= n; i++) {
                for (j = 1; j <= i/2; j++) {
                        if (i % j == 0) {
                                sum = sum + j;
                        }
                }

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



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input:15
  1 2 3 4 5 7 8 9 10 11 13 14 15 



No comments:

Post a Comment