FAQ-c-programs-for-interview


C programming examples
C program to swap two numbers
Code:-1
#include
int main()
{
int x, y, temp;
printf(“Enter the value of x and y\n”);
scanf(“%d%d”, &x, &y);
printf(“Before Swapping\nx = %d\ny = %d\n”,x,y);
temp = x;
x
= y;
y = temp;
printf(“After Swapping\nx = %d\ny = %d\n”,x,y);
return 0;
}
Code:-2
Swapping of two numbers without third variable
#include
int main()
{
int a, b;
printf(“Enter two integers to swap\n”);
scanf(“%d%d”, &a, &b);
a = a + b;
b = a – b;
a = a – b;
printf(“a = %d\nb = %d\n”,a,b);
return 0;
}
Code:-3
Swapping numbers using call by reference
In this method we will make a function to swap numbers.
#include
void swap(int*, int*);
int main()
{
int x, y;
printf(“Enter the value of x and y\n”);
scanf(“%d%d”,&x,&y);
printf(“Before Swapping\nx = %d\ny = %d\n”, x, y);
swap(&x, &y);
printf(“After Swapping\nx = %d\ny = %d\n”, x, y);
return 0;
}
void swap(int *a, int *b)
{
int temp;
temp = *b;
*b = *a;
*a = temp;
}
Code:-4
C programming code to swap using bitwise XOR
#include
int main()
{
int x, y;
scanf(“%d%d”, &x, &y);
printf(“x = %d\ny = %d\n”, x, y);
x = x ^ y;
y = x ^ y;
x = x ^ y;
printf(“x = %d\ny = %d\n”, x, y);
return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s