V
V
Vlad2422018-09-15 19:11:32
C++ / C#
Vlad242, 2018-09-15 19:11:32

C++, how to solve this in c++???

Guys, help.
Look, for example, I write the number 8 and it is divisible by 1,2,4,8 and I need to write a code that can itself display the numbers in descending order. Only the code should be for any number (there, for example, 10.6 ...), the program itself must find what the number is divisible by and then display them in descending order.
I first wrote like this but the code does not quite do what I need.

#include <iostream>
using namespace std;
 
void reverse()
{
  double x;
  if (cin >> x)
  {
    reverse();
    cout << x << "\n";
  }
}
 
int main()
{
  reverse();
  return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sddvxd, 2018-09-16
@Vlad242

#include <iostream>

int main(){
  std::cout << "Enter the number: ";
  int number;
  std::cin >> number;
  int buffer = number;
  if(number <= 0) return 1;
  double multi_factor = 1;
  std::cout << std::endl;
  while((buffer = number * multi_factor) >= 1){
    std::cout << buffer << " ";
    multi_factor /= 2;
    if(buffer % 2 != 0) break;
  }

  std::cin.get();
  std::cin.get();

  return 0;
}

D
Dmitry, 2018-09-15
@demon416nds

Why do you need double if you are going to work with integers?
the simplest option in the loop from number to 1 is to check the remainder of the division
and display if it is equal to zero.

for (int i=x;i>0;i--)
if (x % i==0)
cout << i << "\n";

I haven’t written in C for a long time so I can be wrong in the syntax

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question