C Program to Read Two Integers and Swap their Values

C Programming Computer Science

In this C program we will read two integers and swap their values. Below is the code to do so.

  • Source Code

 * C program to read two integers A and B and to swap their values.
 * Use a user-defined function for swapping. Output the values of A
 * and B before and after swapping.
#include <stdio.h>
void swap(float *ptr1, float  *ptr2);
void main()
    float a, b;
    printf("Enter the values of A and B \n");
    scanf("%f %f", &a, &b);
    printf("Before Swapping:A = %5.2ftB = %5.2f\n", a, b);
    swap(&a, &b);
    printf("After Swapping:A  = %5.2ftB = %5.2f\n", a, b);
/*  Function swap - to interchanges the value of two items */
void swap(float *ptr1, float *ptr2)
    float temp;
    temp = *ptr1;
    *ptr1 = *ptr2;
    *ptr2 = temp;


  • Runtime
Enter the values of A and B
5 9
Before Swapping:A =  5.00    B =  9.00
After Swapping:A  =  9.00    B =  5.00


Also Read: 

C Program to Find the Number of Integers Divisible by 3

C Program to Check if a given Integer is Positive or Negative

C Program to Calculate the Sum of Odd and Even Numbers

C Program to Check if a given Integer is Odd or Even

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.