不带循环和递归用 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;

更新于: 26-Jun-2020

806 个浏览

开启您的 职业生涯

完成课程获得认证

开始使用
广告
© . All rights reserved.