Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Write a program that reads there integers from the user(keyboard) namely, n, a and b. Your program sohuld print all positive numbers satisfying the following condition:

*The number smaller than n and the ratio of the number and sum of its positive divisors is equal to a/b.

You should print the number in ascending order and print their positive divisors on the sam eline as the numbers.

Example
If the user entered n: 10, a: 1, and b:2 then since only 6 satisfies the condition the output should be 6 and it positive divisors as follows :
6 1 2 3 6

(I am a first class engineering student. I do not want to miss the Basic. Homework, I have to deliver it by the end of the day tomorrow. :/ )

What I have tried:

Objective-C
<pre>int a,b,n;

int main()
{
	printf("please enter a number for n");
	scanf("%d",n);
	printf("please enter a number for a");
	scanf("%d",a);
	printf("please enter a number for b");
	scanf("%d",b);
	
	int i = 1;
	for(i = 1; i<n; i++)
	
	return 0;
}

int sumofdivisors(int a)
{
	int i;
	int sum = 0;
	for(i=1;i<=a;i++)
	{
		if(a%i==0)
		sum += i;
	}
	return sum;
}
Posted
Updated 19-Oct-17 4:03am
v3
Comments
ZurdoDev 19-Oct-17 9:08am    
Then ask a question. It's rude and lazy to post your homework and not even ask a question.
E.Açıkgöz 19-Oct-17 9:12am    
I do not do homework here directly. I tried a bit but I did not.
jeron1 19-Oct-17 10:51am    
Can you accomplish this using paper and pencil?

You should write for every needed output a function which deliver it and than do the output.

You can define an array like this, but you also need to free the memory:
C++
int *arr = 0;// define pointer

int count = fillArrayFunc( arr, a, b, n );

for( int i = 0; i <count; i++ ) {
  int number = arr[i];//access
  printf("The number is: %d", number);
}
free( arr );//release the memory

//somewhere else do math and calc the stuff
int count fillArrayFunc( int *arr, int n, int a, int b) 
{
   arr = malloc( sizeof(int) * count );// alloc on input pointer

  return count;// needed for the output
}

You missed to ask earlier, so you have NOW the problem.
 
Share this answer
 
The following code makes no sense. The first iteration of your loop will terminate your program.
C++
for(i = 1; i<n; i++)

return 0;
 
Share this answer
 
Comments
KarstenK 20-Oct-17 2:33am    
An array starts with 0. See it as offset from the starting point. Watch or read some tutorial to understand that BASIC concept of programming. You MUST understand that!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900