Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Given the number of rows, the task is to Print pattern with a combination of Numbers and Stars in C, C++, and Python.
Examples:
Example1:
Input:
Given number of rows = 8
Output:
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 1 * 2 * 3 * 4 * 5 * 6 * 7 1 * 2 * 3 * 4 * 5 * 6 1 * 2 * 3 * 4 * 5 1 * 2 * 3 * 4 1 * 2 * 3 1 * 2 1
Example2:
Input:
Given number of rows = 13
Output:
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 * 13 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 1 * 2 * 3 * 4 * 5 * 6 * 7 1 * 2 * 3 * 4 * 5 * 6 1 * 2 * 3 * 4 * 5 1 * 2 * 3 * 4 1 * 2 * 3 1 * 2 1
- Python Program to Print Square Pattern with Numbers
- Python Program to Print Right Triangle Number Pattern
- Python Program to Print Number Reduction Pattern
Program to Print Pattern with a Combination of Numbers and Stars in C, C++, and Python
Below are the ways to print Print patterns with a combination of Numbers and Stars in C, C++, and Python.
Method #1: Using For Loop (Static Input)
Approach:
- Give the number of rows as static input and store it in a variable.
- Loop from 0 to the number of rows using For loop.
- Inside the For, loop Take a variable(say Tempo) and initialize its value with 1.
- Print the value of the Tempo variable with space.
- Loop from the number of rows -m-1 to 0 in decreasing order using another for loop(Nested For loop) where m is the iterator value of the parent For loop.
- Print the Star Character with space.
- Increment the value of Tempo by 1.
- Print the value of the Tempo variable with space.
- Print the Newline character after the end of the inner loop.
- The Exit of the Program.
1) Python Implementation
Below is the implementation:
# Give the number of rows as static input and store it in a variable. numberOfRows = 8 # Loop from 0 to the number of rows using For loop. for m in range(0, numberOfRows): # Inside the For, loop Take a variable(say Tempo) and initialize its value with 1. Tempo = 1 # Print the value of the Tempo variable with space. print(Tempo, end=' ') # Loop from the number of rows -m-1 to 0 in decreasing order using another for loop(Nested For loop) # where m is the iterator value of the parent For loop. for n in range(numberOfRows-m-1, 0, -1): # Print the Star Character with space. print('*', end=' ') # Increment the value of Tempo by 1. Tempo = Tempo + 1 # Print the value of the Tempo variable with space. print(Tempo, end=' ') # Print the Newline character after the end of the inner loop. print()
Output:
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 1 * 2 * 3 * 4 * 5 * 6 * 7 1 * 2 * 3 * 4 * 5 * 6 1 * 2 * 3 * 4 * 5 1 * 2 * 3 * 4 1 * 2 * 3 1 * 2 1
2) C++ Implementation
Below is the implementation:
#include <iostream> #include <math.h> using namespace std; int main() { // Give the number of rows as static input and store it // in a variable. int numberOfRows = 8; // Loop from 0 to the number of rows using For loop. for (int m = 0; m < numberOfRows; m++) { // Inside the For, loop Take a variable(say Tempo) // and initialize its value with 1. int Tempo = 1; // Print the value of the Tempo variable with space. cout << Tempo << " "; // Loop from the number of rows -m-1 to 0 in // decreasing order using another // for loop(Nested For loop) // where m is the iterator value of the parent For // loop. for (int n = numberOfRows - m - 1; n > 0; n--) { // Print the Star Character with space. cout << "* "; // Increment the value of Tempo by 1. Tempo = Tempo + 1; // Print the value of the Tempo variable with ///space. cout << Tempo << " "; } // Print the Newline character after the end of the // inner loop. cout << endl; } return 0; }
Output:
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 1 * 2 * 3 * 4 * 5 * 6 * 7 1 * 2 * 3 * 4 * 5 * 6 1 * 2 * 3 * 4 * 5 1 * 2 * 3 * 4 1 * 2 * 3 1 * 2 1
3) C Implementation
Below is the implementation:
#include <stdio.h> int main() { // Give the number of rows as static input and store it // in a variable. int numberOfRows = 13; // Loop from 0 to the number of rows using For loop. for (int m = 0; m < numberOfRows; m++) { // Inside the For, loop Take a variable(say Tempo) // and initialize its value with 1. int Tempo = 1; // Print the value of the Tempo variable with space. printf("%d ", Tempo); // Loop from the number of rows -m-1 to 0 in // decreasing order using another // for loop(Nested For loop) // where m is the iterator value of the parent For // loop. for (int n = numberOfRows - m - 1; n > 0; n--) { // Print the Star Character with space. printf("* "); // Increment the value of Tempo by 1. Tempo = Tempo + 1; // Print the value of the Tempo variable with /// space. printf("%d ", Tempo); } // Print the Newline character after the end of the // inner loop. printf("\n"); } return 0; }
Output:
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 * 13 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 1 * 2 * 3 * 4 * 5 * 6 * 7 1 * 2 * 3 * 4 * 5 * 6 1 * 2 * 3 * 4 * 5 1 * 2 * 3 * 4 1 * 2 * 3 1 * 2 1
Method #2: Using For Loop (User Input)
Approach:
- Give the number of rows as user input and store it in a variable.
- Loop from 0 to the number of rows using For loop.
- Inside the For, loop Take a variable(say Tempo) and initialize its value with 1.
- Print the value of the Tempo variable with space.
- Loop from the number of rows -m-1 to 0 in decreasing order using another for loop(Nested For loop) where m is the iterator value of the parent For loop.
- Print the Star Character with space.
- Increment the value of Tempo by 1.
- Print the value of the Tempo variable with space.
- Print the Newline character after the end of the inner loop.
- The Exit of the Program.
1) Python Implementation
Give the number of rows as user input using int(input()) and store it in a variable.
Below is the implementation:
# Give the number of rows as user input using int(input()) and store it in a variable. numberOfRows = int(input('Enter some random number of rows = ')) # Loop from 0 to the number of rows using For loop. for m in range(0, numberOfRows): # Inside the For, loop Take a variable(say Tempo) and initialize its value with 1. Tempo = 1 # Print the value of the Tempo variable with space. print(Tempo, end=' ') # Loop from the number of rows -m-1 to 0 in decreasing order using another for loop(Nested For loop) # where m is the iterator value of the parent For loop. for n in range(numberOfRows-m-1, 0, -1): # Print the Star Character with space. print('*', end=' ') # Increment the value of Tempo by 1. Tempo = Tempo + 1 # Print the value of the Tempo variable with space. print(Tempo, end=' ') # Print the Newline character after the end of the inner loop. print()
Output:
Enter some random number of rows = 11 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 1 * 2 * 3 * 4 * 5 * 6 * 7 1 * 2 * 3 * 4 * 5 * 6 1 * 2 * 3 * 4 * 5 1 * 2 * 3 * 4 1 * 2 * 3 1 * 2 1
2) C++ Implementation
Give the number of rows as user input using cin and store it in a variable.
Below is the implementation:
#include <iostream> using namespace std; int main() { // Give the number of rows as user input using // int(input()) and store it in a variable. int numberOfRows; cin >> numberOfRows; // Loop from 0 to the number of rows using For loop. for (int m = 0; m < numberOfRows; m++) { // Inside the For, loop Take a variable(say Tempo) // and initialize its value with 1. int Tempo = 1; // Print the value of the Tempo variable with space. cout << Tempo << " "; // Loop from the number of rows -m-1 to 0 in // decreasing order using another // for loop(Nested For loop) // where m is the iterator value of the parent For // loop. for (int n = numberOfRows - m - 1; n > 0; n--) { // Print the Star Character with space. cout << "* "; // Increment the value of Tempo by 1. Tempo = Tempo + 1; // Print the value of the Tempo variable with /// space. cout << Tempo << " "; } // Print the Newline character after the end of the // inner loop. cout << endl; } return 0; }
Output:
5 1 * 2 * 3 * 4 * 5 1 * 2 * 3 * 4 1 * 2 * 3 1 * 2 1
3) C Implementation
Give the number of rows as user input using scanf and store it in a variable.
Below is the implementation:
#include <stdio.h> int main() { // Give the number of rows as user input using scanf and // store it in a variable. int numberOfRows; scanf("%d", &numberOfRows); // Loop from 0 to the number of rows using For loop. for (int m = 0; m < numberOfRows; m++) { // Inside the For, loop Take a variable(say Tempo) // and initialize its value with 1. int Tempo = 1; // Print the value of the Tempo variable with space. printf("%d ", Tempo); // Loop from the number of rows -m-1 to 0 in // decreasing order using another // for loop(Nested For loop) // where m is the iterator value of the parent For // loop. for (int n = numberOfRows - m - 1; n > 0; n--) { // Print the Star Character with space. printf("* "); // Increment the value of Tempo by 1. Tempo = Tempo + 1; // Print the value of the Tempo variable with /// space. printf("%d ", Tempo); } // Print the Newline character after the end of the // inner loop. printf("\n"); } return 0; }
Output:
1 * 2 * 3 * 4 * 5 * 6 1 * 2 * 3 * 4 * 5 1 * 2 * 3 * 4 1 * 2 * 3 1 * 2 1
Related Programs:
- python program to print sum of negative numbers positive even numbers and positive odd numbers in a list
- python program to find those numbers which are divisible by 7 and multiple of 5 in a given range of numbers
- python program to create a list of tuples with the first element as the number and second element as the square of the number
- python program to find all numbers in a range which are perfect squares and sum of all digits in the number is less than 10
- python program to read print prime numbers in a range using sieve of eratosthenes
- python program to read a text file and print all the numbers present in the text file
- python program to accept a hyphen separated sequence of words as input and print the words in a hyphen separated sequence after sorting them alphabetically