Wednesday, April 30, 2025

C Program - Swap Two Numbers using a Temporary Variable

This C program swaps the values of two variables first and second using a temporary variable temp_var.

#include <stdio.h>

int main()
{

    int first, second, temp_var;

    printf("First Number: ");
    scanf("%d", &first);

    printf("Second Number: ");
    scanf("%d", &second);

    printf("----------------------------- \n");

    printf("Initial state: %d, %d \n", first, second);

    temp_var = first; // now first is "free" container

    first = second; // first is final, second is "free" now

    second = temp_var; // second is final, number swaped now

    printf("Final state:   %d, %d \n", first, second);

    return 0;
}

Result: 

First Number: 10
Second Number: 20
-----------------------------
Initial state: 10, 20
Final state:   20, 10

Process returned 0 (0x0)   execution time : 5.304 s
Press any key to continue.

Here's a line-by-line explanation of the program: 

#include <stdio.h>

This line is a preprocessor directive that includes the standard input/output library in the program. This library contains functions for reading input and writing output. 

int main()
{

This is the starting point of the program. The main() function is the entry point of the program where the execution begins. The int before main() indicates that the function returns an integer value. 

    int first, second, temp_var;

This line declares three integer variables named first, second, and temp_var

    printf("First Number: ");
    scanf("%d", &first);

    printf("Second Number: ");
    scanf("%d", &second);

These lines prompt the user to enter the values of first and second variables respectively using the printf() function to display a message and the scanf() function to read the user input. 

    printf("----------------------------- \n");

    printf("Initial state: %d, %d \n", first, second);

These lines print out a separator line and display the initial state of the first and second variables. 

    temp_var = first; // now first is "free" container

    first = second; // first is final, second is "free" now

    second = temp_var; // second is final, number swaped now

These three lines perform the swapping operation. The current value of first is stored in the temp_var variable. The value of second is then assigned to first, and the value of temp_var is assigned to second. As a result, the values of first and second are swapped. 

    printf("Final state:   %d, %d \n", first, second);

This line prints out the final state of the first and second variables after the swapping operation. 

    return 0;
}

This line indicates the end of the main() function and returns an integer value of 0 to the operating system, indicating that the program has executed successfully.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...