What is the output of the following program int main int x y
Solution
The output of first program in 15. The forst loop starts with x =0 and its condition x<6 is true so it will exicute the body of the loop. It is an another loop call it as second loop, it assign y = 0 and it will check wether x<y or not when x=0 this cindition is false and it will not exicute its body that is z++; In the next iteration of the first loop the x becones 1 thus the second loop will exicute 1 time. thus z will become 1. In the third iteration of the first loop the second loop will exicute 2 times thus z will increment two times and become 3. Similarly in the 4th, 5th and 6th iterations z will increment 3,4 and 5 times respetively and it become 15. At the time of 7th iteration of the first loop x becomes 6 and the condition fails. Thus the program will exicute the next instruction after the loops, it is print statement of z value. Thus the program will print 15 as an output and terminates the program.
The output of second program is 8. This program will actually count the numer of letters in the string s. s is a string, n is an integer. In this program befor the while loop n is 0 and s is \"aardvark\". In the loop, initially it will check s[0] ! =0, s[0] is \'a\' and it is not a null character, thus the loops body will exicute, hence n will increment to 1. so in the next iteration the loop will check s[n]!=0 or not. s[1] is \'a\', not a null character so the loops body will exicute again and n become 2. This loop will continude until n become 8. when n become 8 the loop will check s[8]!=0 or not. This statement is false because s[8] is a null character, so loop lerminates its iteration and print the value 8 of n by the print instruction after the loop.
