简体   繁体   English

如何在没有因子的情况下打印数字的因子数请查看我的代码

[英]How Can I print The number of Factor of a Number without there factors please have a look on my code

#include<iostream>

using namespace std;

int main(){

    int N,i;
    int count=0;
    cin>>N;

    for(i=1;i<=N;i++){
        if(N%i==0){
            cout<<i<<" ";
            count++;
        }
  
    }

}

INPUT 6输入 6

OUTPUT 1 2 3 6 4 OUTPUT 1 2 3 6 4

I want to print the Number of Factors which is 4 in this case Before the Factors.我想在因子之前打印在这种情况下为 4 的因子数。 I did it few days ago but i forget that now please help me..几天前我做了,但我忘记了,现在请帮助我..

The easiest way to achieve what you want by putting the factors in the array.通过将因子放入数组中来实现您想要的最简单的方法。 like: int k =0;比如:int k =0; arr[k++] = i; arr[k++] = i;

now, first print the count and then traverse and print the array.现在,首先打印计数,然后遍历并打印数组。

There is no need to cout on line 12 and it will work fine:).无需在第 12 行 cout,它会正常工作:)。 That is printing the "i" which are the numbers you don't need.那就是打印你不需要的数字“i”。

cout<<i<<" ";

This line remove it.此行将其删除。

Edit: I reread and found that you wanted to print the factors after it.编辑:我重读并发现你想在它之后打印因子。 You could save it in a vector or an array.您可以将其保存在向量或数组中。

#include <vector> // At the top of your program
std::vector<int> factors; // At the declaration step
factors.push_back(i); // Where your cout << "i" initially was
// After the cout of your count
for ( auto &factor : factors ){
    std::cout << factor << " ";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 C++ 中查找数字的因子,但我想在第一行打印因子的数量 - Finding factors of number in C++ but I want to print number of factors on first line C++ 指向对象的指针堆数组,如何将字符串传递给特定的 object? 有一些代码请看一下: - C++ Heap array of pointers to objects, how can I pass a string to a specific object? Have some code please take a look: 如何使用(返回地址)函数打印“*cp”??(请参考我的代码) - How can I print " *cp " using (Return address )function??( please refer my code) 如果没有找到素数,如何打印“否”? - How can I print “No” if there is no prime number found? 如何针对输入的大量字符串优化我的代码 - How can I optimize my code for large number of strings on input 如何打印端口号的值? - How can I print the value of a Port Number? 我试图通过我的代码找到任何数字的质因数。 但是对于大数字,我的代码不会终止。 为什么? - I'm trying to find prime factors of any number through my code. But for large numbers, my code is not terminating. Why? C++ 因子程序:输出给定因子的数量 - C++ Factor Program: Outputting Number of Factors Given 找出n个数字的公因子数(不包括“ 1”作为公因子) - Find the number of common factors for n numbers excluding “1” as common factor 我试图找到一个数字的阶乘因子 - I am trying to find the factors of factorial of a number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM