0

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);
}
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by TGeorge824
0

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!

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.