# i cant see why the code is not working??

This is a discussion on i cant see why the code is not working?? within the C Programming forums, part of the General Programming Boards category; Abundant numer is a number which the sum of it dividers is bigger then the divider itself. 12 : 1+2+3+4+6=16>12 ...

1. ## i cant see why the code is not working??

Abundant numer is a number which the sum of it dividers is bigger
then the divider itself.
12 : 1+2+3+4+6=16>12

I need to build a program which gets a number input.
It prints all the numbers bellow it which could be written as the sum of two
abundant numbers and present its sum.
If there is more then one combination for the sum,we need to print the sum with the smallest number for all the sums .
for example if i enter 54:
There are two possible abbondant number sum 54=12+42 =18+36
The program will print only
54=12+42

I have built a program and who prints only the pairs of the number
but it shows -1 -1 as if on all the range the is no such numbers
??

Code:
```#include <stdio.h>

void  main(){
int num;
int index,tndex,kndex,jndex;
int flag_1,flag_2;
int sum;
int smallest,smallest_ever,smallest_ever_2;
sum=0;
flag_1=0;
flag_2=0;
smallest=-1;
smallest_ever=-1;
smallest_ever_2=-1;
printf("enter number\n");
scanf("%d",&num);

for(index=0;index<num;index++){ //start for main number

for(tndex=0;tndex<index;tndex++){//start for first num
for(kndex=0;kndex<index;kndex++){//start for second num

if (tndex+kndex==index){ //start sum check

for(jndex=1;jndex<tndex;jndex++){//start abbondence check for first num

if (tndex%jndex==0){
sum=sum+jndex;
}

}//end abbondence check for first num
if (tndex<sum){
flag_1=1;
}

sum=0;
for(jndex=1;jndex<kndex;jndex++){//start abbondence check for first num

if (kndex%jndex==0){
sum=sum+jndex;
}

}//end abbondence check for first num
if (kndex<sum){
flag_2=1;
}

if ((flag_2==1)&&(flag_1==1)){  //start flag check
if (kndex>=tndex){
smallest=tndex;

if (smallest<=smallest_ever){
smallest_ever=smallest;
smallest_ever_2=kndex;
}
}
else
{
smallest=kndex;

if (smallest<=smallest_ever){
smallest_ever=smallest;
smallest_ever_2=tndex;
}

}

}//end flag check

} //end sum check

}//end for second num
}//end for first num

printf("%d %d",smallest_ever,smallest_ever_2);

}//end for main number

}//end main```

2. Even with good indentation, it's still hard to read.
Code:
```#include <stdio.h>

void  main(){
int num;
int index,tndex,kndex,jndex;
int flag_1,flag_2;
int sum;
int smallest,smallest_ever,smallest_ever_2;
sum=0;
flag_1=0;
flag_2=0;
smallest=-1;
smallest_ever=-1;
smallest_ever_2=-1;
printf("enter number\n");
scanf("%d",&num);

for(index=0;index<num;index++){ //start for main number
for(tndex=0;tndex<index;tndex++){//start for first num
for(kndex=0;kndex<index;kndex++){//start for second num
if (tndex+kndex==index){ //start sum check
for(jndex=1;jndex<tndex;jndex++){//start abbondence check for first num
if (tndex%jndex==0){
sum=sum+jndex;
}
}//end abbondence check for first num

if (tndex<sum){
flag_1=1;
}

sum=0;
for(jndex=1;jndex<kndex;jndex++){//start abbondence check for first num
if (kndex%jndex==0){
sum=sum+jndex;
}

}//end abbondence check for first num
if (kndex<sum){
flag_2=1;
}

if ((flag_2==1)&&(flag_1==1)){  //start flag check
if (kndex>=tndex){
smallest=tndex;
if (smallest<=smallest_ever){
smallest_ever=smallest;
smallest_ever_2=kndex;
}
}
else
{
smallest=kndex;
if (smallest<=smallest_ever){
smallest_ever=smallest;
smallest_ever_2=tndex;
}
}
}//end flag check
} //end sum check
}//end for second num
}//end for first num

printf("%d %d",smallest_ever,smallest_ever_2);
}//end for main number
}//end main```
1. You really should be off the void main kick by now.
2. Appending 'ndex' to your single letter variables does not make it any more readable.

To break up the massive nesting of loops, use a function which does some of the work. It'll simplify your main()

3. first i take all the possible sums of each number from one till my input number
then i check if they both are abundant .then i take the smallest pair possible

its very straight forward
where is m problem??

4. There's no need to have a loop on kndex; just set kndex = num - tndex and be done with it. (Note that you don't want the two to sum to index, but to num.) Actually index doesn't appear to do anything either, so you can get rid of that too.

5. still i get pairs of -1 -1
Code:
```#include <stdio.h>

#include <stdio.h>

void  main(){
int num;
int index,tndex,kndex,jndex;
int flag_1,flag_2;
int sum;
int smallest,smallest_ever,smallest_ever_2;
sum=0;
flag_1=0;
flag_2=0;
smallest=-1;
smallest_ever=-1;
smallest_ever_2=-1;
printf("enter number\n");
scanf("&#37;d",&num);

for(index=0;index<num;index++){ //start for main number
for(tndex=0;tndex<num;tndex++){//start for first num
kndex=num-tndex;
sum=0;
for(jndex=1;jndex<tndex;jndex++){//start abbondence check for first num (tndex)
if (tndex%jndex==0){
sum=sum+jndex;
}
}//end abbondence check for first num

if (tndex<sum){
flag_1=1;
}

sum=0;
for(jndex=1;jndex<kndex;jndex++){//start abbondence check for second num (kndex)
if (kndex%jndex==0){
sum=sum+jndex;
}

}//end abbondence check for first num
if (kndex<sum){
flag_2=1;
}

if ((flag_2==1)&&(flag_1==1)){  //start flag check
if (kndex>=tndex){
smallest=tndex;
if (smallest<=smallest_ever){
smallest_ever=smallest;
smallest_ever_2=kndex;
}
}
else
{
smallest=kndex;
if (smallest<=smallest_ever){
smallest_ever=smallest;
smallest_ever_2=tndex;
}
}
}//end flag check

}//end for first num

printf("%d %d",smallest_ever,smallest_ever_2);
}//end for main number
}//end main```

6. It is not possible for you to run this code. This code doesn't compile, which means you could possibly be running the previous code. Specifically, you get these errors:
Code:
```\$ gcc -Wall -Wextra -o temp temp.c
temp.c: In function ‘main’:
temp.c:22: error: syntax error before ‘for’
temp.c:22: warning: statement with no effect
temp.c:22: error: syntax error before ‘)’ token
temp.c:62: warning: control reaches end of non-void function
temp.c: At top level:
temp.c:64: error: syntax error before string constant
temp.c:64: warning: type defaults to ‘int’ in declaration of ‘printf’
temp.c:64: error: conflicting types for ‘printf’
temp.c:64: note: a parameter list with an ellipsis can’t match an empty parameter name list declaration
temp.c:64: warning: data definition has no type or storage class```
which must be fixed before you get anything at all that you can run. (I changed void main to int main for you.)

7. i have changed it
i switched the IF to smallest>=smallest_ever
for input 21
i get 9 1210 1110 1110 1110 1110 1110 1110 1110 1110 1110 1110 1110

it should give me only 9 12
why i get those extra numbers???
Code:
```#include <stdio.h>

#include <stdio.h>

int  main(){
int num;
int index,tndex,kndex,jndex;
int flag_1,flag_2;
int sum;
int smallest,smallest_ever,smallest_ever_2;
sum=0;
flag_1=0;
flag_2=0;
smallest=-1;
smallest_ever=-1;
smallest_ever_2=-1;
printf("enter number\n");
scanf("&#37;d",&num);

for(index=0;index<num;index++){ //start for main number
for(tndex=0;tndex<num;tndex++){//start for first num
kndex=num-tndex;
sum=0;
for(jndex=1;jndex<tndex;jndex++){//start abbondence check for first num (tndex)
if (tndex%jndex==0){
sum=sum+jndex;
}
}//end abbondence check for first num

if (tndex<sum){
flag_1=1;
}

sum=0;
for(jndex=1;jndex<kndex;jndex++){//start abbondence check for second num (kndex)
if (kndex%jndex==0){
sum=sum+jndex;
}

}//end abbondence check for first num
if (kndex<sum){
flag_2=1;
}

if ((flag_2==1)&&(flag_1==1)){  //start flag check
if (kndex>=tndex){
smallest=tndex;
if (smallest>=smallest_ever){
smallest_ever=smallest;
smallest_ever_2=kndex;
}
}
else
{
smallest=kndex;
if (smallest>=smallest_ever){
smallest_ever=smallest;
smallest_ever_2=tndex;
}
}
}//end flag check

}//end for first num

printf("%d %d",smallest_ever,smallest_ever_2);
}//end for main number
return 0;
}//end main```

8. Because your print is inside the for loop, rather than after all your calculations are complete?

9. i put it in the main loop because i was told to choose all the numbers which are smaller of the inputed number.

and from these numbers i need to chose the numbers who are a sum of two abundant numbers

10. Yes, but choosing != printing. You do need to compute numbers in a loop. You don't need to print numbers in a loop.

11. i changed If to smallest<=smallest_ever

Code:
```#include <stdio.h>

#include <stdio.h>

int  main(){
int num;
int index,tndex,kndex,jndex;
int flag_1,flag_2;
int sum;
int smallest,smallest_ever,smallest_ever_2;
sum=0;
flag_1=0;
flag_2=0;
smallest=-1;
smallest_ever=-1;
smallest_ever_2=-1;
printf("enter number\n");
scanf("&#37;d",&num);

for(index=0;index<num;index++){ //start for main number
for(tndex=0;tndex<num;tndex++){//start for first num
kndex=num-tndex;
sum=0;
for(jndex=1;jndex<tndex;jndex++){//start abbondence check for first num (tndex)
if (tndex%jndex==0){
sum=sum+jndex;
}
}//end abbondence check for first num

if (tndex<sum){
flag_1=1;
}

sum=0;
for(jndex=1;jndex<kndex;jndex++){//start abbondence check for second num (kndex)
if (kndex%jndex==0){
sum=sum+jndex;
}

}//end abbondence check for first num
if (kndex<sum){
flag_2=1;
}

if ((flag_2==1)&&(flag_1==1)){  //start flag check
if (kndex>=tndex){
smallest=tndex;
if (smallest<=smallest_ever){
smallest_ever=smallest;
smallest_ever_2=kndex;
}
}
else
{
smallest=kndex;
if (smallest<=smallest_ever){
smallest_ever=smallest;
smallest_ever_2=tndex;
}
}
}//end flag check

}//end for first num

printf("%d %d",smallest_ever,smallest_ever_2);
}//end for main number
return 0;
}//end main```

12. i need to keep some how the smallest pair for each number
how to do that

when i put -1 in each of them
there wasnt any smaller number
so it gave me pare of -1

??

13. I dont know how to builld such a program
ive done every thing

i cant see my mistake

for a number 54 number i get 1 and 53 and copies of 54 0 i cant see
what do i need to correct?

Code:
```#include <stdio.h>

#include <stdio.h>

int  main(){
int num;
int index,tndex,kndex,jndex;
int flag_1,flag_2;
int sum;
int smallest,smallest_ever,smallest_ever_2;
sum=0;
flag_1=0;
flag_2=0;
smallest=-1;
smallest_ever=-1;
smallest_ever_2=-1;
printf("enter number\n");
scanf("&#37;d",&num);

for(index=1;index<num;index++){ //start for main number
tndex=index;
kndex=num-tndex;
sum=0;
for(jndex=1;jndex<tndex;jndex++){//start abbondence check for first num (tndex)
if (tndex%jndex==0){
sum=sum+jndex;
}
}//end abbondence check for first num

if (tndex<sum){
flag_1=1;
}

sum=0;
for(jndex=1;jndex<kndex;jndex++){//start abbondence check for second num (kndex)
if (kndex%jndex==0){
sum=sum+jndex;
}

}//end abbondence check for first num
if (kndex<sum){
flag_2=1;
}

if ((flag_2==1)&&(flag_1==1)){  //start flag check
if (kndex>=tndex){
smallest=tndex;
if ((smallest<smallest_ever)&&(smallest>1)){
smallest_ever=smallest;
smallest_ever_2=kndex;
}
}
else
{
smallest=kndex;
if ((smallest<smallest_ever)&&(smallest>1)){
smallest_ever=smallest;
smallest_ever_2=tndex;
}
}
}//end flag check

printf("%d %d",smallest_ever,smallest_ever_2);

}//end for main number
return 0;
}//end main```

14. You need to reset your flags inside the loop, otherwise everything is abundant.