Write a program to input values of two variables and swap their values by calling a function
Answers
Answer:
#include<stdio>
using namespce std;
main()
{
int a, b;
cout<<"Enter two numbers\n";
cin>>a>>b;
cout<<"A = "<<a<<" B = "<<b;
swap(a,b);
}
void swap(int y, int z)
{
int s;
s = y;
y = z;
z = s;
cout<<"After Swaping";
cout<<"A = "<<y<<" B = "<<z;
}
Question:-
Write a program to input values of two variables and swap their values by calling a function.
Program:-
import java.util.*;
class Swap
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a: ");
int a=sc.nextInt();
System.out.print("Enter b: ");
int b=sc.nextInt();
swap(a, b);
}
static void swap(int a, int b)
{
System.out.println("Before swapping, ");
System.out.println("a: "+a);
System.out.println("b: "+b);
int c=a;
a=b;
b=c;
System.out.println("After swapping, ");
System.out.println("a: "+a);
System.out.println("b: "+b);
}
}