Wednesday - November 29, 2023
#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 2023
Swap Program in C and Java 2023

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

.