C Programming :: Declarations and Initializations - Discussion
45 / 54
#include<stdio.h>
printd (int n)
{
if (n < 0)
{
printf ("-");
n = -n;
}
if (n % 10)
printf ("%d", n);
else
printf ("%d", n/10);
printf ("%d", n);
}
If initially n = -24;
#include<stdio.h>
printd (int n)
{
if (n < 0)
{
printf ("-");
n = -n;
}
if (n % 10)
printf ("%d", n);
else
printf ("%d", n/10);
printf ("%d", n);
}
If initially n = -24;
A-24
B24
C-2424
DNone of the above
Show Explanation
Initially, n=24
Line 3 => if(n<0), is true because -24<0.
Line 5 => "-" get printed.
Line 6 => n=-n, or n=-(-24), or n=24
Line 8 => if(n%10), or if(24%10), or if(4), which is true as "4 is not equal to 0" is true.
Line 9 => "24" gets printed
Here, the else block won't get executed as if block has already executed.
Finally, Line 12 => "24" gets printed.
Hence, the complete output is -2424
Asked In ::
Let us discuss the code line by line-
Initially, n=24
Line 3 => if(n<0), is true because -24<0.
Line 5 => "-" get printed.
Line 6 => n=-n, or n=-(-24), or n=24
Line 8 => if(n%10), or if(24%10), or if(4), which is true as "4 is not equal to 0" is true.
Line 9 => "24" gets printed
Here, the else block won't get executed as if block has already executed.
Finally, Line 12 => "24" gets printed.
Hence, the complete output is -2424
Read Full Answer
Report Error
Please Login First Click Here