How to swap two variables, without using third variable ?
Answers were Sorted based on User's Feedback
Answer / suseela
for ex x=10,y=20
x=x+y=30(i.e 10+20)
y=x-y=10(i.e 30-20)
x=x-y=20(i.e 30-10)
Is This Answer Correct ? | 0 Yes | 1 No |
Answer / shubham
#include<stdio.h>
#imclude<conio.h>
main()
{
int a,b;
printf("enter two number");
scanf("%d%d",&a,&b);
b=a-b;
a=a-b;
b=a+b;
printf("after swaping &d",swap);
getch();
}
Is This Answer Correct ? | 0 Yes | 1 No |
Answer / hamsa
a=a+b;
b=a-b;
a=a-b;
ex a=2, b=4
a=2+4
b=6-4
a=6-2
ANS a=4
b=4
Is This Answer Correct ? | 0 Yes | 1 No |
Answer / debjyoti talukder
a=a+b;
if(a>b)//only the smaller can b subtracted from greater...
b=a-b://...reverse is not possible
else
b=b-a;
a=a-b;
Is This Answer Correct ? | 0 Yes | 1 No |
Answer / swapnil r
Swapping two variables
1. #define SWAP(x,y) x^=y^=x^=y
2. a=a+b;
b=a-b;
a=a-b;
3. use xor to swap
a = a^b
b= a^b
a= a^b
4. a=a*b;
b=a/b;
a=a/b;
Is This Answer Correct ? | 1 Yes | 3 No |
To reverse an entire text file into another text file.... get d file names in cmd line
main() { char *p; int *q; long *r; p=q=r=0; p++; q++; r++; printf("%p...%p...%p",p,q,r); }
main() { static int var = 5; printf("%d ",var--); if(var) main(); }
main() { int a=10,*j; void *k; j=k=&a; j++; k++; printf("\n %u %u ",j,k); }
#define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr, int)); }
Write a C program to add two numbers before the main function is called.
main() { struct date; struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); }
Is the following code legal? struct a { int x; struct a b; }
main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); }
Write, efficient code for extracting unique elements from a sorted list of array. e.g. (1, 1, 3, 3, 3, 5, 5, 5, 9, 9, 9, 9) -> (1, 3, 5, 9).
13 Answers Intel, Microsoft, TCS,
# include <stdio.h> int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); }
Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it.