如何输出变量可除数
How do I output the numbers which the variable is divisable with?
对不起,我的电子课程作业有问题。我被要求编写一个程序,确定用户输入的数字是否是素数,然后说出它的素数,或者给出输入可除的数字。我应该使用哪种语法来实现它
例如:"6可与6;3;2;1整除"这是我迄今为止的代码:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int P,count=0;
cout<<"Enter a number:n"; /*Asks for user input*/
cin>>P; /* User input P*/
for(int a=1;a<=P;a++)
{
if(P%a==0)
{
count++;
}
if(count==2)
{
cout<<"Prime number.n"; /* Provided option when the number is prime number*/
}
else
{
cout<<" Not prime number n"; /* This is where I am supposed to provide the numbers input is divisible with*/
}
getch();
}
}
不完全确定出了什么问题,但您的程序打印出类似的东西
Enter a number:
6
Not prime number
Prime number.
Not prime number
Not prime number
Not prime number
Not prime number
此外,<conio.h>
不是标准C++。我建议你写这样的东西(注意使用std::vector
来累积除数(:
#include <iostream>
#include <vector>
int main()
{
int n;
std::cout << "Enter a number: ";
std::cin >> n;
if (n == 0) {
"Not a prime; 0 is divisible by all integers";
return 0;
}
std::vector<int> divisors;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
divisors.push_back(i);
}
}
if (divisors.size() > 2) {
std::cout << "Not a prime; divisors:" << std::endl;
for (std::vector<int>::iterator it = divisors.begin(); it != divisors.end(); it++) {
std::cout << *it << std::endl;
}
} else {
std::cout << "Prime" << std::endl;
}
return 0;
}
相关文章:
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 使用静态变量的递归调用的不同输出
- C++具有模板成员变量的类. 和参数内存输出
- 更改输出的变量
- 如何在C++中访问作用域的变量输出?
- 简化C++包括变量名称和函数输出的模式
- C++ - 输出与初始化变量不同?
- 是否可以在C++中基于程序集输出(.dll或.exe)定义变量
- 当对字符变量使用toupper()时,所述char变量输出多个字符.我该如何防止这种情况发生
- 如何让变量随机输出四个单词之一
- 输出(用于 GraphViz)提升图顶点及其属性,使用具有私有变量的类作为捆绑属性
- 如何将C++输出捕获到 Bash 变量?
- 当我在循环内打印变量时无限输出
- 字符串比较在与变量一起使用时给出不同的输出
- 双变量输出 32767 始终代替用户输入
- 使用std :: ofstream中的c 中的float变量输出
- C++:Xcode中未初始化的变量输出为0
- C++转义字符和引用变量输出混淆
- 矢量类C++的变量输出不正确
- 硬编码变量输出中没有反斜杠,但在文件输出中存在