[英]how to print a right angle triangle in c++ using single loop
this is the code that I wrote for printing the pattern using single however it doesn't work can you help me out.这是我为使用 single 打印模式而编写的代码,但是它不起作用你能帮我一下吗?
#include<iostream>
using namespace std;
int main()
{
int line, star = 0, n;
cin >> n;
for (line = 1; line <= n; line++)
{
if (star < n)
{
cout << "*";
star++;
continue;
}
if (star == line)
{
cout << endl;
star
}
}
system("pause");
return 0;
}
To print the right angle traingle we can use the string and then add some of the part to it to increase the length of string which look similer to triangle.要打印直角三角形,我们可以使用字符串,然后向其中添加一些部分以增加看起来类似于三角形的字符串的长度。
void solve()
{
string a = "*";
string add_to_a= "*";
int n;
cin>>n;
for (int i = 0; i < n; ++i)
{
cout<<a<<"\n";
a+=add_to_a;
}
}
this is code written by me this may help这是我写的代码,这可能会有所帮助
//Try this Code to print right angled triangle in c++ //试试这个代码在C++中打印直角三角形
#include<bits/stdc++.h>
using namespace std;
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 << "\n";
line_no++;
curr_star = 0;
}
}
}
// Driver code
int main()
{
printPattern(7);
return 0;
}
//Output of the code //代码输出
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.