C++ Online Compiler
Example: Print Star Pattern without using loop in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Print Star Pattern without using loop in C++ #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); } // It's the driver function 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; }
12
Output
Clear
ADVERTISEMENTS