Wednesday, December 1, 2010

Algorithm, Program & Output for Sum of factors of a number

Algorithm
  1. Start
  2. Read a number ‘n’ > 0
  3. Assign sum with 0, i with 1
  4. If i<=n then repeat steps (5) and (6) else go to (7)
  5. If modulus(n,i) = 0
then sum Ɵ sum + i and go to (6)
else go to (6)
  1. i Ɵ i + 1
  2. Print sum of factors of n is sum
  3. Stop
 Program
#include <stdio.h>

main()
{
            int n,i,sum ;

            clrscr() ;
            printf("\tProgram to find Sum of factors of a Number\n") ;
            printf("\n  Input\n\n") ;
            printf("Enter a Number : ") ;
            scanf("%d",&n) ;
            sum = 0 ;
            for (i=1;i<=n;i++)
                if ((n%i) == 0)
                        sum += i ;
            printf("\n  Output\n\n") ;
            if (n <= 0)
                        printf("Enter greater than Zero") ;
            else
                        printf("Sum of factors of %d is : %d",n,sum) ;
            getch() ;
}


Output

Program to find Sum of factors of a Number

  Input

Enter a Number : 36

  Output

Sum of factors of 36 is : 91

Program to find Sum of factors of a Number

  Input

Enter a Number : 0

  Output

Enter greater than Zero

No comments:

Post a Comment