Logo F2FInterview

C Interview Questions

Q   |   QA

Program:

#include <stdio.h>

int main() {
 int i, j, ctr = 1;
 for (i = 1; i < 5; i++) {
     for (j = 1; j <= i; j++){
       printf("%2d ", ctr++);
     }
     printf("\n");
 }
 return 0;
}

Explanation:
There are two loops, a loop inside another one. Outer loop iterates 5 times. Inner loop iterates as many times as current value of i. So for first time outer loop is executed, inner loop is executed once. Second time the outer loop is entered, inner loop is executed twice and so on. And every time the program enters inner loop, value of variable ctr is printed and is incremented by 1. %2d ensures that the number is printed in two spaces for proper alignment

Program:

#include <stdio.h>
int main() {
 int i = 65;
 int k = 120;
 printf("\n value of i=%d k=%d before swapping", i, k);
 i = i ^ k;
 k = i ^ k;
 i = i ^ k;
 printf("\n value of i=%d k=%d after swapping", i, k);
 return 0;
}

Explanation:

i = 65; binary equivalent of 65 is 0100 0001

k = 120; binary equivalent of 120 is 0111 1000

 

i = i^k;

i...0100 0001

k...0111 1000

---------

val of i = 0011 1001

---------

 

k = i^k

i...0011 1001

k...0111 1000

---------

val of k = 0100 0001 binary equivalent of this is 65

---------(that is the initial value of i)

 

i = i^k

i...0011 1001

k...0100 0001

---------

val of i = 0111 1000 binary equivalent of this is 120

---------(that is the initial value of k)

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: