n 次奇數 C 程式


給定一個數字 N,我們需要找到 N 次奇數。

奇數是指不能被 2 完全整除且餘數不為零的數。例如 1、3、5、7、9 ….

如果我們仔細觀察偶數列表,我們還可以將它們表示為

(2*1)-1=1, (2*2)-1=3,( 2*3)-1=5, (2*4)-1=7,….(2*N)-1.

因此,為解決該問題,我們可以簡單地將數字 N 乘以 2 並從結果中減去 1,即構成一個奇數。

示例

Input: 4
Output: 7
The 4th odd number is 1, 3, 5, 7..
Input: 10
Output: 19

演算法

START
   STEP 1 -> Declare and assign an integer ‘n’.
   STEP 2 -> Print n*2-1(odd number).
STOP

示例

#include <stdio.h>
int main(int argc, char const *argv[]){
   int n = 10;
   //for odd numbers we can simply subtract 1 to the even numbers
   printf("Nth odd number = %d", n*2-1);
   return 0;
}

輸出

Nth odd number = 19

更新日期:2019-9-23

875 次瀏覽

開啟你的職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.