使用一个循环| Set 2(使用Continue语句)打印图案

给定一个数字n,打印三角形图案。我们只允许使用一个循环。 例子:

null
Input: 7Output:** * * * ** * * ** * * * ** * * * * ** * * * * * *

我们使用单for循环,在循环中,我们为行计数和当前星计数维护两个变量。如果当前星号计数小于当前行计数,则打印星号并继续。否则我们打印一个新行并增加行数。

C

// C++ program to print a pattern using single
// loop and continue statement
#include<bits/stdc++.h>
using namespace std;
// printPattern function to print pattern
void printPattern( int n)
{
// Variable initialization
int line_no = 1; // Line count
// Loop to print desired pattern
int curr_star = 0;
for ( int line_no = 1; line_no <= n; )
{
// If current star count is less than
// current line number
if (curr_star < line_no)
{
cout << "* " ;
curr_star++;
continue ;
}
// Else time to print a new line
if (curr_star == line_no)
{
cout << "" ;
line_no++;
curr_star = 0;
}
}
}
// Driver code
int main()
{
printPattern(7);
return 0;
}


JAVA

// Java program to print a pattern using single
// loop and continue statement
import java.io.*;
class GFG {
// printPattern function to print pattern
static void printPattern( int n)
{
// Variable initialization
// Line count
int line_no = 1 ;
// Loop to print desired pattern
int curr_star = 0 ;
for ( line_no = 1 ; line_no <= n;)
{
// If current star count is less than
// current line number
if (curr_star < line_no)
{
System.out.print ( "* " );
curr_star++;
continue ;
}
// Else time to print a new line
if (curr_star == line_no)
{
System.out.println ( "" );
line_no++;
curr_star = 0 ;
}
}
}
// Driver code
public static void main (String[] args)
{
printPattern( 7 );
}
}
// This code is contributed by vt_m


Python 3

# Python 3 program to print
# a pattern using single
# loop and continue statement
# printPattern function
# to print pattern
def printPattern(n):
# Variable initialization
line_no = 1 # Line count
# Loop to print
# desired pattern
curr_star = 0
line_no = 1
while (line_no < = n ):
# If current star count
# is less than current
# line number
if (curr_star < line_no):
print ( "* " , end = "")
curr_star + = 1
continue
# Else time to print
# a new line
if (curr_star = = line_no):
print ("")
line_no + = 1
curr_star = 0
# Driver code
printPattern( 7 )
# This code is contributed
# by Smitha


C#

// C# program to print a pattern using single
// loop and continue statement
using System;
class GFG {
// printPattern function to print pattern
static void printPattern( int n)
{
// Variable initialization
// Line count
int line_no = 1;
// Loop to print desired pattern
int curr_star = 0;
for ( line_no = 1; line_no <= n;)
{
// If current star count is less than
// current line number
if (curr_star < line_no)
{
Console.Write ( "* " );
curr_star++;
continue ;
}
// Else time to print a new line
if (curr_star == line_no)
{
Console.WriteLine ();
line_no++;
curr_star = 0;
}
}
}
// Driver code
public static void Main ()
{
printPattern(7);
}
}
// This code is contributed by vt_m


PHP

<?php
// php program to print a
// pattern using single loop
// and continue statement
// printPattern function
// to print pattern
function printPattern( $n )
{
// Variable initialization
$line_no = 1; // Line count
// Loop to print desired pattern
$curr_star = 0;
for ( $line_no = 1; $line_no <= $n :winking_face:
{
// If current star count is less
// than current line number
if ( $curr_star < $line_no )
{
echo "* " ;
$curr_star ++;
continue ;
}
// Else time to print
// a new line
if ( $curr_star == $line_no )
{
echo "" ;
$line_no ++;
$curr_star = 0;
}
}
}
// Driver code
$n =7;
printPattern( $n );
// This code is contributed by mits
?>


Javascript

<script>
// JavaScript program to print a pattern using single
// loop and continue statement
// printPattern function to print pattern
function printPattern(n)
{
// Variable initialization
var line_no = 1; // Line count
// Loop to print desired pattern
var curr_star = 0;
for ( var line_no = 1; line_no <= n; )
{
// If current star count is less than
// current line number
if (curr_star < line_no)
{
document.write( "* " );
curr_star++;
continue ;
}
// Else time to print a new line
if (curr_star == line_no)
{
document.write( "<br>" );
line_no++;
curr_star = 0;
}
}
}
// Driver code
printPattern(7);
// This code is contributed by rdtank.
</script>


输出:

** ** * ** * * ** * * * ** * * * * ** * * * * * *

时间复杂性: O(n)

辅助空间: O(1) 请参阅下面的帖子,了解更多方法。 仅使用一个循环打印图案 本文由 阿希什·瓦什尼 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享