Click here to Skip to main content
15,891,885 members
Please Sign up or sign in to vote.
1.75/5 (4 votes)
See more:
i want to code factors of a number in descending order
I want to use only 3 functions without recursions
I want to get the factors of a number in descending order as an output

What I have tried:

i tried to do it without the recursion.Cos i dont want recursion
Posted
Updated 14-Aug-20 20:56pm
Comments
Patrice T 14-Aug-20 23:02pm    
Here, we help you to fic your code.
Show your code and state the problems you have.
Isaac David 14-Aug-20 23:33pm    
#include <iostream>

using namespace std;

int get_number(void);
void print_divisors (int input_integer);
bool user_repeat (void);

int main (void)
{
int input_integer;

do
{
input_integer = get_number();
print_divisors(input_integer);
}
while (user_repeat());
}

int get_number(void)
{
int num;
cout<<"Enter a number\n";
cin>>num;
if(num<1)
cout<<"This number is unacceptable. Please enter another number"<<endl;
="" else
="" return="" num;
=""
}

void="" print_divisors(int="" input_integer)
{

="" for(int="" factor="1;factor<=input_integer;factor++)
" {
="" if(input_integer%factor="=" 0)
="" cout<<factor<<"="" ";
="" }
}

bool="" user_repeat="" (void)
{
="" char="" answer;
="" if((answer="" !="y" ||answer="" )||(answer="" ))
="" cout<<"please="" respond="" with="" y="" (or="" y)="" for="" yes="" and="" n="" n)="" no"<<endl;
="" do
="" cout<<"would="" you="" like="" to="" try="" another="" integer="" (y="" n)?\n="" cin="">>answer;;
if(answer=='Y' || answer == 'y')
cout<< main();


}while(answer =='N' || answer == 'n');

}


I want this output
Please enter a positive integer: 18
18 9 6 3 2 1
Would you like to try another integer (Y/N)? y
Please enter a positive integer: -44
-44 is not a positive integer.
Please enter a positive integer: 109
109 1
Would you like to try another integer (Y/N)? m
Please respond with Y (or y) for yes and N (or n) for no.
Would you like to try another integer (Y/N)? N
Patrice T 15-Aug-20 0:22am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Patrice T 15-Aug-20 0:26am    
Don't forget to tell what is the problem with the code.
Dave Kreskowiak 14-Aug-20 23:28pm    
You mean your homework assignment says you can't use recursion.

I see your code in comments, note that was corrupted during paste in comment.
The code contain numerous syntax errors, without even speaking about its logic.
A few rules:
- Never call main() from any place in code.
C++
cout<< main();

- Any command ends with a semicolon
C++
cout<<"would you like to try another integer (y n)?\n cin>>answer;;  // 2 semicolon at the end is an error
// prefer
cout<<"would you like to try another integer (y n)?\n; cin>>answer;

- Advice: do not put multiple command on same line of code, it works, but it also make code more difficult to read.
C++
cout<<"would you like to try another integer (y n)?\n ;
cin>>answer;

- Quotes always goes by pairs
C++
cout<<"would you like to try another integer (y n)?\n";
cin>>answer;

-
C++
int get_number(void)
{
	int num;
	cout<<"Enter a number\n";
	cin>>num;
	if(num<1)
		cout<<"This number is unacceptable. Please enter another number"<<endl;
		// this code path is missing a return command
	else
		return num;
}


Advice: rewrite the program as a single piece of code in main() until it works, then analyze the code to see which part can be made a function (the part that make the function will be replaced in main code by the function name.
How to make a function for a piece of code is a skill you have learn.

Learn debugger as soon as possible.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v3
Comments
Stefan_Lang 17-Aug-20 11:33am    
Have a 5.

You might also mention that calling main() at the end of the loop is a recursion! Didn't he say he was supposed not to use recursion? ;-p
Patrice T 17-Aug-20 11:37am    
Thank you.
you are free to say it in your own solution.
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
 
Share this answer
 
Comments
Patrice T 15-Aug-20 2:04am    
Hi OG,
code is in comments.
OriginalGriff 15-Aug-20 2:24am    
Yeah, I saw.
Did you read it?
cout << main();

OMG ...
Patrice T 15-Aug-20 2:30am    
Yes I know :) typical from very beginners.
But at least he did some work.
Isaac David 20-Aug-20 12:30pm    
yo man its not my grades. Its just a pratice qns. The program i sent is my version of the program. Im not getting paid either

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