How to solve problems dealing with stars and numbers in a Triangle?

Hi there, Dhruvam this side. Today we are going to cover the problems of the triangles. They’re always deep in trouble and so we are going…

How to solve problems dealing with stars and numbers in a Triangle?

Hi there, Dhruvam this side. Today we are going to cover the problems of the triangles. They’re always deep in trouble and so we are going to help them today. We are going o learn how to solve these problems and how to go about thinking to solve them. I am going to take up this problem in Java, but if you want the code in C or C++ or any other language, just comment down. Happy to help.
If you just want the code, then head here.

Prerequisites:

  1. Basic Syntax for the execution of the code in any language. And that’s it.

Let’s Start.

Both the problems are the same, the star problem and the number problem. It’s just a triangle, No biggie. We will tackle this problem head on and try to think how to solve them. It’s really simple, believe me.
Both the problems are the same but one involve stars and the other involve numbers. We will take a beautiful look at both of them. Let’s start.

1. Triangle thinking of a 5 digit Salary — 1 2 3 4 5.

Let’s take a deep breath and understand what we want to do. Let’s make a list.

  1. We want to print 1 in the 1st line.
  2. Then we want to enter a new line.
  3. Then we want to print 1 and 2 in 2nd line.
  4. Then we want to enter a new line.
  5. And this process keeps on repeating.
So what we want to do is move down the lines while printing the numbers starting from 1 and one number extra in each line. Yes?

Let’s make a code to move down the lines first.class Test {public static void main(String...args) {
 int n=5;
 for (int i=0;i<n;i++) {
  System.out.println();
 }}

What we’ve done is we have used a for loop to move down the code while printing a new line each time. We have initialsed two variables. One is n which is how many lines you want to print. I’ve supposed we want to print 5 lines. Also, I have initialised i for the loop. And an empty print statement.

Now if you run this code, it will print something like this.

An empty screen.

Good, now we want to add some numbers and print them, right? This means we want to move in two directions. One is to go down and one is to go right , yes?
We moved down with a for loop. This means for moving right, we will have to use another loop to move from left to right. And because we want to move right while moving down, we will have to nest the two loops. Let’s build that.class test {public static void main(String...args) {for (int i=0;i<5;i++) {for (int j = 1; j <= 5; j++) {
   System.out.print(j);
  }
  System.out.println();
 }
}

What’s new here is I’ve declared another for loop inside the old for loop and initialized the variable j with 1 and then printed it. Easy?
See the oputput of this code.

So we have printed the same numbers again and again in every line. So we need something to print only 1 in the first line and then keep on increasing it. We do not need a loop here, that’s for sure. We need something to stop printing. It must be some condition.

As you see the original triangle we want to print, with every line we move down, we want one number extra in that line. This means the outer loop controls how many numbers we want in each line. And the inner loop moves right that means that the condition must be in the inner loop.

So let’s declare a variable k and let that be controlled by the outer for loop. We need that variable k to be a global variable because we want that whenever the outer for loop completes it’s one cycle it should increase the number of numbers in each line by 1.class test {public static void main(String...args) {
 int k = 1;for (int i=0;i<5;i++) {for (int j = 1; j <= 5; j++) {
   System.out.print(j);
  }
  k++;
  /*This variable,k is in the outer loop so that it is controlled by it*/
  System.out.println();
 }
}

We’ve declared the variable k , now we want to impose the condition on the inner for loop so that it restricts the printing of the numbers in each line while going down.
We need to print just 1 in 1st line and then keep on increasing that. If we see the code, we are increasing the k by one each time in the outer for loop, right?. Now we just want a small condition. What would that be?
If you see the inner loop, we declared the variable j with 1. and We’ve declared the variable k with 1too. Is it ringing any bells? No?
Okay, no problem. We should print the value of j in the inner looponly if it smaller or equal to k. Think about it.In that case, we have now restricted the code to print only 1 in the first line. And because of the restriction, the inner loop will run only once. And it will run 2 times in the second line.

This is the code.class test {public static void main(String...args) {
 int k = 1;for (int i=0;i<5;i++) {for (int j = 1; j <= 5; j++) {
   if (j <=k) /* the condition we needed */
    System.out.print(j);
  }
  k++;
  /*This variable,k is in the outer loop so that it is controlled by it*/
  System.out.println();
 }
}

The output is like this:

YEAAAYYY!!

2. Triangle in love with the stars.

So we’re done with the first part, now on to the second. This will not be difficult.
Obviously we will need two loops. One to move down and one to move right. If we build upon the previous code, let’s just replace the variable j
which was printing numbers with an asterisk (*).class test {public static void main(String...args) {
 int k = 1;for (int i=0;i<5;i++) {for (int j = 1; j <= 5; j++) {
   if (j <=k)
    System.out.print("*");
  }
  k++;
  System.out.println();
 }
}

This code will output like this.

See the difference? We wanted the stars upside down. We wanted the stars to be 5 in first line and the decrease it down in every line while moving down. If we come on the bedrock of the condition and why we placed it there, it was done to restrict the numbers of number in each line. So now what if we reverse the condition?class test {public static void main(String...args) {
 int k = 1;for (int i=0;i<5;i++) {for (int j = 1; j <= 5; j++) {
   if (j >= k)
    System.out.print("*");
  }
  k++;
  System.out.println();
 }
}

We just reversed the signs in the condition, and see the output.
And Voila!


If you reached this far, thank you for reading the post. If you liked it, please comment and clap. Also share. ❤