Program 9 - CSU1128 - CSE 2026 - Shoolini University

Program to swap the values without temp.

Explanation

This code is a C program that swaps the values of two variables without using a temporary variable. The code starts by including the standard input/output library in C, <stdio.h>, which provides functions such as printf and scanf that are used in this code.

The main function of the code is where the program execution starts. It starts by declaring three integer variables, d, m, and j. The program then prints a message to the user asking them to enter two numbers with a space and press enter. The values are then stored in d and m using the scanf function.

The program then prints the input values of d and m to the user, and then it performs the swapping of the values using two different logics. The first logic involves multiplying d by m, then dividing both d and m by m to get the original values of d and m respectively.

The second logic involves adding d and m, then subtracting m from the result to get the original value of d, and finally subtracting d from the result to get the original value of m. The program then prints the swapped values of d and m using both logics.

The code ends with a return statement, which returns the value 0 to the operating system, indicating that the program has executed successfully. The code can be executed on any C compiler, such as GCC or Turbo C, and it will produce the same results.

Code

                    
/*
 * -----------------------------------------------------------
 * Logic Building with Computer Programming (CSU1128)
 * Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
 * 
 * This code is a part of the educational initiative by dmj.one
 * with aim of empowering and inspiring learners in the field of
 * Computer Science and Engineering through the respective courses.
 * 
 * (c) 2022, Divya Mohan for dmj.one. All rights reserved.
 * -----------------------------------------------------------
 */

#include <stdio.h>
int main()
{
    int d, m, j;
    printf("\n\n Program to swap the values without temp. \n\n");
    printf("Enter two numbers with a space - (Example: 23 76) - and press enter: ");
    scanf("%d %d", &d, &m);
    printf("\n Input values of a and b are %d and %d respectively.\n", d, m);
    // Logic 1
    d = d * m;
    m = d / m;
    d = d / m;
    printf("\n Swapped input values of a and b using first method are %d and %d respectively.\n", d, m);
    // Logic 2
    d = d + m;
    m = d - m;
    d = d - m;
    printf("\n Swapped input values of a and b using second method are %d and %d respectively.\n", d, m);

    return 0;
}
                

Output

Program to swap the values without temp.

Enter two numbers with a space - (Example: 23 76) - and press enter: 34 54

Input values of a and b are 34 and 54 respectively.
 Swapped input values of a and b using first method are 54 and 34 respectively.
 Swapped input values of a and b using second method are 34 and 54 respectively.