不带循环和递归用 C++ 打印 1 至 100
有几种方法可以在不使用循环的情况下打印数字,例如使用递归函数、goto 语句并在 main() 函数之外创建一个函数。
以下是一个使用 C++ 语言中的 goto 语句打印数字的示例,
示例
#include <bits/stdc++.h>
using namespace std;
int main() {
int count=1;
int x;
cout << "Enter the max value of x : ";
cin >> x;
PRINT:
cout << " " << count;
count++;
if(count<=x)
goto PRINT;
return 0;
}输出
Enter the max value of x : 1
在上述程序中,我们使用 GOTO 语句来打印 1 到 100 的数字,而不使用循环和递归。
PRINT: cout << " " << count; count++; if(count<=x) goto PRINT;
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP