The program I'm trying to run checks to see if a number is perfect, abundant or deficient. It's perfect if its factors add up to the number(ie 6 = 3+2+1), deficient if the number is greater than the factors, and abundant if the factors add up to more than the number. This is one of the first times ive coded in c, we had to make a java one and it worked fine when I did it then.

Anyways i get a bus error after it asks for an upper bound...What is a bus error and how can I get this to work?

s84n109:desktop teg3$ gcc -o perfectnumbers perfectnumbers.c
s84n109:desktop teg3$ ./perfectnumbers
Enter an upper bound
10
Bus error


Thanks alot guys

/*
Tom George
9/16/09
perfectnumbers.c
tells whether given number is perfect, deficient, or abundant
*/

#include <stdio.h>
int get_sum_of_divs(int num);
void detect(int x);
int num;

int main()
{
	printf("Enter an upper bound\n");	//Finds the number to check to
	scanf("%d", num);

	int x = 2;

	while(x<=num)		//Checks the numbers for their status
	{

		detect(num);
		x++;
	}

	return 0;
}

int get_sum_of_divs(int num)		//finds the sum of the divisors
{
	int sum = 0;
	int i;
	for(i=1; i<num; i++)
	{
		if(num % i == 0)
			sum = sum+i;
	}
	return sum;
}

void detect(int x)
{
	int sum = get_sum_of_divs(x);
	
	if(x>sum)
		printf("\n%d is deficient\n", x);
	if(sum>x)
		printf("\n%d is abundant\n",x);
	if(sum==x)
		printf("\n%d is perfect\n",x);
}

You need to pass the address of "num" to scanf using the & (address of) operator. This allows scanf to actually modify the variable "num".

Like this:

/* Fixed input*/
    scanf("%d", &num);

Good luck!

Hey thanks dude. It compiled still, but instead of a bus error I get a Floating Point Exception. Any ideas?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.