Program 8 - CSU1128 - CSE 2026 - Shoolini University

Program to swap the values with temp.

Explanation

The following code is a simple program that swaps the values of two numbers entered by the user. The program starts with the inclusion of the standard input/output library in the code using the line:

#include <stdio.h>

This library provides functions such as scanf and printf which are used in the program. The main function is the starting point of the program where all the logic is defined.

int main()
{

The code then declares 3 integer variables d, m, and j. The printf statement is used to display a prompt asking the user to enter two numbers separated by a space. The scanf statement is used to read these two numbers entered by the user and store them in variables d and m. The next printf statement displays a message indicating the input values of the two numbers.

int d, m, j;
printf("\n\n Program to swap the values with 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);

The code then uses a temporary variable j to swap the values of d and m. The value of d is stored in j, then the value of m is assigned to d and finally the value of j is assigned to m. This effectively swaps the values of d and m. The next printf statement displays the swapped values of d and m.

j = d;
d = m;
m = j;
printf("\n Swapped input values of a and b are %d and %d respectively.\n", d, m);

The return statement with a value of 0 indicates that the program has executed successfully.

    return 0;
                }
                

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 with 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);
    j = d;
    d = m;
    m = j;
    printf("\n Swapped input values of a and b are %d and %d respectively.\n", d, m);
    return 0;
}                    
                

Output

Program to swap the values with temp.

Enter two numbers with a space - (Example: 23 76) - and press enter: 34 65
 Input values of a and b are 34 and 65 respectively.
 Swapped input values of a and b are 65 and 34 respectively.