What will be the output of the following C# code? [code] static void Main(string[] args) { int n = 5; int x = 4; int z,

Đức Lưu

New member
What will be the output of the following C# code?
Code:
 static void Main(string[] args)
 {
     int n = 5;
     int x = 4;
     int z, c, k;
     z = 3 * x * x + 2 * x + 4 / x + 8;
     for (c = 1; c <= n; c++)
     {
         for (k = 1; k <= c; k++)
         {
             Console.Write(Convert.ToString(Convert.ToChar(z)));
             z++;
         }
         Console.WriteLine("\n");
     }
     Console.ReadLine();
 }
A.
A
AA
AAA
AAAA
AAAAA
B.
A
AB
ABC
ABCD
ABCDE
C.
A
BC
DEF
GHIJ
KLMNO
D.
A
AB
BC
BCD
BCDE
 
Answer: c
Explanation: Solving expression 'z' value is 65. Before going inside first loop
Step 1: c = 1, n = 5
k = 1, k <= 1. (as c = 1)
z = 65 converted to 'A' as ascii value of 'A' is 65.
z++ increment for next loop condition by '1'as 66.
Row 1: A
Step 2: c = 2, n = 5
k = 2, k <= 2. (as c = 2)
z = 66 from step 1 converted value of 66 is 'B'. Since, k < =2
loop will again loop to second value after 66 which is 67 as z is
incremented from 66 to +1 as '67'. So, converting ascii value of 67 to character as 'C'.
Row 2: B C
Similarly,
Step 3:
Row 3: D E F
Step 4:
Row 4: G H I J
Step 5:
Row 5: K L M N O.
Code:
Output:  A
         BC
         DEF
         GHIJ
         KLMNO
 

Tìm kiếm

Back
Top