How to swap two variables, without using third variable ?
Answers were Sorted based on User's Feedback
Answer / yogendra
a=5
a=5,b=10
a=a+b
a=10+5=15
a=15
b=a-b
b=15-10
b=5
a=a-b
a=15-5
a=10 and b=5
a=10 and b=5
| Is This Answer Correct ? | 0 Yes | 2 No |
Answer / praveen kumar kesani
x=10,y=20;
x=x*y;
y=x/y;
x=x/y;
after swapping : x=20,y=10
| Is This Answer Correct ? | 2 Yes | 5 No |
Answer / ruchi
let the two numbers are a & b
a=a+b
b=a-b
a=a-b
let a=5 & b=10
a=a+b=15
b=a-b=15-10=5
b=5
a=a-b=15-5=10
hence a & b become 10 & 5
| Is This Answer Correct ? | 0 Yes | 3 No |
Answer / balusamy
Try this answer for reverse a string
#include<stdio.h>
void main()
{
char array[50] = "thgir si rewsna ym";
int len,i,j,temp;
len = strlen(array);
for(i = 0, j = len - 1; i < j; i++, j--)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
printf("%s\n",array);
}
| Is This Answer Correct ? | 0 Yes | 4 No |
Answer / smita
suppose a=5 and b=10
a=a*b ==>a=50
b=a/b ==>b=5;
a=a/b ==>a=10;
| Is This Answer Correct ? | 1 Yes | 5 No |
Answer / ashok
The first two answers are correct. Third will FAIL in the
case the second num is 0...Please do not post wrong answer
| Is This Answer Correct ? | 3 Yes | 8 No |
Answer / some guy
declare a fourth variable and use that.
I dont understand why you need to do any of the above ??? if fourth is a problem, declare fifth and so on...
| Is This Answer Correct ? | 2 Yes | 7 No |
Answer / robince kumar
//C++ code..
void main()
{
int number1,number2;
cout<<"Enter 1st number;
cin>>number1;
cout<<"Enter 2nd number;
cin>>number2;
number1=number1*number2;
number2=number1/number2;
number1=number1/number2;
getch();
}
| Is This Answer Correct ? | 0 Yes | 6 No |
const int perplexed = 2; #define perplexed 3 main() { #ifdef perplexed #undef perplexed #define perplexed 4 #endif printf("%d",perplexed); } a. 0 b. 2 c. 4 d. none of the above
What is the output for the following program main() { int arr2D[3][3]; printf("%d\n", ((arr2D==* arr2D)&&(* arr2D == arr2D[0])) ); }
union u { struct st { int i : 4; int j : 4; int k : 4; int l; }st; int i; }u; main() { u.i = 100; printf("%d, %d, %d",u.i, u.st.i, u.st.l); } a. 4, 4, 0 b. 0, 0, 0 c. 100, 4, 0 d. 40, 4, 0
plz tell me the solution.......... in c language program guess any one number from 1 to 50 and tell that number within 8 asking question in yes or no...............
Cau u say the output....?
#define int char main() { int i=65; printf("sizeof(i)=%d",sizeof(i)); }
Find the largest number in a binary tree
struct aaa{ struct aaa *prev; int i; struct aaa *next; }; main() { struct aaa abc,def,ghi,jkl; int x=100; abc.i=0;abc.prev=&jkl; abc.next=&def; def.i=1;def.prev=&abc;def.next=&ghi; ghi.i=2;ghi.prev=&def; ghi.next=&jkl; jkl.i=3;jkl.prev=&ghi;jkl.next=&abc; x=abc.next->next->prev->next->i; printf("%d",x); }
Display the time of the system and display the right time of the other country
#define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr, int)); }
main() { clrscr(); } clrscr();
why do you use macros? Explain a situation where you had to incorporate macros in your proc report? use a simple instream data example with code ?