C Exercise Example 19
100 cases of classic C language
Title: If a number is exactly equal to the sum of its factors, this number is called "complete count." For example, 6 = 1 + 2 + 3. Programming to find all finished within a few 1000.
Program analysis: Refer to: C Exercise Example 14 .
Source Code:
// Created by www.w3big.com on 15/11/9. // Copyright © 2015年 本教程. All rights reserved. // #include<stdio.h> #define N 1000 int main() { int i,j,k,n,sum; int a[256]; for(i=2;i<=N;i++) { sum=a[0]=1; k=0; for(j=2;j<=(i/2);j++) { if(i%j==0) { sum+=j; a[++k]=j; } } if(i==sum) { printf("%d=%d",i,a[0]); for(n=1;n<=k;n++) printf("+%d",a[n]); printf("\n"); } } return 0; }
The above example output is:
6=1+2+3 28=1+2+4+7+14 496=1+2+4+8+16+31+62+124+248