本文概述
- C ++
- C
- C#
- C ++
- C
- Java
- C#
可以使用任何循环条件(例如for(), while(), do-while())以多种方式打印数字。但是, 无需使用循环(使用递归函数, goto语句)也可以做到这一点。
使用递归函数打印从1到100的数字已经在套装1。在这篇文章中, 讨论了其他两种方法:
使用goto语句:
C ++
#include <
iostream>
using namespace std;
int main()
{
int i = 0;
begin:
i = i + 1;
cout <
<
i <
<
" " ;
if (i <
100)
{
goto begin;
}
return 0;
}//This code is contributed by ShubhamCoder
C
#include <
stdio.h>
int main()
{
int i = 0;
begin:
i = i + 1;
printf ( "%d " , i);
if (i <
100)
goto begin;
return 0;
}
C#
using System;
class GFG{static public void Main ()
{
int i = 0;
begin:
i = i + 1;
Console.Write( " " + i + " " );
if (i <
100)
{
goto begin;
}
}
}//This code is contributed by ShubhamCoder
输出如下:
1 2 3 4 . . . 97 98 99 100
使用递归主函数:
C ++
#include <
iostream>
using namespace std;
int main()
{
static int i = 1;
if (i <
= 100)
{
cout <
<
i++ <
<
" " ;
main();
}
return 0;
}//This code is contributed by ShubhamCoder
C
#include <
stdio.h>
int main()
{
static int i = 1;
if (i <
= 100) {
printf ( "%d " , i++);
main();
}
return 0;
}
Java
//Java program to count all pairs from both the
//linked lists whose product is equal to
//a given value
class GFG
{
static int i = 1 ;
public static void main(String[] args)
{if (i <
= 100 )
{
System.out.printf( "%d " , i++);
main( null );
}
}
}//This code is contributed by Rajput-Ji
C#
//C# program to count all pairs from both the
//linked lists whose product is equal to
//a given value
using System;
class GFG
{
static int i = 1;
public static void Main(String[] args)
{
if (i <
= 100)
{
Console.Write( "{0} " , i++);
Main( null );
}
}
}//This code is contributed by Rajput-Ji
【如何在不使用循环的情况下打印1到100之间的数字(|S2)】输出如下:
1 2 3 4 . . . 97 98 99 100
推荐阅读
- ML中的DBSCAN集群|基于密度的聚类
- 算法题(如何用一个数字替换另一个数字())
- Floyd Warshall算法原理和实现|DP-16
- 用Python打印列表(4种不同方式)
- 8086微处理器中的寻址模式
- 让老系统换新装 WindowsXP用上Windows7的任务栏
- XP提高网速的隐秘武器:只需要一个协议
- 健康运用U盘的一些建议
- 对于Windows下JSP开发环境的设置方案