Hi Vimrod,
Good atttempt so far, but you have to look at some things in your code
1. you have the defnition of the function sum which you havent invoked in your main(not calling anywhere in the program)
2. The expected output so called perfet number or a number near to it. your implemented logic doesnt contain the needed steps to find the perfect or nearly perfect number.
Here i post the algorithm to find the perfect number ( defined accorfing to you)
algorithm to find perfect no
1. for 1 to n/2
2. if n%i==0 then s+=i
3. if n-s<0 then diff[j]=s-n,num[j++]=n
else diff[j]=n-s,num[j++]=n
1. input a, b
2. for i between a and b
call perfect no passing i
3. now search for the least no in diff ..if diff[i] is least then num[i] is the answer so called perfect or near to perfect no.
Hope this helps.
Just check-out the following code if this solves your problem i hope it does.
CODE
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
int diff[10],num[10];
int j;
void calc(int n)
{
int i,s;
for(i=1;i<(n/2);i++)
{
if((n%i)==0)
s+=i;
if((n-s)<0){diff[j]=s-n;num[j++]=n;}else {diff[j]=n-s;num[j++]=n;}
}
}
void main()
{
int a,b;
printf("enter the min and max number");
cin>>a>>b;
for(int i=a;i<b;i++)
calc(i);
int temp=diff[0];
int no=0;
for(i=0;i<j;i++)
{
if(temp>diff[i+1])
{
temp=diff[i+1];
no=i+1;
}
}
printf("the number is %d",num[no]);
getch();
}