ProgrammingHardTCS NQT · Infosys SP
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer c = 0
for (each a from 1 to 20)
for (each b from a to 20)
for (each h from b to 20)
if (a * a + b * b == h * h)
c = c + 1
end if
end for
end for
end for
Print c- A. 6
- B. 12
- C. 3
- D. 5
Correct answer: A. 6
With a ≤ b ≤ h ≤ 20 each right triangle is counted once: (3, 4, 5), (6, 8, 10), (5, 12, 13), (9, 12, 15), (8, 15, 17), (12, 16, 20). 12 ignores b starting from a and counts each triangle twice, 3 keeps only the primitive triples, and 5 misses (12, 16, 20) by treating 20 as excluded.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? String s = "Tcs NQT 2024 Exam!" Integer v = 0, c = 0, d = 0, o = 0 for (each ch in s)…
- What will be the output of the following pseudocode? String s = "AAAAA", r = "" Integer i = 0 while (i < length(s)) if (i + 1 < length(s)…
- What will be the output of the following pseudocode? (code(c) gives the ASCII code: 'A' = 65, 'a' = 97, '0' = 48; ^ is XOR) Integer x =…
- What will be the output of the following pseudocode? (| is OR, & is AND, << and >> are shifts) String s = "THE QUICK BROWN FOX" Integer…
- What will be the output of the following pseudocode? (Arrays are indexed from 0; abs gives the absolute value) Integer a[8] = {4, 3, 2, 7…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[4] = {3, 4, -1, 1}, n = 4 for (each i from 0 to…