Monday, August 4, 2014

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

This C Program reads two integers & swap their values.
Here is source code of the C program to read two integers & swap their values. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
  1. /*
  2.  * C program to read two integers M and N and to swap their values.
  3.  * Use a user-defined function for swapping. Output the values of M
  4.  * and N before and after swapping.
  5.  */
  6. #include <stdio.h>
  7. void swap(float *ptr1, float  *ptr2);
  8.  
  9. void main()
  10. {
  11.     float m, n;
  12.  
  13.     printf("Enter the values of M and N \n");
  14.     scanf("%f %f", &m, &n);
  15.     printf("Before Swapping:M = %5.2ftN = %5.2f\n", m, n);
  16.     swap(&m, &n);
  17.     printf("After Swapping:M  = %5.2ftN = %5.2f\n", m, n);
  18. }
  19. /*  Function swap - to interchanges the contents of two items */
  20. void swap(float *ptr1, float *ptr2)
  21. {
  22.     float temp;
  23.  
  24.     temp = *ptr1;
  25.     *ptr1 = *ptr2;
  26.     *ptr2 = temp;
  27. }

$ cc pgm36.c
$ a.out
Enter the values of M and N
2 3
Before Swapping:M =  2.00    N =  3.00
After Swapping:M  =  3.00    N =  2.00

No comments:

Post a Comment