Sponsored Links

Interview Questions



INTERVIEW QUESTIONS C ARRAYS IN C DETAILS

Question: Code for swapping of two numbers without using temporary variable using C.

Answer: int a=10,b=5;

a=a+b; // a=15
b=a-b; // b=10
a=a-b; // a=5

Category Arrays in C Interview Questions & Answers - Exam Mode / Learning Mode
Rating (0.3) By 7681 users
Added on 11/15/2012
Views 74690
Rate it!

Question: Code for swapping of two numbers without using temporary variable using C.

Answer:

int a=10,b=5;

a=a+b; // a=15
b=a-b; // b=10
a=a-b; // a=5
Source: CoolInterview.com

Answered by: Mohan | Date: 11/27/2007 | Contact Mohan Contact Mohan

main()
{
int a,b;
printf("enter the values of a,b");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("after swapping the values of a and b");
} Source: CoolInterview.com

Answered by: sukavasi suresh | Date: 11/27/2007 | Contact sukavasi suresh Contact sukavasi suresh

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;

printf("enter a , b");
scanf("%d%d",&a,&b);
printf("a: %d
b: %d ",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("a: %d",a);
printf("b: %d",b);
getch();
} Source: CoolInterview.com

Answered by: sourav | Date: 11/27/2007 | Contact sourav Contact sourav

main()
{
int a,b;
printf("Enter values for a & b");
scanf("%d""%d",@a,@b);
if(a==b)
printf("enter valid numbers");
else
{
a=a+b; a=a*b;
b=a-b; /*(or)*/ b=a/b;
a=a-b; a=a/b;
}
printf("a=%d b=%d",&a,&b);
} Source: CoolInterview.com

Answered by: tirumalarao peddineni | Date: 11/28/2007 | Contact tirumalarao peddineni Contact tirumalarao peddineni

By using this simple arithmatic you can solve this problem.
======
a=10;
b=20;

a=a+b
b=a-b
a=a-b
====== Source: CoolInterview.com

Answered by: Ashish | Date: 11/29/2007 | Contact Ashish Contact Ashish

int a=10, b=20;
a=a+b;
b=a-b;
a=a-b;
Source: CoolInterview.com

Answered by: Ashish | Date: 11/29/2007 | Contact Ashish Contact Ashish

code is :
b=a+b;
a=b-a;
b=b-a; Source: CoolInterview.com

Answered by: murali | Date: 11/29/2007 | Contact murali Contact murali

a,b
a=a+b
b=a-b
a=a-b
now a and b are swapped Source: CoolInterview.com

Answered by: girish | Date: 11/29/2007 | Contact girish Contact girish

int i=9,j=8;
i=i+j;
j=i-j;
i=i-j;
printf("J = ",&j);
System.out.println("I =",&i); Source: CoolInterview.com

Answered by: bablu | Date: 11/29/2007 | Contact bablu Contact bablu

int a = 10;
int b = 20;

