Program to print the Solid Diamond Alphabet Pattern

     A B B C C C D D D D E E E E E F F F F F F E E E E E D D D D C C C B B A



Problem statement:-   Program to print the Solid Diamond Alphabet Pattern.

 Data requirement:-

   Input Data:- row_size

  Output Data:- x

  Additional Data:-in or inn(for python), out, p

Program in C

Here is the source code of the C  Program to print the Solid Diamond Alphabet Pattern.

Code:

#include<stdio.h>
#include<math.h>
int main()
{
    int out,in,p,x=0;
    printf("Enter the row size:");
    int row_size;
    scanf("%d",&row_size);
    for(out=row_size;out>=-row_size;out--)
    {
     for(in=1;in<=abs(out);in++)
     {
    printf(" ");
     }
    for(p=row_size;p>=abs(out);p--)
    {
    printf("%c ",x+65);
    }
      if(out>0)
    x++;
    else
    x--;
    printf("\n");
}}

Input/Output:
Enter the row size:5
      
    B B     
   C C C    
  D D D D   
 E E E E E  
F F F F F F 
 E E E E E  
  D D D D   
   C C C    
    B B 
     A 

Program in C++

Here is the source code of the C++  Program to print the Solid Diamond Alphabet Pattern.

Code:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int out,in,p,x=0;
    cout<<"Enter the row size:";
    int row_size;
    cin>>row_size;
    for(out=row_size;out>=-row_size;out--)
    {
     for(in=1;in<=abs(out);in++)
     {
    cout<<" ";
     }
    for(p=row_size;p>=abs(out);p--)
    {
    cout<<char(x+65)<<" ";
    }
    if(out>0)
    x++;
    else
    x--;
    cout<<"\n";
}}

Input/Output:
Enter the row size:4
      A
    B B
  C C C
 D D D D
E E E E E
 D D D D
  C C C
   B B
    A

Program in Java

Here is the source code of the Java  Program to print the Solid Diamond Alphabet Pattern.

Code:

import java.util.Scanner;
public class AlphabetPattern19 {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
        int out,in,p,x=0;
    System.out.println("Enter the row size:");
    int row_size=cs.nextInt();
    for(out=row_size;out>=-row_size;out--)
    {
     for(in=1;in<=Math.abs(out);in++)
     {
    System.out.printf(" ");
     }
    for(p=row_size;p>=Math.abs(out);p--)
    {
    System.out.print((char)(x+65)+" ");
    }
    if(out>0)
    x++;
    else
    x--;
    System.out.println();
    }
    cs.close();
}}

Input/Output:
Enter the row size:
4
     A 
    B B 
  C C C 
 D D D D 
E E E E E 
 D D D D 
  C C C 
    B B 
      A 

Program in Python

Here is the source code of the Python  Program to print the Solid Diamond Alphabet Pattern.

Code:

row_size=int(input("Enter the row size:"))
x=0
for out in range(row_size,-(row_size+1),-1):
    for inn in range(1,abs(out)+1):
        print(" ",end="")
    for p in range(row_size,abs(out)-1,-1):
        print((chr)(x+65),end=" ")
    if out > 0:
        x +=1
    else:
        x -=1
    print("\r")

Input/Output:
Enter the row size:3
     A 
   B B 
 C C C 
D D D D 
 C C C 
   B B 
     A 

Post a Comment

2 Comments

  1. How to write Python code for hollow pyramid pattern like
    A
    B B
    C C
    D D
    E E
    D D
    C C
    B B
    A

    ReplyDelete
  2. size=9
    inn=size
    k=0
    for out in range(1, size+1):
    for a in range(1,n+1):
    if(a==inn or a==size-inn+1):
    print(chr(k+65),end="")
    else:
    print(" ",end="")
    if(out<=size/2):
    inn-=1
    k+=1
    else:
    inn+=1
    k-=1
    print()

    ReplyDelete

Please do not Enter any spam link in the comment box