Jammy is the trainer in Secondary school, he has given the task of printing the letter pattern.
Students has to create a logic which get the integer number which specify the number of rows and according to that students has to print the letter pattern
Programming language need to be used:C++
#include <iostream>
using namespace std;
int main() {
int i,j,n;
char c;
cin>>n;
c='A';
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
cout<<c;
c++;
if(c=='[')
c='A';
}
cout<<endl;
}
return 0;
}
Students has to create a logic which get the integer number which specify the number of rows and according to that students has to print the letter pattern
Programming language need to be used:C++
TEST CASE 1
INPUT
INPUT
7
OUTPUTA
BC
DEF
GHIJ
KLMNO
PQRSTU
VWXYZAB
TEST CASE 2
INPUT
INPUT
6
OUTPUTA
BC
DEF
GHIJ
KLMNO
PQRSTU
#include <iostream>
using namespace std;
int main() {
int i,j,n;
char c;
cin>>n;
c='A';
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
cout<<c;
c++;
if(c=='[')
c='A';
}
cout<<endl;
}
return 0;
}
Creating a Letter Pattern for Secondary School Students in C++
In this problem, students need to create a pattern of letters based on the number of rows given as input. The pattern should start with the letter 'A' and continue sequentially, wrapping around back to 'A' after 'Z' if needed.
Problem Description
You need to write a C++ program that:
- Takes an integer input
n
, representing the number of rows. - Prints a pattern of letters where each row starts with the next letter in the alphabet from where the previous row ended. If the alphabet reaches 'Z', it should wrap around to 'A'.
Breaking Down the Solution
To solve this problem, the logic involves:
- Initializing a character variable
c
with 'A'. - Iterating through rows, where each row prints a sequential number of characters starting from
c
. - Wrapping around to 'A' when 'Z' is exceeded.
How the Code Works
- Input Handling: The program reads an integer
n
that represents the number of rows to be printed. - Character Initialization: The character
c
is initialized to 'A'. - Outer Loop (Rows): The outer loop runs from 0 to
n-1
, controlling the number of rows. - Inner Loop (Columns): The inner loop runs from 0 to
i
(the current row number), controlling how many characters are printed on each row. - Character Printing and Wrapping: After each character is printed, the character is incremented. If the character exceeds 'Z', it is reset to 'A' using the condition
if(c == '[')
. - New Line: After each row, a newline character is printed.