Friday, February 20, 2009

Write a C program to calculate the sum of factors of a number.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
int no, sum=0, x;
clrscr( );
printf("Enter the required number :");
scanf("%d", &no);
for( x=1; x<=no; x++) {
if(no%x==0) sum=sum+x;
}
printf("\nSum of the factors of %d is: %d", no, sum);
getch( );
}

Output:-
Enter the required number: 10

Sum of the factors of 10 is: 18.
ie., 1+2+5+10 =18

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

No comments:

Post a Comment