C Program to Read Two Integers M and N & Swap their Values

In this C programming tutorial,we will learn how to swap two integers with the help of C programming language.

Variable Used in this program: 

m : An Integer defined in this program.
n : An Integer defined in this program.

Functions Used in this program:
swaptwointeger : To interchanges the contents of two integers M & N.

C Program to swap two integers M & N


#include <stdio.h>
void swaptwointegers(float *ptr1, float *ptr2);
void main()
{
 float m, n;
 printf("Enter the values of M and N \n");
 scanf("%d %d", &m, &n);
 printf("Before Swapping the integers:M = %d N = %d\n", m, n);
 swaptwointegers(&m, &n);
 printf("After Swapping the integers:M = %d N = d\n", m, n);
}
void swaptwointegers(float *ptr1, float *ptr2)
{
 float temp;
 temp = *ptr1;
 *ptr1 = *ptr2;
 *ptr2 = temp;
}

Output of the program:

Enter the values of M and N 
9 2
Before Swapping the integers M = 9 N = 2
After Swapping the integers M = 2 N = 9

In the above C program, we learnt how to swap 2 Integers with a function named swaptwointegers.

Leave a Reply

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