日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

位运算使奇数+1 偶数-1_C ++程序打印从1到N的所有偶数和奇数

發布時間:2025/3/11 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 位运算使奇数+1 偶数-1_C ++程序打印从1到N的所有偶数和奇数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

位運算使奇數+1 偶數-1

Problem: Take input from the user (N) and print all EVEN and ODD numbers between 1 to N.

問題:從用戶那里輸入(N),并打印1至N之間的所有偶數和奇數編號。

Solution:

解:

  • Input an integer number (N).

    輸入一個整數( N )。

  • Run two separate loops from 1 to N.

    從1到N運行兩個單獨的循環。

  • In the first loop, check the condition to check EVEN numbers and print them.

    在第一個循環中,檢查條件以檢查偶數并打印它們。

  • In the second loop, check the condition to check ODD numbers and print them.

    在第二個循環中,檢查條件以檢查奇數編號并打印它們。

  • To check EVEN/ODD number – find the remainder dividing by 2, if it is 0 then the number will be an EVEN number, else the number will be an ODD number.

    要檢查EVEN / ODD號碼-找到除以2的余數,如果為0,則該號碼將為EVEN號碼,否則該號碼將為ODD號碼。

C++ program:

C ++程序:

// C++ program to print all // Even and Odd numbers from 1 to N#include <iostream> using namespace std;// function : evenNumbers // description: to print EVEN numbers only. void evenNumbers(int n) {int i;for (i = 1; i <= n; i++) {//condition to check EVEN numbersif (i % 2 == 0)cout << i << " ";}cout << "\n"; }// function : oddNumbers // description: to print ODD numbers only. void oddNumbers(int n) {int i;for (i = 1; i <= n; i++) {//condition to check ODD numbersif (i % 2 != 0)cout << i << " ";}cout << "\n"; }// main code int main() {int N;// input the value of Ncout << "Enter the value of N (limit): ";cin >> N;cout << "EVEN numbers are...\n";evenNumbers(N);cout << "ODD numbers are...\n";oddNumbers(N);return 0; }

Output

輸出量

RUN 1: Enter the value of N (limit): 11 EVEN numbers are... 2 4 6 8 10 ODD numbers are... 1 3 5 7 9 11RUN 2: Enter the value of N (limit): 50 EVEN numbers are... 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 ODD numbers are... 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49

翻譯自: https://www.includehelp.com/cpp-programs/print-all-even-and-odd-numbers-from-1-to-n.aspx

位運算使奇數+1 偶數-1

總結

以上是生活随笔為你收集整理的位运算使奇数+1 偶数-1_C ++程序打印从1到N的所有偶数和奇数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。