b = a + b;
a = b - a;
b = b - a;
printf ("a = %d, b = %d
", a, b); Source: CoolInterview.com

Answered by: Nathan | Date: 11/30/2007 | Contact Nathan Contact Nathan

a=a+b;
b=a-b;
a=a-b;
Source: CoolInterview.com

Answered by: topcoder | Date: 12/2/2007 | Contact topcoder Contact topcoder

void main()
{int a=50,b=100;
a=a+b; //a=50+100, a= 150
b=a-b; //b=150-100, b= 50
a=a-b; //a=150-50, a=100
} Source: CoolInterview.com

Answered by: Sarwat | Date: 12/2/2007 | Contact Sarwat Contact Sarwat

swapping of two numbers without using temp veriable

int a,b;
a=a+b;
b=a-b;
a=a-b;
now print a and b with swapped values Source: CoolInterview.com

Answered by: SUKHCHAIN SANDHU | Date: 12/3/2007 | Contact SUKHCHAIN SANDHU Contact SUKHCHAIN SANDHU

#include <stdio.h>
void main()
{
int a,b;
printf("
Enter the two number ::");
scanf("%d %d",&a,&b);
/*Swapping*/
a=a+b;
b=a-b;
a=a-b;
printf("The swapped numbers are::%d %d",a,b);
}
Source: CoolInterview.com

Answered by: Jai | Date: 12/3/2007 | Contact Jai Contact Jai

one method :

a=a+b;
b=a-b;
a=a-b;


other method :
a^=b^=a;
Source: CoolInterview.com

Answered by: vikas kr | Date: 12/4/2007 | Contact vikas kr Contact vikas kr

a=a-b;
b=a+b;
a=b-a; Source: CoolInterview.com

Answered by: Vikas | Date: 12/4/2007 | Contact Vikas Contact Vikas

#include<stdio.h>

int main()
{
int a,b;
printf("
Enter the values for a and b");
scanf("%d%d",&a,&b);
printf("
Value of a and b before swapping are ::
a = %d b = %d

",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("
Value of a and b after swapping are ::
a = %d b = %d

",a,b);
system("pause");
return 0;
}
Source: CoolInterview.com

Answered by: Syed Baseer Ahmed | Date: 12/4/2007 | Contact Syed Baseer Ahmed Contact Syed Baseer Ahmed

#include<stdio.h>

int main()
{
int a,b;
printf("Enter the two number a and b
");
scanf("%d%d", &a,&b);
printf("Number before swapping a = %d b = %d", a,b);
a = a + b;
b = a - b;
a = a - b;
printf(" Number after swapping a = %d b = %d", a,b); Source: CoolInterview.com

Answered by: praveen.c | Date: 12/4/2007 | Contact praveen.c Contact praveen.c

#include<stdio.h>

int main()
{
int a,b;
printf("Enter the two number a and b
");
scanf("%d%d", &a,&b);
printf("Number before swapping a = %d b = %d", a,b);
a = a + b;
b = a - b;
a = a - b;
printf(" Number after swapping a = %d b = %d", a,b);
return 0;
} Source: CoolInterview.com

Answered by: praveen.c | Date: 12/4/2007 | Contact praveen.c Contact praveen.c

1st method :
a = a + b;
b = a - b;
a = a - b;

2nd method :
a = a xor b;
b = b xor a;
a = a xor b; Source: CoolInterview.com

Answered by: Satish Aradhya | Date: 12/5/2007 | Contact Satish Aradhya Contact Satish Aradhya

a,b are two variables...
a=a+b;
b=a-b;
a=a-b; Source: CoolInterview.com

Answered by: Naga Bhushanam | Date: 12/5/2007 | Contact Naga Bhushanam Contact Naga Bhushanam

main()
{
int a,b;
printf("Enter values of a,b");
scanf("%d%d",&a,&b);//forex:u enter 100,50
a=a+b;//a=150
b=a-b;//b=100
a=a-b;//a=50
printf("a=%d b=%d",a,b);
}
Source: CoolInterview.com

Answered by: uppala mamatha | Date: 12/5/2007 | Contact uppala mamatha Contact uppala mamatha

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b;
printf(" Enter two values");
scanf("%d%d",&a,&b);
a=a-b;
b=abs(a+b);
a=abs(a-b);
printf(" Swaped values are %d,%d",a,b);
getch();
} Source: CoolInterview.com

Answered by: Jeevan K.M. | Date: 12/6/2007 | Contact Jeevan K.M. Contact Jeevan K.M.

#include<stdio.h>
void main()
{
int a=5,b=7;
a=a+b;
b=a-b;
a=a-b;
printf("%d %d",a,b);
}
output:
7 5 Source: CoolInterview.com

Answered by: sunanda | Date: 12/6/2007 | Contact sunanda Contact sunanda

a=a+b;
b=a-b;
a=a-b; Source: CoolInterview.com

Answered by: vijay jain | Date: 12/6/2007 | Contact vijay jain Contact vijay jain

If a and b are two variables then swapping can be done in a single statement without using any temporary variable.

a^=b^=a^=b; Source: CoolInterview.com

Answered by: Manoj Kumar | Date: 12/7/2007 | Contact Manoj Kumar Contact Manoj Kumar

main()
{
int x,y;
temp z;
x = y;
y = z;
z = x;
printf("%d%d",x,y);
} Source: CoolInterview.com

Answered by: D.N.Pani | Date: 12/8/2007 | Contact D.N.Pani Contact D.N.Pani

void main()
{
int x=5, y=10;
swap (x,y);
printf(“%d %d
”,x,y);
swap2(x,y);
printf(“%d %d
”,x,y);
} Source: CoolInterview.com

Answered by: lokesh | Date: 12/9/2007 | Contact lokesh Contact lokesh

#define swap(a,b) a=a+b;b=a-b;a=a-b;

void main()
{
int x=5, y=10;
swap (x,y);
printf(“%d %d
”,x,y);
swap2(x,y);
printf(“%d %d
”,x,y);
}

int swap2(int a, int b)
{
int temp;
temp=a;
b=a;
a=temp;
return 0;

}
Source: CoolInterview.com

Answered by: lokesh | Date: 12/10/2007 | Contact lokesh Contact lokesh

use, 2 variables a, b.then try this code,

a=a+b;
b=a-b;
a=a-b;

that's it Source: CoolInterview.com

Answered by: nethaji | Date: 12/10/2007 | Contact nethaji Contact nethaji

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("
Enter 2 no: ");
scanf("%d%d",&a,&b);
printf("
Before swapping a=%d b=%d",a,b);
a=(a+b)-(b=a);
printf("
After swapping a=%d b=%d",a,b);
getch();
}

/*this program is executed in
turboc3 */
Source: CoolInterview.com

Answered by: sudarsan dash | Date: 12/10/2007 | Contact sudarsan dash Contact sudarsan dash

Using XOR operation will also
Solve the purpose
a=7,
b=10,

a = a^b;//a=13
b = a^b;//b=7
a = a^b;//a=10

a and b are swapped Source: CoolInterview.com

Answered by: Sagar | Date: 12/18/2007 | Contact Sagar Contact Sagar

int a,b;
a=a^b;
b=a^b;
a=a^b;
Source: CoolInterview.com

Answered by: vish | Date: 12/23/2007 | Contact vish Contact vish

#include<stdio.h>
#include<conio.h>

main()
{
int a=5,b=7;

a=a^b;
b=a^b;
a=a^b;
printf("a is %d b is %d
",a,b);
getch();
return 0;
} Source: CoolInterview.com

Answered by: Essam Ali | Date: 1/3/2008 | Contact Essam Ali Contact Essam Ali

void main()
{
int a=5,a=10;
printf("
A=%d B=%d",a,b);
a=a-(b=(a=a+b)-b);
printf("
A=%d B=%d",a,b);
} Source: CoolInterview.com

Answered by: Ram | Date: 2/1/2008 | Contact Ram Contact Ram

This snippet of code will swap 2 numbers
a = a + b - (b = a) ;
also we can use ^ operator
a^b;b^a;a^b; Source: CoolInterview.com

Answered by: bharath | Date: 2/6/2008 | Contact bharath Contact bharath

If a and b are two variables then we can swapped with this code
a=a*b/(b=a) Source: CoolInterview.com

Answered by: DHEERAJ SHARMA | Date: 3/11/2008 | Contact DHEERAJ SHARMA Contact DHEERAJ SHARMA

/*using pointers*/
main()
{
int *a=10, *b=4;
printf("BEFORE SWAPPING THE VALUES OF A & B ARE %d%d"*a,*b);
*a=*a+*b; //*a=14
*b=*a-*b; //*b=10;
*a=*a-*b; //*a=4
printf("AFTER SWAPPING THE VALUES OF A & BARE %d%d"*a,*b);
}

Source: CoolInterview.com

Answered by: venkatarathnam.m.v | Date: 3/15/2008 | Contact venkatarathnam.m.v Contact venkatarathnam.m.v

#include<stdio.h>
#include<conio.h>
void main()
{
int a=3,b=2;
clrscr();
printf("The values of a and b before swapping are %d%d",a,b);
a^=b^=a^=b;
printf("The values of a and b after swapping are %d%d",a,b);
getch();
} Source: CoolInterview.com

Answered by: mahila | Date: 7/11/2008 | Contact mahila Contact mahila

main()
{
int a=5,b=10;
printf("before swap %d %d",a,b);
a=(a*b)/(b=a);
printf("after swap %d %d",a,b); Source: CoolInterview.com

Answered by: ravindrabonthu | Date: 10/7/2008 | Contact ravindrabonthu Contact ravindrabonthu

#include<stdio.h>
void main()
{
int a,b;
printf("%d%d",a,b);
a=a^b;
b=a^b;
a=a^b;
printf("a=%d,b=%d",a,b);
}
Source: CoolInterview.com

Answered by: Praveen | Date: 12/17/2008 | Contact Praveen Contact Praveen

a=10;
b=20
b=((a+b)-(a=b))//in a single line Source: CoolInterview.com

Answered by: Debasis Nayak | Date: 12/28/2008 | Contact Debasis Nayak Contact Debasis Nayak

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter a and b");
scanf("%d%d",&a,&b);
a=a*b;
b= a/b;
a = a/b;
printf("Swapped values are:%d,%d",a,b);
getch();
} Source: CoolInterview.com

Answered by: GAUATM DESHPANDE. | Date: 7/28/2009 | Contact GAUATM DESHPANDE. Contact GAUATM DESHPANDE.

#include<stdio.h>
main()
{
int a,b,i;
clrscr();
printf("enter the value of a and b");
scanf("%d %d",&a,&b);
a=a+b;
printf("a=%d,b=%d",a=a-b,b=a-b);
} Source: CoolInterview.com

Answered by: Govind | Date: 8/22/2009 | Contact Govind Contact Govind

main()
{
int a=6,b=3;
a=a^b;
b=a^b;
a=a^b;
printf("%d %d",a,b);
}
output:
3 6
othermethod
a=a+b;
b=a-b;
a=a-b; Source: CoolInterview.com

Answered by: sushma | Date: 12/9/2009 | Contact sushma Contact sushma

#include<iostrem.h>
#include<conio.h>
void main()
{
int a,b,t;
printf("enter the nos");
scanf("%d%d",&a,&b);
t=a;
a=b;
b=t;
printf("the nos after swapping",
a,b);
getch();
} Source: CoolInterview.com

Answered by: jayasree | Date: 8/27/2010 | Contact jayasree Contact jayasree


If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification Enter the above shown code: *
Inform me about updated answers to this question

Related Questions
View Answer
Is it better to use a pointer to navigate an array of values,or is it better to use a subscripted array name?


View Answer
Can the sizeof operator be used to tell the size of an array passed to a function?


View Answer
How to remove duplicate elements from an array

View Answer
How can you sort the elements of the array in descending order?
View Answer
Array is an lvalue or not?
View Answer
Can the size of an array be declared at runtime?
View Answer
Can the sizeof operator be used to tell the size of an array passed to a function?
View Answer
Is it better to use a pointer to navigate an array of values,or is it better to use a subscripted array name?
View Answer
When does the compiler not implicitly generate the address of the first element of an array?
View Answer

Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

View All Arrays in C Interview Questions & Answers - Exam Mode / Learning Mode



User Options
India News Network

Latest 20 Questions
Payment of time- barred debt is: (a) Valid (b) Void (c) Illegal (d) Voidable
Consideration is defined in the Indian Contract Act,1872 in: (a) Section 2(f) (b) Section 2(e) (c) Section 2(g) (d) Section 2(d)
Which of the following is not an exception to the rule, "No consideration, No contract": (a) Natural love and affection (b) Compensation for involuntary services (c) Completed gift (d) Agency
Consideration must move at the desire of: (a) The promisor (b) The promisee (c) The promisor or any other party (d) Both the promisor and the promisee
An offer which is open for acceptance over a period of time is: (a) Cross Offer (b) Counter Offer (c) Standing Offer (d) Implied Offer
Specific offer can be communicated to__________ (a) All the parties of contract (b) General public in universe (c) Specific person (d) None of the above
_________ amounts to rejection of the original offer. (a) Cross offer (b) Special offer (c) Standing offer (d) Counter offer
A advertises to sell his old car by advertising in a newspaper. This offer is caleed: (a) General Offer (b) Special Offer (c) Continuing Offer (d) None of the above
In case a counter offer is made, the original offer stands: (a) Rejected (b) Accepted automatically (c) Accepted subject to certain modifications and variations (d) None of the above
In case of unenforceable contract having some technical defect, parties (a) Can sue upon it (b) Cannot sue upon it (c) Should consider it to be illegal (d) None of the above
If entire specified goods is perished before entering into contract of sale, the contract is (a) Valid (b) Void (c) Voidable (d) Cancelled
______________ contracts are also caled contracts with executed consideration. (a) Unilateral (b) Completed (c) Bilateral (d) Executory
A offers B to supply books @ Rs 100 each but B accepts the same with condition of 10% discount. This is a case of (a) Counter Offer (b) Cross Offer (c) Specific Offer (d) General Offer
_____________ is a game of chance. (a) Conditional Contract (b) Contingent Contract (c) Wagering Contract (d) Quasi Contract
There is no binding contract in case of _______ as one's offer cannot be constructed as acceptance (a) Cross Offer (b) Standing Offer (c) Counter Offer (d) Special Offer
An offer is made with an intention to have negotiation from other party. This type of offer is: (a) Invitation to offer (b) Valid offer (c) Voidable (d) None of the above
When an offer is made to the world at large, it is ____________ offer. (a) Counter (b) Special (c) General (d) None of the above
Implied contract even if not in writing or express words is perfectly _______________ if all the conditions are satisfied:- (a) Void (b) Voidable (c) Valid (d) Illegal
A specific offer can be accepted by ___________. (a) Any person (b) Any friend to offeror (c) The person to whom it is made (d) Any friend of offeree
An agreement toput a fire on a person's car is a ______: (a) Legal (b) Voidable (c) Valid (d) Illegal



Fresher Jobs | Experienced Jobs | Government Jobs | Walkin Jobs | Company Profiles | Interview Questions | Placement Papers | Companies In India | Consultants In India | Colleges In India | Exams In India | Latest Results | Notifications In India | Call Centers In India | Training Institutes In India | Job Communities In India | Courses In India | Jobs by Keyskills | Jobs by Functional Areas

Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes

Gate Articles | Gate Books | Gate Colleges | Gate Downloads | Gate Faqs | Gate Jobs | Gate News | Gate Sample Papers | Gate Training Institutes

MBA Articles | MBA Books | MBA Case Studies | MBA Business Schools | MBA Current Affairs | MBA Downloads | MBA Events | MBA Notifications | MBA FAQs | MBA Jobs
MBA Job Consultants | MBA News | MBA Results | MBA Courses | MBA Sample Papers | MBA Interview Questions | MBA Training Institutes

GRE Articles | GRE Books | GRE Colleges | GRE Downloads | GRE Events | GRE FAQs | GRE News | GRE Training Institutes | GRE Sample Papers

IAS Articles | IAS Books | IAS Current Affairs | IAS Downloads | IAS Events | IAS FAQs | IAS News | IAS Notifications | IAS UPSC Jobs | IAS Previous Question Papers
IAS Results | IAS Sample Papers | IAS Interview Questions | IAS Training Institutes | IAS Toppers Interview

SAP Articles | SAP Books | SAP Certifications | SAP Companies | SAP Study Materials | SAP Events | SAP FAQs | SAP Jobs | SAP Job Consultants
SAP Links | SAP News | SAP Sample Papers | SAP Interview Questions | SAP Training Institutes |




Copyright ©2003-2025 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions