Computer Science, asked by RagaviRagavendra, 1 year ago

Program to print

*
**
***
****

C++..class 11​


RagaviRagavendra: hi

Answers

Answered by Avengers00
17
\underline{\underline{\huge{\textbf{Pattern\: Printing\: program\: in\: C++}}}}

/* C++ program to print the given pattern */

include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter number of rows: ";
cin >> n;
for(int r = 1; r <= n; r++)
{
for(int c = 1; c <= r; c++)
{
cout << "*";
}
cout << "\n";
}
return 0;
}

\underline{\LARGE{\textsf{Explanation :}}}

\begin{tabular}{|c||c|c|c|c|}\cline{1-2}Row-1&amp;*\\\cline{1-3}Row-2&amp;*&amp;*\\\cline{1-4}Row-3&amp;*&amp;*&amp;*\\\cline{1-5}Row-4&amp;*&amp;*&amp;*&amp;*\\\cline{1-5}\end{tabular}

\sf\textsf{First the logic with which the pattern exists is to be noted.}

In each row, asterisks are to be printed and the number of asterisks in each row is equal to corresponding row number.

\sf\textsf{If there is a need of user interaction,}\\\sf\textsf{then the no. of rows is taken as $\rm in put$ from user.}

\sf\textsf{Here, the variable that stores this value is considered}\\\sf\textsf{as 'n'. (which is declared in the function main)}

\sf\textsf{In this case, n = 4, as there are 4 rows}

\sf\textsf{Summarizing the observed conditions}\\\sf\textsf{of the pattern:}

\sf\textsf{1. Asterisks are to be printed in each row}\\\sf\textsf{till the Last row (Which is given by}\\\sf\textsf{the Number of Rows (n))}

\sf\textsf{2. No. of asterisks in each row is equal to}\\\sf\textsf{the the Row Number.}

\sf\textsf{3. The printing line has to be changed to new line}\\\sf\textsf{when No. of Asterisks are printed as per [2].}

\sf\textsf{So, there is a need of 2 variables,}\\\sf\textsf{1. To track the Row}\\\sf\textsf{2. To track the column}

\sf\textsf{Here, r is row control and c is column control}

\sf\textsf{(which are declared inside respective for $\rm lo op$ )}

\sf\textsf{To implement Condition -1, a for $\rm lo op$ is used}\\\sf\textsf{with variable r initialised to 1 and incremented}\\\sf\textsf{after evaluation of Statements inside it with condition r$&lt;$=n}

\sf\textsf{To implement condition-2, another for $\rm lo op$ is used}\\\sf\textsf{with variable c initialised to 1 and incremented}\\\sf\textsf{after evaluation of Statements inside it with condition c$&lt;$=r }

\sf\textsf{Note that the condition -2 is implemented in according}\\\sf\textsf{ to condition-1, so the for $\rm lo ops$ will be nested.}

\sf\textsf{When the condition inside second for $\rm lo op $ (c$&lt;$=r)}\\\sf\textsf{evaluates to false, the printing line is changed}\\\mathsf{using\: cout\: &lt;&lt; " \backslash n " ;}

\underline{\textbf{Note:}}

\sf\textsf{In case, if space is needed after each asterisk,}\\\sf\textsf{line-12 can be changed to cout $&lt;&lt;$ "* ";}

\sf\textsf{If there is no need of user interaction in}\\ \sf \textsf{the program, then the no. of rows can directly}\\\sf\textsf{assigned to n at the time of declaration (in line-5)}\\\sf\textsf{and line- 6 \&amp; 7 can be excluded}
Attachments:
Answered by lovemyINDIA
0
Hey.........,,.., do u remember me.....

RagaviRagavendra: me??
RagaviRagavendra: no...im really sry
lovemyINDIA: Ok
RagaviRagavendra: say ...who u r
piyushRahulSingh: hi sis
lovemyINDIA: I’m Nandana
lovemyINDIA: From Kerala
Similar questions