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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP