Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Given the number of rows, the task is to print Exponentially Increasing Star Pattern in C, C++, and Python
Examples:
Example1:
Input:
given number of rows =5
Output:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Example2:
Input:
given number of rows =4 given character to print =@
Output:
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
- Python Program to Print Mirrored Right Triangle Star Pattern
- Python Program to Print Right Angled Triangle Star Pattern
- Python Program to Print Hollow Right Triangle Star Pattern
Program to Print Exponentially Increasing Star Pattern in C, C++, and Python
Below are the ways to print Exponentially Increasing Star Pattern in C, C++, and python.
Method #1: Using For loop (Star Character)
Approach:
- Give the number of rows of the pattern as static input and store it in a variable.
- Loop till a given number of rows using For loop.
- Calculate the exponential value of the first loop iterator value using 2**(iterator value of the first loop).
- Loop till the exponential value using another For loop(Nested For loop).
- Print the star character in the inner for loop.
- Print the newline character after the end of the inner for loop.
- The Exit of the Program.
1) Python Implementation
Below is the implementation:
# Give the number of rows of the pattern as static input and store it in a variable. rowsnumb = 5 # Loop till a given number of rows using For loop. for m in range(rowsnumb+1): # Calculate the exponential value of the first loop iterator # value using 2**(iterator value of the first loop). expvalue = 2**m # Loop till the exponential value using another For loop(Nested For loop). for n in range(expvalue): # Print the star character in the inner for loop. print("*", end=" ") # Print the newline character after the end of the inner for loop. print()
Output:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2) C++ Implementation
Below is the implementation:
#include <iostream> #include <math.h> using namespace std; int main() { // Give the number of rows of the pattern as static // input and store it in a variable. int rowsnumb = 5; // Loop till a given number of rows using For loop. for (int m = 0; m <= rowsnumb; m++) { // Calculate the exponential value of the first loop // iterator value using 2**(iterator value of the // first loop). int expvalue = pow(2, m); // Loop till the exponential value using another For // loop(Nested For loop). for (int n = 1; n <= expvalue; n++) { // Print the star character in the inner for // loop. cout << "* "; } // Print the newline character after the end of the // inner for loop. cout << endl; } return 0; }
Output:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3) C Implementation
Below is the implementation:
#include <math.h> #include <stdio.h> int main() { // Give the number of rows of the pattern as static // input and store it in a variable. int rowsnumb = 5; // Loop till a given number of rows using For loop. for (int m = 0; m <= rowsnumb; m++) { // Calculate the exponential value of the first loop // iterator value using 2**(iterator value of the // first loop). int expvalue = pow(2, m); // Loop till the exponential value using another For // loop(Nested For loop). for (int n = 1; n <= expvalue; n++) { // Print the star character in the inner for // loop. printf("* "); } // Print the newline character after the end of the // inner for loop. printf("\n"); } return 0; }
Output:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Method #2: Using For loop (User Character)
Approach:
- Give the number of rows of the pattern as user input and store it in a variable.
- Give the character to print as user input and store it in a variable.
- Loop till a given number of rows using For loop.
- Calculate the exponential value of the first loop iterator value using 2**(iterator value of the first loop).
- Loop till the exponential value using another For loop(Nested For loop).
- Print the user character in the inner for loop.
- Print the newline character after the end of the inner for loop.
- The Exit of the Program.
1) Python Implementation
- Give the number of rows using int(input()) and store it in a variable.
- Give the Character as user input using input() and store it in another variable.
Below is the implementation:
# Give the number of rows using int(input()) and store it in a variable. rowsnumb = int(input('Enter some random number of rows = ')) # Give the Character as user input using input() and store it in another variable. usercharact = input('Enter some random character to print = ') for m in range(rowsnumb+1): # Calculate the exponential value of the first loop iterator # value using 2**(iterator value of the first loop). expvalue = 2**m # Loop till the exponential value using another For loop(Nested For loop). for n in range(expvalue): # Print the user character in the inner for loop. print(usercharact, end=" ") # Print the newline character after the end of the inner for loop. print()
Output:
Enter some random number of rows = 4 Enter some random character to print = @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
2) C++ Implementation
- Give the number of rows using cin and store it in a variable.
- Give the Character as user input using cin and store it in another variable.
Below is the implementation:
#include <iostream> #include <math.h> using namespace std; int main() { // Give the number of rows of the pattern as user input // and store it in a variable. int rowsnumb; cin >> rowsnumb; // Give the character to print as user input and store // it in a variable. char usercharact; cin >> usercharact; cout<<endl; // Loop till a given number of rows using For loop. for (int m = 0; m <= rowsnumb; m++) { // Calculate the exponential value of the first loop // iterator value using 2**(iterator value of the // first loop). int expvalue = pow(2, m); // Loop till the exponential value using another For // loop(Nested For loop). for (int n = 1; n <= expvalue; n++) { // Print the user character in the inner for // loop. cout << usercharact << " "; } // Print the newline character after the end of the // inner for loop. cout << endl; } return 0; }
Output:
Enter some random number of rows = 4 Enter some random character to print = @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
3) C Implementation
- Give the number of rows using scanf and store it in a variable.
- Give the Character as user input using scanf and store it in another variable.
Below is the implementation:
#include <math.h> #include <stdio.h> int main() { // Give the number of rows of the pattern as user input // and store it in a variable. int rowsnumb; scanf("%d", &rowsnumb); // Give the character to print as user input and store // it in a variable. char usercharact; scanf("%c", &usercharact); // Loop till a given number of rows using For loop. for (int m = 0; m <= rowsnumb; m++) { // Calculate the exponential value of the first loop // iterator value using 2**(iterator value of the // first loop). int expvalue = pow(2, m); // Loop till the exponential value using another For // loop(Nested For loop). for (int n = 1; n <= expvalue; n++) { // Print the user character in the inner for // loop. printf("%c ", usercharact); } // Print the newline character after the end of the // inner for loop. printf("\n"); } return 0; }
Output:
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
Related Programs:
- python program to print an inverted star pattern
- python program to print rectangle star pattern
- python program to print hollow rectangle star pattern
- python program to print square star pattern
- python program to print hollow square star pattern
- python program to print rhombus star pattern
- python program to print hollow rhombus star pattern