1 
   2 2 
  3 3 3 
 4 4 4 4 
5 5 5 5 5 
 4 4 4 4 
  3 3 3 
   2 2 
    1 
Problem Statement:- Program to print the right triangle star pattern.
Sample Input/Output:-
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 Number Pattern.
Code:
#include<stdio.h>
#include<math.h>
int main()
{
    int out,in,p,x=1;
    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("%d ",x);
	    	 }
	    	  if(out>0)
	    		 x++;
	    	 else
	    		 x--;
	    	 printf("\n");
}}
Enter the row size:4
    1
   2 2
  3 3 3
 4 4 4 4
5 5 5 5 5
 4 4 4 4
  3 3 3
   2 2
    1
Program in C++
Here is the source code of the C++  Program to print the Solid Diamond Number Pattern.
Code:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int out,in,p,x=1;
    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<<x<<" ";
	    	 }
	    	 if(out>0)
	    		 x++;
	    	 else
	    		 x--;
	    	 cout<<"\n";
}}
Enter the row size:5
     1
    2 2
   3 3 3
  4 4 4 4
 5 5 5 5 5
6 6 6 6 6 6
 5 5 5 5 5
  4 4 4 4
   3 3 3
    2 2
     1
Program in Java
Here is the source code of the Java  Program to print the Solid Diamond Number Pattern.
Code:
import java.util.Scanner;
public class NumberPattern14 {
	public static void main(String[] args) {
		Scanner cs=new Scanner(System.in);
        int out,in,p,x=1;
	    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(x+" ");
	    	 }
	    	 if(out>0)
	    		 x++;
	    	 else
	    		 x--;
	    	 System.out.println();
	    }
	    cs.close();
	}
}
Enter the row size:
3
   1 
  2 2 
 3 3 3 
4 4 4 4 
 3 3 3 
  2 2 
   1 
Program in Python
Here is the source code of the Python  Program to print the Solid Diamond Number Pattern.
Code:
row_size=int(input("Enter the row size:"))
x=1
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(x,end=" ")
    if out > 0:
        x +=1
    else:
        x -=1
    print("\r")
Enter the row size:2
  1 
 2 2 
3 3 3 
 2 2 
  1 
 

1 Comments
Thanks a lot for this article, these programs helped me in my assignment.
ReplyDeletePlease do not Enter any spam link in the comment box