Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Write a program that swaps the values of two variables.

Write a program that swaps the values of two variables using temporary variable.

Program in C

C
#include <stdio.h>

int main() {
    int a, b, temp;

    printf("Enter the value of a: ");
    scanf("%d", &a);

    printf("Enter the value of b: ");
    scanf("%d", &b);

    printf("\nBefore swapping:\n");
    printf("a = %d\n", a);
    printf("b = %d\n", b);

    temp = a;
    a = b;
    b = temp;

    printf("\nAfter swapping:\n");
    printf("a = %d\n", a);
    printf("b = %d\n", b);

    return 0;
}

Explanation:

  • When you run this program, it will prompt the user to enter the values of a and b.
  • After the user inputs the values, the program will swap their values using a temporary variable temp.
  • It will then print the values of a and b before and after swapping.
  • In this program, we declare three variables a, b, and temp of type int.
  • We use printf() to display the prompt messages and scanf() to read the user input.
  • The values of a and b are swapped by assigning the value of a to temp, then assigning the value of b to a, and finally assigning the value of temp to b.
  • We use printf() to display the values of a and b before and after swapping.

Output:

C
Enter the value of a: 4
Enter the value of b: 2

Before swapping:
a = 4
b = 2

After swapping:
a = 2
b = 4

Program in Java

Java
import java.util.Scanner;

public class SwapNumbers {
    public static void main(String[] args) {
        int a, b, temp;
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the value of a: ");
        a = scanner.nextInt();

        System.out.print("Enter the value of b: ");
        b = scanner.nextInt();

        System.out.println("\nBefore swapping:");
        System.out.println("a = " + a);
        System.out.println("b = " + b);

        temp = a;
        a = b;
        b = temp;

        System.out.println("\nAfter swapping:");
        System.out.println("a = " + a);
        System.out.println("b = " + b);
    }
}

Explanation:

  1. import java.util.Scanner;: This line imports the Scanner class from the java.util package. It is used for reading user input.
  2. public class SwapNumbers: This line declares a public class named SwapNumbers.
  3. public static void main(String[] args): This is the main method that serves as the entry point of the program.
  4. int a, b, temp;: Declares three integer variables a, b, and temp to store the input values and perform the swap operation.
  5. Scanner scanner = new Scanner(System.in);: Creates a new Scanner object named scanner to read user input from the console.
  6. a = scanner.nextInt(); and b = scanner.nextInt();: These lines prompt the user to enter values for a and b using System.out.print and read the integer input using scanner.nextInt().
  7. System.out.println(“\nBefore swapping:”);: Prints a message indicating the values before swapping.
  8. System.out.println(“a = ” + a); and System.out.println(“b = ” + b);: Prints the values of a and b before swapping.
  9. temp = a;, a = b;, and b = temp;: These lines perform the swap operation by storing the value of a in temp, assigning the value of b to a, and then assigning the value of temp (initial value of a) to b.
  10. System.out.println(“\nAfter swapping:”);: Prints a message indicating the values after swapping.
  11. System.out.println(“a = ” + a); and System.out.println(“b = ” + b);: Prints the values of a and b after swapping.
Output
Enter the value of a: 4
Enter the value of b: 2

Before swapping:
a = 4
b = 2

After swapping:
a = 2
b = 4