• The School
    • Introduction
    • Admissions
    • Academic Calendar 2020
  • Students
    • Projects 2nd QAT 2019
    • Routines – Quarterly Assessment Test and Unit Test
  • Photo Gallery
  • School Events
  • Hostel
  • Higher Secondary (Arts and Science)
  • Resources
    • Computer Science Class 11
    • Class 1
    • Class 2
    • Class 3
    • Class 4
    • Class 5
    • Class 6
    • Class 7
    • Class 8
    • Class 9
    • Class 10
    • Class 11
    • Class 12
  • Class 11 Registration Form 2020

Little Star Hr. Sec. School

Love Truth Service

Pattern Printing C++

For input n=5
ABCDEDCBA
ABCD DCBA
ABC   CBA
AB     BA
A       A 

Using an array to print this pattern eliminates the need to use if statements and nested for loops. We create an array with all the alphabets from the first row and keep two markers – left and right which converts alphabets into blank spaces each row

#include<iostream>
using namespace std;
int main()
{
   int n,i;
   cout<<"Enter number of lines : ";
   cin>>n;
   int size = 2*n; //Array size;
   char a[size],ch='A';
   for(i=0;i<n;i++)  //Fill up array with the alphabets of first row
      a[i] = a[size-2-i] = ch++;
   a[size-1] = '\0';
   int left=n-1,right=n-1; // These track the alphabets to be converted into spaces each row
   for(i=1;i<=n;i++)
   {
   	   cout<<a<<endl;
   	   a[left--] = a[right++] = ' ';
   }
}

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Admissions

Recent Posts

  • FELICITATION PROGRAMME CONDUCTED FOR HSLCE TOPPERS-2020
  • Congratulations to Sir Bikash and Sir Vijay
  • Online One on One Oral Exams Going On.
  • Mobile Legends Armor Calculation
  • Little Star Higher Secondary develops website for viewing HSLC/HSSLC toppers of NBSE

Little Star Higher Secondary School

Love Truth Service

Midland, Dimapur
Nagaland

Copyright © · Little Star Higher Secondary School Dimapur