Wednesday, May 16, 2018

C program to swap two numbers.

Program code
#include <stdio.h>
#include <conio.h>
void main()
{
int a=0, b=0, temp=0;
clrscr();
printf(“Please input a number to store in a.\n”);
scanf(“%d”,&a);
printf(“Please input a number to store in b.\n”);
scanf(“%d”,&b);
printf(“Before swapping.\na=%d\nb=%d\n”, a,b);
/*displays the values of a and b before swapping.*/
/* swapping starts */
temp=a;
a=b;
b=temp;
/*swapping ends*/
printf(“After swapping.\na=%d\nb=%d\n”, a,b);
/*displays the values of a and b after swapping.*/
getch();
}

Example of output
Please input a number to store in a.
98
Please input a number to store in b.
34
Before swapping.
a=98
b=34
After swapping.
a=34
b=98

No comments:

Post a Comment

Please write here, your opinion about this C program