google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.: Palindrome triangle

Monday, January 9, 2017

Palindrome triangle


    Write a program in C to print print palindrome triangle pattern of n rows.


    How to print a right triangle of palindrome strings using for loops.

 

Palindrome triangle pattern of 5 rows:

A
ABA
ABCBA
ABCDCBA
ABCDEDCBA

Required Knowledge

    For Loop
    If Else Statement
    printf and scanf function in C


 Algorithm to print triangle pattern of palindrome strings
This program is similar to right triangle star pattern program.

    Take the number of rows(N) of right triangle as input from user using scanf function.
    Number of characters in Kth row is always 2K-1. 1st row contains 1 character, 2nd row contains 3 characters, 3rd row contains 5 characters and so on.
    In any row, we will first print the characters in increasing order of their ASCII value till mid of the row and then in decreasing order.
    We will use two for loops to print right triangle of palindrome strings.
        Outer for loop will iterate N time. Each iteration of outer loop will print one row of the pattern.
        Each iteration of inner loop will print a palindrome string as explained above.

C program to print palindrome triangle pattern

#include<stdio.h>

int main() {
   int i, j, rows, count=0;
  
   printf("Enter the number of rows\n");
   scanf("%d", &rows);
   
   for (i = 1; i <= 2*rows; i=i+2)
    {
      for (j = 1; j <= i; j++)
        {
               printf("%c", 'A'+count);
               if(j <= i/2)
                       count++;
                else 
                       count--;
              }
           count = 0;
          printf("\n");
       }
   return(0);
}

Output

Enter the number of rows
5
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA

शिव भोलेनाथ स्तुति

 जय शिवशंकर, जय गंगाधर, करुणा-कर करतार हरे,   जय कैलाशी, जय अविनाशी, सुखराशि, सुख-सार हरे जय शशि-शेखर, जय डमरू-धर जय-जय प्रेमागार हरे,   जय ...