给定一个起始值begin,及一个整数n,输出begin之后(可包含begin)的n个偶数。
c语言
#includeint main() { int begin; int n; scanf("%d%d", &begin, &n); int k = 0; while(k < n) { if (begin % 2 == 0) { ++k; printf("%dn", begin); } ++begin; } }
C语言实现二
#includeint main() { int begin; int n; scanf("%d%d", &begin, &n); if (n <= 0) { printf("0n"); retrun 0; } int start = begin % 2 == 0 ? begin : begin + 1; for (int k = 1; k <= n; ++k) { printf("%dn", start); start += 2; } }
java 语言实现
package com.suanfa; import java.util.Scanner; public class even { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int begin = scanner.nextInt(); int n = scanner.nextInt(); begin = begin % 2 == 0 ? begin : begin + 1; int k = 1; while (k <= n) { System.out.println(begin); begin += 2; k++; } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)