f

Triangle of height N || Newton school Assignment solution || Newton school assignment Question and answer

Hey, guys how are you all so, in this blog, I am going to show you the solution to the triangle pattern of height N : 

I have taken this question from the Newton school assignment so the credit goes to Newton school only.

So if you are first time on my blog please make sure that you also ping a question and solution. if you have a better approach

Triangle of height N || Newton school Assignment solution || Newton school assignment Question and answer

 Problem Statement

Print a right-angled triangle of given height N as shown in the example.
Input
The input contains a single integer N.

Constraints:-
1 <= N <= 10
Output
Print the Right angled triangle of height N.

Sample Input:-
3

Sample Output:-
*
**
***

Sample Input:-
4

Sample Output:-
*
**
***
****






Triangle of height ||  Newton school assignment solution

Solution

   import java.io.*; 
  import java.util.*; 

  class Main

public static void main (String[] args) { // Your code here

Scanner sc =new Scanner(System.in); int abhinav = sc.nextInt();
// you can take any variable
for(int i=0; i<abhinav; i++){

for(int j=0;j<=i; j++){

System.out.print("*");
}
 System.out.println();
    }
  }
}


Let me explain to you through this code:

   import java.io.*; 
  import java.util.*; 

  class Main

public static void main (String[] args) { // Your code here
 


Till here I didn't do anything extra I just give the class name as man and if you give any other class name guys I make you sure that this could won't be run on your Newton's School platform so if you are running this code on Newton School platform always try to give the class name as a main class name after that I just define the return type of of men method which have to define.

Scanner sc =new Scanner(System.in); int abhinav = sc.nextInt();
// you can take any variable
for(int i=0; i<abhinav; i++){

for(int j=0;j<=i; j++){

System.out.print("*");
}
 System.out.println();
    }
  }
}
 Here you need to give more concentration because the first line I use scanner method to take input from users and after that I define one variable of name Abhinav and I just take the input from user through scanner and I just store them into abhinav variable.
After that I take one for loop and I it read it up to the number what the user has given to us suppose if user has given us food then we have to go from 0 to 4 here basically I do not the number of roads and J did not the number of columns so that in first for loop I itraate only rose and in the second for loop I torates only columns show the inner for loop will run till when the inner for loop got false sorry outer for loop girl false suppose the value of I is to the inner for loop will run two time if the value of i3 the inner for loop will run 3 time in this way if you see the pattern you will clear that in first so we have first star in second row we are second star in 3rd row we have three star so keep this things into the mind we made a logic that if J is less than equal to I then we print only start and in the other case we just print space
Tags

Post a Comment

2 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
  1. Hey guys if you have some better approach please ping me in the comment section

    ReplyDelete
  2. thank you for coding. its really helpfull

    ReplyDelete