answersLogoWhite

0


Best Answer

Using a temporary variable:

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,temp;

clrscr():

printf("Enter the first number a\n");

scanf("%d",&a);

printf("Enter the second number b\n");

scanf("%d",&b);

printf("Before swapping a=%d b=%d",a,b);

temp=a;

a=b;

b=temp;

printf("The swapped values are: a=%d b=%d",a,b);

getch();

}

Without using a temporary variable:

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b;

clrscr();

printf("Enter the first number a\n");

scanf("%d",&a);

printf("Enter the second number b\n");

scanf("%d",&b);

printf("Before swapping a=%d b=%d",a,b);

a=a+b;

b=a-b;

a=a-b;

printf("The swapped values are: a=%d b=%d\n");

getch();

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

16y ago

int num1=15;

int num2=25;

int temp;

temp=num1;

num1=num2;

num2=temp;

int num1=15;

int num2=25;

num1=num1+num2;

num2=num1-num2;

num1=num1-num2;

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

#include; main() {int a,b; printf("enter the value of a"); scanf(%d%,&a); printf("enter the vaule of b"); scanf(%d%,&a); a=a+b b=a-b a=a-b printf("%d%%d%",a,b);}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

A ^= B;

B ^= A;

A ^= B;

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C program for swap two numbers without using temporary variables?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How can you accept sum and print numbers without creating variables?

It is very easy. The program begins here..... /*Program to sum and print numbers without creating variables*/ #include&lt;stdio.h&gt; main() { clrscr(); printf("%d+%d=%d",5,2,5+2); getch(); } /*Program ends here*/ Now just by changing the numbers in the "printf" statement we can add, subtract, multiply and divide the numbers without using variables. Hence the problem is solved..........


What is the Algorithm for exchanging values of two variables?

There are three primary algorithms to exchange the values of two variables. Exchange with Temporary Variable temp = a; a = b; b = temp; Exchange Without Temporary Variable Using Exclusive Or a = a ^ b; b = b ^ a; a = a ^ b; Exchange Without Temporary Variable Using Arithmetic a = a + b; b = b - a; a = a - b;


How do you write a c program to swap the values of two variables in unix?

#include&lt;stdio.h&gt; void main() { int a=2,b=4; printf("Program for swapping two numbers "); printf("Numbers before swapping"); printf("a=%d and b=%d",a,b); a=((a+b)-(b=a)); printf("Numbers after swapping"); printf("a=%d and b=%d",a,b); getch(); }


How do you write a program in Perl to swap two variables without using the third one?

Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)


What is program in c to swap entered two numbers without using third variable?

The required c program is given below /*Swapping(interchange) the two entered numbers*/ #include&lt;stdio.h&gt; main() { /*Without using third variable*/ int a,b,t; printf("Enter a:"); scanf("%d",&amp;a); printf("Enter b:"); scanf("%d",&amp;b); a=a+b; b=a-b; a=a-b; printf("\n After swapping without using third variable"); printf("\na=%d\nb=%d",a,b); }

Related questions

How can you accept sum and print numbers without creating variables?

It is very easy. The program begins here..... /*Program to sum and print numbers without creating variables*/ #include&lt;stdio.h&gt; main() { clrscr(); printf("%d+%d=%d",5,2,5+2); getch(); } /*Program ends here*/ Now just by changing the numbers in the "printf" statement we can add, subtract, multiply and divide the numbers without using variables. Hence the problem is solved..........


What is the Algorithm for exchanging values of two variables?

There are three primary algorithms to exchange the values of two variables. Exchange with Temporary Variable temp = a; a = b; b = temp; Exchange Without Temporary Variable Using Exclusive Or a = a ^ b; b = b ^ a; a = a ^ b; Exchange Without Temporary Variable Using Arithmetic a = a + b; b = b - a; a = a - b;


How do you write a program in C plus plus plus plus How do you write a program in C to swap two variables without using the third oneo swap two variables without using the third one?

To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;


How do you write a c program to swap the values of two variables in unix?

#include&lt;stdio.h&gt; void main() { int a=2,b=4; printf("Program for swapping two numbers "); printf("Numbers before swapping"); printf("a=%d and b=%d",a,b); a=((a+b)-(b=a)); printf("Numbers after swapping"); printf("a=%d and b=%d",a,b); getch(); }


What is a mathematical phrase that contains operations, numbers, and/or variables without equal signs.?

Algebraic expression


How do you write a program in Perl to swap two variables without using the third one?

Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)


What does terms of an expression mean?

An expression is a collection of numbers and variables, along with mathematical operations, but without an equality (or inequality) symbol.


What is the definition of a computer program?

An organized list of instructions that, when executed, causes the computer to behave in a predetermined manner. Without programs, computers are useless.A program is like a recipe. It contains a list of ingredients (called variables) and a list of directions (called statements) that tell the computer what to do with the variables. The variables can represent numeric data, text, or graphical images.


How do you write a program in C to swap two variables without using the third one using XOR?

a=a^b; b=a^b; a=a^b;


What is program in c to swap entered two numbers without using third variable?

The required c program is given below /*Swapping(interchange) the two entered numbers*/ #include&lt;stdio.h&gt; main() { /*Without using third variable*/ int a,b,t; printf("Enter a:"); scanf("%d",&amp;a); printf("Enter b:"); scanf("%d",&amp;b); a=a+b; b=a-b; a=a-b; printf("\n After swapping without using third variable"); printf("\na=%d\nb=%d",a,b); }


Does a mathematical expression have to have variables?

Yes. Expressions cannot be expressed without variables. There are numerical expressions for ex. 2 + 3 is an expression without variables.


What is the difference between Algebra and calculus?

Algebra must be learned before calculus. Concepts that are learned in algebra are used in calculus, to the extent that a student cannot succeed in calculus unless he knows algebra so well that he does it without thinking.Algebra is the study of constants and variables; that is, it is the study of numbers without knowing specifically what those numbers are.Calculus is the study of rates of change, and is done almost entirely abstractly (without using specific numbers), so it cannot be done without the use of constants and variables (algebra).