write a program to input two Integers and interchange their values without using a third variable
Answers
Answer:
C Program to swap two numbers without third variable
#include<stdio.h>
int main()
{
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a+b;//a=30 (10+20)
b=a-b;//b=10 (30-20)
a=a-b;//a=20 (30-10)
Program:-
Without using a third variable.
import java.util.*;
public class Brainly
{
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b;
System.out.println("Enter two integers: ");
a=sc.nextInt();
b=sc.nextInt();
System.out.println("Before interechange: a="+a+",b="+b);
a=a+b;
b=a-b;
a=a-b;
System.out.println("After interechange: a="+a+",b="+b);
}
}
With using third variable.
import java.util.*;
public class Brainly
{
static void main()
{
Scanner sc=new Scanner(System.in);
int a,b,c;
System.out.println("Enter two integers: ");
a=sc.nextInt();
b=sc.nextInt();
System.out.println("Before interechange: a="+a+",b="+b);
c=a;
a=b;
b=c;
System.out.println("After interechange: a="+a+",b="+b);
}
}