Write an interactive program to print a diamond shape

Write an interactive program to print a diamond shape

Hello friends i hope you like my previous post, in today’s post we are going discuss about, “Write an interactive program to print a diamond shape”

(note : programs based on gtu syllabus)

So you have to  Write an interactive program to print a diamond shape 

so now let’s discuss the logic, how you can implement it

Implementation

First define the variables in main class

  •  Integer  
    • i = 0
    • j = 0
    • k = 0
    • no

So you have to write program that prints set of ” * ” Symbol in diamond pattern, suppose user enters number 3 as input than diamond pattern is made of total 5 rows 

Ex:

pattern should look similar as shown above, you have to use 6 “FOR LOOPS” i explain the intention of using 6 LOOPS So let’s start

so first you have to take “integer” input value from user for diamond pattern and store it to “no” variable (Note: input value represents the center of diamond pattern) and use Scanner class for taking input from user

for printing diamond pattern we have to divide diamond in two parts 1. Upper Triangle &               2. Reverse Triangle

first you have to print Upper Triangle

Example:

and then Reverse Triangle

Example:

 

Upper Triangle:

FIRST FOR LOOP:

for upper triangle take one “FOR LOOP” which run until it’s index variable ” i ” value less than user’s entered integer value, this loop helps to perform all the operation for printing Upper Triangle

value of index variable ” i ” incremented by ” 1 ” after completion of one iteration

SECOND FOR LOOP (inside first for loop):

Second “FOR LOOP” is used to print blank spaces so ” * ” Symbol can be printed in Triangle pattern

index variable ” j ” initialized with “(user’s input / 2) + 1” AND it runs until it’s index variable ” j ”  greater or equal to value of variable ” i

value of index variable ” j ” decremented by ” 1 ” after completion of one iteration

THIRD FOR LOOP (inside first for loop after second for loop)

Third “FOR LOOP” is used to print ” * ” symbol in triangle pattern (this is the loop which prints pattern)

index variable ” k ” initialized with ” 0 ” AND it runs until it’s index variable ” ” less or equal to value of variable ” i “

value of index variable ” ” incremented by ” 1 ” after completion of one iteration

Example code for Upper Triangle:

Reverse Triangle:

for reverse triangle you have to print bottom part of triangle first

FOURTH FOR LOOP (outside First for loop):

fourth “FOR LOOP” which run until it’s index variable ” i ” ( i initialized with 0) value less than user’s entered integer value, this loop helps to perform all the operation for printing reverse Triangle

value of index variable ” i ” incremented by ” 1 ” after completion of one iteration

FIFTH FOR LOOP (inside Fourth for loop):

Fifth “FOR LOOP” is used to print blank spaces so ” * ” Symbol can be printed in Reverse Triangle pattern

index variable ” j ” initialized with ” 0 ” AND it runs until it’s index variable ” j ”  less or equal to value of variable ” i

value of index variable ” i ” incremented by ” 1 ” after completion of one iteration

SIXTH FOR LOOP (inside Fourth for loop and after Fifth for loop)

Sixth “FOR LOOP” is used to print ” * ” symbol in triangle pattern (this is the loop which prints pattern)

index variable ” k ” initialized with ” user’s input – 1 ” AND it runs until it’s index variable ” ” greater or equal to value of variable ” i

value of index variable ” ” decremented by ” 1 ” after completion of one iteration

Example code for Reverse Triangle:

so here’s our programming logic discussion is completed for “Write an interactive program to print a diamond shape”

Check Out other post – Write an interactive program to print a string entered in a pyramid form For instance the string stream

so here your program’s solution, check program below the post for reference

these are the java basic programs based on gtu syllabus

Program :

OUTPUT :

Write an interactive program to print a diamond shape

Program As per GTU Syllabus

if you like the the way of explanation tell us in comment section you can also write the queries about this program we are defiantly going reply

share this post with your college friends so everyone can get benefit of it good bye friends see you in next post happy coding

You May Also Like

About the Author: TheCoder

Leave a Reply

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