int main(int i, int j) {
for (i=4; i>0; i--) {
j=i;
while (j>=0) {
printf("X");
j--;
}
printf("%lf\n", (1.0*(i)) / (j+2));
}
}
Output:
XXXXX 4.0
XXXX 3.0
XXX 2.0
XX 1.0
The >=
sign is probably confusing you. The code prints out X
5 times in the first row instead of 4 (if that's what you mean) is because once j = 0
, the loop continues once more.
Basically, the value of j
after the while
loop ends is -1
. Substitute it into your last printf
and the rest of the output makes sense.
If you change the >=
to a >
, the code will produce the correct output.
why does the first line print 5 "X's"? & why is the first number 4.0?
Because,
j takes values of j = 4,3,2,1,0. Each time it is printing X hence 5X.
After that loop j becomes -1.
Now 1.0*(4) /(-1+2)
=> (4.0/1)
=> 4.0
Hence the output is XXXXX 4.0
Well, j is set to i
, so it prints X
j+1
times due to the >= 0
check. Then divides i
by j
, now -1 + 2. which is now one, to get i
, which it prints to the screen before a newline begins.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.