Write
a
program in c++ to display
the
following
pattern
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Answers
C++ program
//Program to display the pattern
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"1"<<endl;
cout<<"1 2"<<endl;
cout<<"1 2 3"<<endl;
cout<<"1 2 3 4"<<endl;
cout<<"1 2 3 4 5"<<endl;
}
hope this helps you.
Pattern program in c++
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Explanation:
The program to the given question as follows:
Program:
#include <iostream> //defining header file.
using namespace std;
int main() //defining main method
{
int i, j,num; //defining integer variable
for(i=0; i<5; i++) // outer loop for column
{
num=1;
for(j=0; j<=i; j++) // inner loop for rows
{
cout<<num; //print value
num++; //increment value by 1
}
cout<<endl; //use endl for new line
}
return 0;
}
Explanation of the above program as follows:
- In the above C++ language code firstly the header file is included, then defining the main method, inside the main method three integer variable "i,j, and num" is defined.
- In the next step, two for loop is declared, The first loop uses the variable "i", that starts from 0 and ends when, it is less than 5, these loop print columns values.
- Inside a loop, another for loop is defined, which uses variable j, which starts from 0 and ends when the value of j is less than equal to "i", this loop prints rows values.
- End of the inner loop the "endl" is used, which prints value in new line.
Learn more:
- What are patterns: https://brainly.in/question/11676412
- Pattern program: https://brainly.in/question/682203