1) WTAHT will be the output of the following Printf function.
printf("%d",printf("%d",printf("%d",printf("%s","ILOVECPROGRAM"))));
Ans-ILOVECPROGRAM1321
The above printf line gives output like this because printf returns the number of character successfully written in the output.So the inner printf("%s","ILOVECPROGRAM") writes 13 characters to the output so the outer printf function will print 13 and as 13 is of 2 characters so the next outer printf function will print 2 and then next outer printf will print 1 as 2 is one character.So is the output 1321
2-Write code snippets to swap two variables in five different ways.
printf("%d",printf("%d",printf("%d",printf("%s","ILOVECPROGRAM"))));
Ans-ILOVECPROGRAM1321
The above printf line gives output like this because printf returns the number of character successfully written in the output.So the inner printf("%s","ILOVECPROGRAM") writes 13 characters to the output so the outer printf function will print 13 and as 13 is of 2 characters so the next outer printf function will print 2 and then next outer printf will print 1 as 2 is one character.So is the output 1321
2-Write code snippets to swap two variables in five different ways.
Answer:
a. /* swapping using three variables*/ (Takes extra memory space)
Int a=5, b=10, c;
c=a;
a=b;
b=c;
b. /* using arithmetic operators */
a=a+b;
b=a-b;
a=a-b;
c. /* using bit-wise operators */
a=a^b;
b=b^a;
a=a^b;
Line
|
Operation
|
Value of a
|
Value of b
| |
1
|
-
|
5
|
10
|
Initial values
|
2
|
a=a^b
|
15
|
10
| |
3
|
b=a^a
|
15
|
5
| |
4
|
a=a^b
|
10
|
5
|
values after swapping
|
d. /* one line statement using bit-wise operators */ (most efficient)
a^=b^=a^=b;
The order of evaluation is from right to left. This is same as in approach (c) but the three statements are compounded into one statement.
e. /* one line statement using arithmetic & assignment operators */
a=(a+b) - (b=a);
In the above axample, parenthesis operator enjoys the highest priority & the order of evaluation is from left to right. Hence (a+b) is evaluated first and replaced with 15. Then (b=a) is evaluated and the value of a is assigned to b, which is 5. Finally a is replaced with 15-5, i.e. 10. Now the two numbers are swapped.
3- Find the maximum & minimum of two numbers in a single line without using any condition & loop.
Answer:
void main ()
{
int a=15, b=10;
printf (“ max = %d, min = %d”, ((a+b) + abs(a-b)) /2, ((a+b) – abs (a-b)) /2);
}
4. How to print number from 1 to 100 without using conditional operators.
Answer:
void main ()
{
int i=0;
while (100 – i++)
printf (“ %d”, i);
}
a. 2.890000
b. 2.89
c. 2
d.3
b. 2.89
c. 2
d.3
Answer
Answer:d
5-#include<stdio.h>
int main(){
int i,j,k;
for(i=0,j=2,k=1;i<=4;i++){
printf("%d ",i+j+k);
}
return 0;
}
Output: 3 4 5 6 7
No comments:
Post a Comment