Wednesday - April 24, 2024
#C, C++ and C# #Algorithm #Blog #C Program

Swap Program in C and Java

Write a program to accept two numbers & swap them.

Swap Program in C

The purpose of this C program is to show how to Swap Program the values of two numbers that the user has supplied. The code is explained in detail below:

Swap Program in C 2024
Swap Program in C and Java 2024

Add the required header files:

The standard input/output library, which offers functions for reading and writing data, is included on this line using the notation #include stdio.h>.
This line includes the “conio” library, a non-standard C library that is frequently used in older DOS-based systems to carry out console input and output operations. #include “conio.h” This library is not accessible on contemporary systems, hence substitutes must be used.

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

void main()
{
         int a, b, temp;
         printf("Enter two numbers : ");

         scanf("%d%d", &a, &b);
         printf("nOriginal values are a = %d b = %d", a, b);

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

          printf("nAfter exchange a = %d b = %d", a, b);
          getch();
}
Output:
Enter two numbers: 10 20
Original values are a = 10 b = 20
After exchange a = 20 b = 10

The program’s primary function is void main(). The program’s entry point in C is the main function, which has a return type of void, indicating that it doesn’t return any values. Establish variables:

Three integer variables are declared in this line: a, b, and temp.

These variables will be employed to execute the swap operation and store user input.

To ask the user for input, print the following message:

In order to request the user to enter two integers, the line printf(“Enter two numbers: “); shows the message “Enter two numbers: “. User input is read:

scanf(“%d%d”, &a, &b);: This line uses scanf to read two integer values from the user and store them in variables a and b. “%d%d” is the format specifier used to read two integers separated by whitespace.

Show the initial values:

printf(“nOriginal values are a =%d b =%d”, “a, b”);: This line displays the user-inputted values for a and b as they originally appeared.
Using a temporary variable, switch the values of variables a and b:

The temporary variable temp is set to the value of an in-the-line temp = a;.
a = b;: This line essentially swaps the values of a and b by assigning the value of b to a.
This line completes the swap by assigning the value from a that was previously saved in temp to b.
Show the values that were switched:

This line publishes the swapped values of a and b, which are now in reversed order, as printf(“nAfter exchange a =%d b =%d”, a, b);.

Getch();: This line holds the program in place until a key is pressed. This function might not be included in contemporary systems, and it’s frequently left out.

As seen in the output, the program will display the original values, swap them, and then display the swapped values when you run it and enter “10 20,” as in your example.

Swap Program in Java

Similar to your C program, this Java software accepts two integers from the user, swaps their values, and then displays the new values. Note that in Java, user input is read using Scanner, and output is sent to the console using System.out.println.

import java.util.Scanner;

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

        System.out.print("Enter two numbers: ");
        a = scanner.nextInt();
        b = scanner.nextInt();
        System.out.println("Original values are a = " + a + " b = " + b);

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

        System.out.println("After exchange a = " + a + " b = " + b);
    }
}
Swap Program in C and Java

Swap Program in C and Java

.