从下列程序中求除数的奇数
finding the odd numbers from below programs divisors
本文关键字:程序 更新时间:2023-10-16
#include <iostream>
using namespace std;
int main()
{ int i,num1,num2,sum=0,count=0;
cout<<"Enter a range: ";
cin>> num1>> num2;
for(i = num1;i <= num2; i++)
if(i%3 ==0 ||i%5 ==0)
{
count++;
//sum=sum+i;
cout<<i<<" ";
}
return 0;
}
我编写了一个程序来查找给定范围内3和5的除数,但是现在我想从这些除数中找到奇数。怎么做?假设我在这个程序中输入1到20的范围。我将得到除数:3 5 6 10 12 15 18 20。现在我想从这些数中得到奇数。怎么做?
如果要检查i
是否为奇数,只需将i % 2!= 0
添加到条件中。正如leaf指出的那样,不要使用using namespace std;
,当你在那里时,为什么不实际使用变量count
呢?
#include <iostream>
int main(){
int low{ 0 };
int max{ 0 };
std::cout << "Type in low: ";
std::cin >> low;
std::cout << "Type in max: ";
std::cin >> max;
unsigned int count{ 0u };
for (int i = low; i < max; ++i){
if (i % 2 != 0 && (i % 3 == 0 || i % 5 == 0)){
++count;
std::cout << i << " ";
}
}
std::cout << "nNumber of odd integers that are multiples of 3 or 5 found: " << count << std::endl;
return 0;
}
示例运行:
Type in low: 300
Type in max: 795
303 305 309 315 321 325 327 333 335 339 345 351 355 357 363 365 369 375 381 385
387 393 395 399 405 411 415 417 423 425 429 435 441 445 447 453 455 459 465 471
475 477 483 485 489 495 501 505 507 513 515 519 525 531 535 537 543 545 549 555
561 565 567 573 575 579 585 591 595 597 603 605 609 615 621 625 627 633 635 639
645 651 655 657 663 665 669 675 681 685 687 693 695 699 705 711 715 717 723 725
729 735 741 745 747 753 755 759 765 771 775 777 783 785 789
Number of odd integers that are multiples of 3 or 5 found: 115
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 在C应用程序中运行C++(带有STL)函数
- 使用mongocxx驱动程序时包含头文件问题
- 如何在c++程序中找到函数的地址