• The School
    • Introduction
    • Admissions
    • Academic Calendar 2022
  • Students
    • Projects 1st Term 2022
    • 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

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

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

Admissions

Recent Posts

  • Cuet Exam 2022 for admission to Central Universities and other participating universities in 2022
  • Mathematics Study Material (Class 12) : Relations and Functions
  • Bridge Classes for Class 10 Matric Appeared Students
  • FELICITATION PROGRAM OF LSHSS on 26th July 2021
  • As part of the Life Skill Class, Physical Education was conducted of Class-12 Arts and Science. The main objective was to inculcate physical fitness and healthy lifestyle especially during the pandemic. The class started with a brief theory session by the class teachers followed by live demonstration and workouts under the guidance of Miss Neishedeno.

Little Star Higher Secondary School

Love Truth Service

Midland, Dimapur
Nagaland

Copyright © 2022 · Education Pro Theme On Genesis Framework · WordPress · Log in