How to print a star pattern without using loop in the c++ programming language
How to print a star pattern without using loop in the c++ programming language. In this program, you will learn how to print the star pattern without using the loop.
Take an example to print this star pattern through a c++ program:
// How to print a star pattern without
// using loop in the c++ programming language
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
int i = 1, r = 1;
// It's the recursive function
// to print the star pattern
void starPattern(int n)
{
((int) sqrt(pow((i - (2 * n - 1) *(r - 1) - n), 2)) < r) ? cout << "*" : cout << " ";
if ((i - (2 * n - 1) * (r - 1)) % (2 * n - 1) == 0)
{
cout << "\n";
r++;
}
if (i++ < n * (2 * n - 1))
starPattern(n);
}
int main()
{
// size of the pattern
int x;
cout << "-----Enter the size of the pattern-----\n";
cin >> x;
// This will print the pattern
starPattern(x);
return 0;
}
Output:
-----Enter the size of the pattern-----
12
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
Tags:
# print star pattern without using loop in c++
# write a program to print done without using any loop in c++
# print all numbers between 1 to n without using any loop in c++