Print a Pattern Series

C++C++Beginner
Practice Now

Introduction

In this lab, we will be creating a C++ program to print a given pattern or series like 12345, 5432, 234, 43, 3. This is a common exercise for beginners to practice in programming as it requires the use of loops and basic arithmetic operations.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/ControlFlowGroup -.-> cpp/while_loop("`While Loop`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") subgraph Lab Skills cpp/variables -.-> lab-96202{{"`Print a Pattern Series`"}} cpp/data_types -.-> lab-96202{{"`Print a Pattern Series`"}} cpp/operators -.-> lab-96202{{"`Print a Pattern Series`"}} cpp/while_loop -.-> lab-96202{{"`Print a Pattern Series`"}} cpp/for_loop -.-> lab-96202{{"`Print a Pattern Series`"}} end

Create a new C++ file

Create a new C++ file in the terminal with the following command:

touch ~/project/main.cpp

Open the file with your preferred text editor, and let's get started.

Add the necessary header files

We will be needing the iostream, stdio.h, and math.h libraries for the program. Add the following code to include them:

#include<iostream>
#include<stdio.h>
#include<math.h>

Define the variables

We need to define integer variables i, j, p, n, and reverse.

int i,j,p=1,n,reverse;

Take input from the user

Prompt the user to enter the number of terms and store it in the variable n with the following code:

cout<<"Enter The Number To Print A Pattern\n";
cin>>n;

Find the number of digits

We need to find the number of digits in the input value to determine the number of iterations for the loop. We can use the log10 function from the math.h library to find this value.

int dig=log10(n);

Print the full input

Print the full input value in the first line of the output with the following code:

cout<<"\n"<<n<<endl;

Reverse and print the series

We use a for loop to reverse the number and print the series. We use a while loop to change the order of digits in the input value to be reversed in each iteration:

for(i=1;i<=dig;i++)
{
    while (n != 0)
    {
        reverse = reverse * 10;
        reverse = reverse + n%10;
        n       = n/10;
    }
    n=reverse/10;
    cout<<n<<endl;
    reverse=0;
}

Compile and run the program

Save the file and compile it in the terminal with the following command:

g++ ~/project/main.cpp -o main && ./main

Verify the program output

The output for the entered value of 12345 should be:

12345
5432
234
43
3

Summary

In this lab, we have created a C++ program to print a pattern series like 12345, 5432, 234, 43, 3. We have used the while loop and log10 function to find the number of digits in the input value, and a for loop to reverse and print the series in the specified format.

Other C++ Tutorials you may like