Problem: need to closing my program

Page 1 of 1

3 Replies - 262 Views - Last Post: 03 February 2013 - 09:40 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=310929&amp;s=265a9edec70df24e84558e90f013f1b4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 sunal135

Reputation: 1
• Posts: 41
• Joined: 01-July 09

Problem: need to closing my program

Posted 02 February 2013 - 01:10 PM

bellow is my program to find abundant numbers; the code works I just can get it to exit properly.
```#include <stdio.h>
#include <stdlib.h>

int main(void) {

// Declare and initialize variables.
int n, num, sum=0, i, j;

//prompting user for input
printf("Please enter n followed by n numbers: ");
scanf("%d", &n);

//for loop for reading in the n amount of numbers
for(i=0;i<=n;i++){
scanf("%d", &num);
//setting sum to zero
sum=0;
//for loop for finding factors of num
for(j=1;j<=(num/2);j++){
//if the current j is a factor of num, add it to sum
if((num%j)==0){
sum+=j;
}
}
//print if number is abundant
if (sum>num){
printf("Test case %d: %d is abundant.\n",i+1,num);
}
//print if numer isnt abundant
else{
printf("Test case %d: %d is NOT abundant.\n",i+1, num);

}
}
system("pause");
return 0;
}
```

Any help will be very appreciated

This post has been edited by sunal135: 02 February 2013 - 01:12 PM

Is This A Good Question/Topic? 0

Replies To: Problem: need to closing my program

#2 andrewsw

• I'm not here to twist your niblets

Reputation: 4703
• Posts: 17,437
• Joined: 12-December 12

Re: Problem: need to closing my program

Posted 02 February 2013 - 01:50 PM

Quote

the code works I just can get it to exit properly.

This is vague. You should describe what is happening that shouldn't.

I suspect that you have characters left in the input buffer..

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

Re: Problem: need to closing my program

Posted 02 February 2013 - 02:34 PM

Line #14, change <= n to < n.

The inner for loop is OK with <= but not the outer one.

#4 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,440
• Joined: 14-September 12

Re: Problem: need to closing my program

Posted 03 February 2013 - 09:40 PM

Yes, I certainly agree with Adak on that.

What you would mean by doing <=n is you will be inputting one extra value

say, you choose n=5, you will be inputting 6 values as the for loop runs from i=0 to i=5 and since 5<=5, the value of the 6th number is also taken as input and finally you end up with 6 numbers instead of 5, which is logically incorrect.

One more thing I would suggest is to give the user some time to input the values...in other words, lets not have Please enter n followed by n numbers: in the same sentence. Instead you could try out something like this.

```printf("Please enter the value of n:");
scanf("%d", &n);
.
.
```

In this way, the user will not have any confusion and it will enable a 'user-friendly' environment.

regards,
Raghav