[Solved] Hari is a civil engineer who is designing a fountain in square shape with water sprinklers in the edges with n nuluber of steps

Hari is a civil engineer who is designing a fountain in square shape with water sprinklers in the edges with n nuluber of steps. He needs to draw a sketch of the fountain in top view with the step number at the edges of the square.

Write a program to help him in printing the pattern with n number of steps Input format Input is an positive integer describing the step levels of the fountain.

The program must accept a string S and an integer N as the input. The program must print the desired pattern as shown below:

Input:

abcdefghijk
3

Output:

a
*b
**c
*d
e
*f
**g
*h
i
*j
**k

Explanation:

Here N is 3. The highest possible height of the string pattern must be 3. Start from the height as 1. Increment till the value n(=3) is reached. Once the height is reached start decrementing. Repeat the process until all the characters in the string are printed.

Solution

step-by-step explanation:

  1. Initialize a boolean variable named “flag” to false and an integer variable named “x” to 0.
  2. Iterate through each character of the input string using a for loop with a loop variable “i”.
  3. Check if the value of “x” is 0. If it is, set “flag” to true.
  4. Check if the value of “x” is equal to n-1. If it is, set “flag” to false.
  5. Iterate through each integer from 0 to “x” using a nested for loop with a loop variable “j”. Print out a star for each integer in this range.
  6. Print out the character at the current index of the input string.
  7. Check if the value of “flag” is true. If it is, increment the value of “x” by 1. Otherwise, decrement the value of “x” by 1.
  8. Repeat steps 3 to 7 until all characters in the input string have been processed.
  9. The output of the “steps” method will be a pattern of stars and characters printed to the console. The pattern will start with 1 star and the first character of the input string and will continue to increase the number of stars until “x” is equal to n-1, at which point it will start decreasing the number of stars until it reaches 1 star and the last character of the input string.
import java.util.*;

public class Main
{
    public static void steps(String string, int n)
    {
        boolean flag = false;
        int x = 0;

        for (int i = 0; i < string.length(); i++)
        {
            if (x == 0)
                flag = true;
            if (x == n - 1)
                flag = false;

            for (int j = 0; j < x; j++)
                System.out.print("*");
            System.out.println(string.charAt(i));

            if (flag == true)
                x++;
            else
                x--;
        }
    }
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        String string = sc.nextLine();
        int n = sc.nextInt();
        steps(string, n);
    }
}
def steps(string, n):

    flag = False
    x = 0

    for i in range(len(string)):

        if (x == 0):
            flag = True
 
        if (x == n - 1):
            flag = False
 
        for j in range(x):
            print("*", end = "")
 
        print(string[i])
 
        if (flag == True):
            x += 1
        else:
            x -= 1

string = input()
n = int(input())
steps(string, n)
// c++
#include <iostream>
#include <string>
using namespace std;

void steps(string string, int n)
{
    bool flag = false;
    int x = 0;

    for (int i = 0; i < string.length(); i++)
    {
        if (x == 0)
            flag = true;
        if (x == n - 1)
            flag = false;

        for (int j = 0; j < x; j++)

            // sauravhathi
            cout << "*";
        cout << string[i] << endl;

        if (flag == true)
            x++;
        else
            x--;
    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

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