3 Replies - 172 Views - Last Post: 03 February 2013 - 09:40 PM Rate Topic: -----

#1 sunal135  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3360
  • View blog
  • Posts: 11,397
  • 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..
Was This Post Helpful? 0
  • +
  • -

#3 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#4 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • 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);
printf("Please enter n numbers:");
.
.


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

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1