Write an interactive program to print a string entered in a pyramid form For instance the string stream

Write an interactive program to print a string entered in a pyramid form For instance the string stream

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 string entered in a pyramid form For instance the string stream “

(note : programs based on gtu syllabus)

So you have to  Write an interactive program to print a string entered in a pyramid form. For instance, the string “stream” 

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
  •  String 
    • str = “stream”

So you have to make pyramid using String  “stream”

 Example : 

pyramid

 

 

for printing “stream” in pyramid pattern you have to follow this pattern in first row word’s length minus 1 blank spaces and 1st character of word follow this up-to last row in last row 0 spaces and all character needs to be print

ex: for 1st  row (word’s length – 1) Blank spaces & first character of word

for 2nd row (words’s length – 2) Blank spaces & first two characters of word

…….

up-to (Word’s length – Word’s length) Blank spaces & all characters of word

you can do this type computation using 3 for loops

  • 1st “FOR LOOP” used for performing all operation on all character’s of word
  • 2nd “FOR LOOP” used for printing blank spaces (Note: 2nd “FOR LOOP” is placed inside 1st  “FOR LOOP”)
  • 3rd  “FOR LOOP” used for printing characters in pyramid pattern (Note: 3rd “FOR LOOP” is also placed inside 1st “FOR LOOP” below 2nd “FOR LOOP” )

first initialize variable ” ” with 0 for 1stFOR LOOP” and initialize 2ndFOR LOOP” inside 1st “FOR LOOP” & for 2nd “FOR LOOP” initialize variable ” ” with “(word’s_length/2)+1” and runs the 2nd loop up-to value of ” j ” is grater than value of ” i

2nd for loop is used to print blank spaces, for printing blank spaces print blank character inside second for loop  [Ex: System.out.print(” “); ] 

then create one more “FOR LOOP” inside 1st “FOR LOOP” after 2nd “FOR LOOP” and initialize variable ” ” with 0 for 3rd “FOR LOOP” and run 3rd “FOR LOOP” until value of ” ” is greater than ” i ” for printing word “stream” in pyramid pattern

inside 3rd loop make use of charAt[] for printing word  [Ex. str.charAt(k)] 

after ending of 3rd for loop print one line break [“\n”] so in second iteration blank spaces & word prints in next line cause you have to print pyramid pattern

so here your program ” Write an interactive program to print a string entered in a pyramid form For instance the string stream” completed

Check Out other post – Create a class which ask the user to enter a sentence and it should display count of each vowel type in the sentence The program should continue till user enters a word quit

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 string entered in a pyramid form For instance the string stream

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 *