82.9k views
2 votes
Assume that the int variables i and j have been declared, and that n has been declared and initialized.Using for loops (you may need more than one), write code that will cause a triangle of asterisks of size n to be output to the screen.I have this so far and I can't figure out what I'm missing:for (i = 1; i <= n; i++){ for (j = 1; j <= i; j++) { System.Console.Write("*"); }System.Console.Write();}

User ChrisJ
by
5.1k points

2 Answers

2 votes

Answer:

The correct program to this question can be given as:

Program:

using System;

class Pattern

//declare class.

{

static void Main(string[] args)

//declare main method .

{

int n,i,j;

//declare variable

Console.WriteLine("please enter the Number");

n= Convert.ToInt32(Console.ReadLine());

//user input

for (i = 1; i <= n; i++)

//for loop

{

for (j = 1; j <= i; j++)

{

System.Console.Write("*");

//print

}

System.Console.Write("\\");

}

Console.ReadLine();

}

}

Output:

please enter the Number

5

*

**

***

****

*****

Explanation:

In the given code there are many mistakes. So, the correct code is given above and the explanation of this program can be given as:

In the above C# program we firstly we declare the Using system It is used to run the code in the system. Then we declare the class that is Pattern in this we declare the main method in this method we declare 3 variable(n, i,j). In variable n we take input from the user, To take input from the user we use Console.ReadLine() that is used to take input and to convert the input into the number we used the Convert.ToInt32. It converts the string into a number, Then i,j variable is used is the loop. In the first loop, it starts from the 1 and ends from n and in the second loop, it starts from 1 and ends from i.When the condition is true it prints the triangle.

User Ben Richards
by
5.0k points
2 votes

Answer:

The following answer is written in C# programming language:

using System; //using directives

public class Program //define a class

{

public static void Main() //define main method

{

int n; //set an integer variable

String val; // set string variable

Console.Write("Enter an integer value - "); // print message

val=Console.ReadLine(); // read value from the user

n=Convert.ToInt32(val); // convert string input into integer

for(int i = 1; i <= n; i++) // set for loop

{

for (int j = 1; j <= i; j++) //set for loop

{

System.Console.Write(" * "); // print "*"

}

System.Console.WriteLine("");

}

}

}

Input:

Enter an integer value - 4

Output:

*

* *

* * *

* * * *

Step-by-step explanation:

Here, we define a class "program", then define void type main method.

Then, we set two-variable first is integer type "n" and second is string type "val".

Then, we print the message for the user and then get input from the user in "val".

Then, we convert the string variable into the integer.

Then, set the for loop which starts from 1 to n, again we the set for loop which starts from 1 to i.

Then, we print an asterisk "*" and after that break the line.

User Hazzelnuttie
by
5.3k points