0% found this document useful (0 votes)
15 views

Nested Loop

Uploaded by

nu418792
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Nested Loop

Uploaded by

nu418792
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1

Using system;

Class program

Static void main()

// ask the user to input an integer

Console.write(“enter an integer: “);

Int n = int.parse(console.readline());

// outer loop for each row

For (int i = 1; i <= n; i++)

// inner loop to print numbers from 1 to i

For (int j = 1; j <= i; j++)

Console.write(j + “ “);

// print a new line after each row

Console.writeline();

2
Using system;

Class alphabettriangle

Static void main()

// prompt for input

Console.write(“enter the number of rows: “);

Int n = int.parse(console.readline());

// initialize the starting character (ascii code for ‘a’ is 65)

Int currentcharcode = 65;

// outer loop for the number of rows

For (int i = 1; i <= n; i++)

// inner loop to print characters in each row

For (int j = 1; j <= i; j++)

// print the current character

Console.write((char)currentcharcode);

// move to the next character in the ascii table

Currentcharcode++;
// if the current character code exceeds 126 (‘~’ in ascii), reset to
65 (‘a’)

If (currentcharcode > 126)

Currentcharcode = 32; // reset to space (ascii 32)

// print a space after each character except for the last one in the
row

If (j < i)

Console.write(“ “);

// move to the next line after each row

Console.writeline();

Using system;

Class program

Static void main()


{

Console.write(“enter an integer: “);

Int n = int.parse(console.readline());

Console.writeline(“\nmultiplication table”);

For (int x = 1; x <= n; x++)

For (int y = 1; y <= n; y++)

Console.write(x * y + “\t”); // fixed the multiplication operator

Console.writeline();

Using system;

Class program

Static void main()

// get inputs from the user

Console.write(“enter first character: “);

Char firstchar = console.readline()[0];


Console.write(“enter second character: “);

Char secondchar = console.readline()[0];

Console.write(“enter size: “);

Int size = int.parse(console.readline());

// generate the pattern

For (int i = 0; i < size; i++)

// print hyphens for the current row

For (int j = 0; j < i; j++)

Console.write(“-“);

// print the alternating character for the current row

If (i % 2 == 0)

Console.writeline(firstchar);

Else

Console.writeline(secondchar);

}
}

Using system;

Class program

Static void main()

// get input from the user

Console.write(“enter a number: “);

Int size = int.parse(console.readline());

// generate the pattern

For (int i = 1; i <= size; i++)

// print spaces to shift the triangle to the right

For (int j = 0; j < size – i; j++)

Console.write(“ “);

// print the character literal of the current row

For (int j = 0; j < i; j++)

Console.write(i);
}

// move to the next line

Console.writeline();

You might also like