Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


How to swap values between two variables without using a
third variable?

Answers were Sorted based on User's Feedback



How to swap values between two variables without using a third variable?..

Answer / shobha

my answer is very correct compared to the others.(1 to 3)

in answer 1 pinky declared a variable(ia) that is never
used in the program and used a variable(a) that is not
declared.finally pinky did not close main function i.e }

in answer 2 Banti Sharma has written the logic of the
program like a program, but he did not wrote the actual
program,if it is a program then the output is nothing,
because there is no statement present for displaying the
values of a and b


in answer 3 Mona also written the logic of the program like
Banti Sharma but Mona wrote it in a method.and mona never
called that method and from where mona is passing values to
var1 and var2.and mona did not displayed the result.

but in answer 4, that means in my answer,

i did not write the entire program,i just explained the
logic how u can swap without the third variable.

i have taken two variables and assigned sample values
(x=10,y=20) to them and explained each and every line with
that sample values, such that the person who asked this
question can write a program of his own.

Banti Sharma, i think u are not trying to answer the
questions but u are trying to point out the silly mistakes
in answers posted by others.

Is This Answer Correct ?    10 Yes 11 No

How to swap values between two variables without using a third variable?..

Answer / sushma rani

Here we have two variable A=4, B=3

we need to swap values without taking any third variable
result should be A=3, B=4

for that we can do ,
A=A+B-A (e.g. A=4+3-4 => 3)
and
B=B+A-B (e.g. B=3+4-3 => 4)

That's it....

Thanks & Regards
Sushma Rani
Bangalore
===========

Is This Answer Correct ?    3 Yes 7 No

How to swap values between two variables without using a third variable?..

Answer / banti sharma

Miss Sobha upto My Knowlegde the answer of "Shiv" was most
appropriate and if you have Any knowledge of Programming I
repeat again "Any knowledge of programming" then you may
find it out yourself that Bitwise Shift Operation is much
more time Efficient then that of "+","-","*","/.And if I say
about my answer then it is also correct having No mistakes
as the Question is "How to swap values between two variables
without using a third variable?" so if you read it carefully
then it does not say anywhere to print the Swaped values.I
think you have got Y i wrote like that,IF I say about
another logic then one more logic may be used like:

a=a*b;
b=a/b;
a=a/b;

though I m using Multiply and Devision still its better then
my past logic(so better then urs aytomatically) now if You
may find y? multiplication/Devision is more efficient then
Plus/minus then Do Googling.
have a good day

Is This Answer Correct ?    7 Yes 12 No

How to swap values between two variables without using a third variable?..

Answer / kiran

int a=10,b=20;
a=b;
b=a/2;

Is This Answer Correct ?    3 Yes 49 No

Post New Answer

More Programming Languages AllOther Interview Questions

how to invoke the macro in another macro?

1 Answers   Accenture,


In an customer exit (include Z) program, only the last record is getting fetched. The exit is getting called after pressing save button. What to write for fetching the first record ? Before saved to a SAP table how can i call all these records in an itab in the exit ?

0 Answers  


what is the difference between an OS(operating system) and Framework?

0 Answers  


what is web service in java? have u use before.

0 Answers  


How can you incorporate a Datawindow to a Oracle8i stored procedure?

0 Answers   IBM,


I'm trying to solve this. But I'm not figuring the right solution. Can some one help what the answer is for the question below? You can use as many variables as you need, there are no negative numbers, all numbers are integers. You do not know the size of the integers, they could be infinitely large, so you can't count on truncating at any point. There are NO comparisons allowed, no if statements or anything like that. There are only four operations you can do on a variable. 1) You can set a variable to 0. 2) You can set a variable = another variable. 3) You can increment a variable (only by 1), and it's a post increment. 4) You can loop. So, if you were to say loop(v1) and v1 = 10, your loop would execute 10 times, but the value in v1 wouldn't change so the first line in the loop can change value of v1 without changing the number of times you loop. You need to do 3 things. 1) Write a function that decrements by 1. 2) Write a function that subtracts one variable from another. 3) Write a function that divides one variable by another. 4) See if you can implement all 3 using at most 4 variables. Meaning, you're not making function calls now, you're making macros. And at most you can have 4 variables. The restriction really only applies to divide, the other 2 are easy to do with 4 vars or less. Division on the other hand is dependent on the other 2 functions, so, if subtract requires 3 variables, then divide only has 1 variable left unchanged after a call to subtract. Basically, just make your function calls to decrement and subtract so you pass your vars in by reference, and you can't declare any new variables in a function, what you pass in is all it gets.

1 Answers   Microsoft,


I've an application where i need to give access to all the features only to admin and only few features to normal users. Say Menu...i dont want all my menu items to be accessible to all the users only the admin people can see few all the features where as normal users can have access to limited menu items...how can i achieve this. Please note that my menu is not a database driven menu.

1 Answers   Tesco,


what is fisrt female program

1 Answers  


what is meaning of MDM in sap?let me know that meaning

0 Answers  


write the a cl program with the following specification A. Accept 2 parameters-date and date type B. if date type is J then convert date to *MDY format C. if date type is M convert date to *JUL format 4.send a program message with the value of converted date Please explain for each with coding?

0 Answers  


There are 2 tables: EMP : EmpId, Ename, Sal, DeptId DEPT : DeptId, Dname Write a query to find out emp names and their department names. if any emp has null in Deptid the it shows ?No Department?. Write a query to find out those department names which has no employee. Write a query to find out those employees whose salary is greater than their department?s average salary.

1 Answers   Oracle,


Programs in JAVA to get the remainder and quotient of given two numbers without using % and / operators?

2 Answers  


Categories