Last active
November 26, 2022 02:55
-
-
Save sharadcodes/0eee8cf09d8a099c0715af23a934ea2a to your computer and use it in GitHub Desktop.
Random question of pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Enter num: 1 | |
| * | |
| Enter num: 3 | |
| * | |
| * * * | |
| * | |
| * | |
| * | |
| * * * | |
| * | |
| Enter num: 5 | |
| * | |
| * * * | |
| * * * * * | |
| * | |
| * | |
| * | |
| * | |
| * | |
| * * * * * | |
| * * * | |
| * | |
| */ | |
| #include <iostream> | |
| using namespace std; | |
| void internalLoop(int num, int i); | |
| int main() | |
| { | |
| int num, n; | |
| cout <<"Enter num: " << endl; | |
| cin >> num; | |
| // saving n since we will modify it later but original and modified values are used in priniting section 2 | |
| n = num; | |
| if(num > 3) { | |
| num -= 2; | |
| } else if (num > 1) { | |
| num -= 1; | |
| } | |
| // section 1 | |
| // for top pyramid | |
| for(int i = 1; i <= num && num != 1; i++) { | |
| internalLoop(num, i); | |
| } | |
| // section 2 | |
| // for line of * | |
| for(int i= 1; i<= n; i++) { | |
| for(int j=1; j <= (num*2)-2; j++) { | |
| // this loop adds spaces from left | |
| cout << " "; | |
| } | |
| cout << "*" <<endl; | |
| } | |
| // section 3 | |
| // for bottom pyramid | |
| for (int i = num; i >= 1 && num != 1; --i) { | |
| internalLoop(num, i); | |
| } | |
| return 0; | |
| } | |
| void internalLoop(int num, int i) { | |
| for (int sp = 0; sp < num - i; sp++) { | |
| cout << " "; | |
| } | |
| for (int j = i; j <= 2 * i - 1; j++) { | |
| cout << "* "; | |
| } | |
| for (int j = 0; j < i - 1; j++) { | |
| cout << "* "; | |
| } | |
| cout << endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
π