Custom Search

Friday, 13 January 2012

WAP, Write a C or C++ Program to swap the value of two variables using temporary variable with output using temporary variable

WAP, Write a C or C++ Program to swap the value of two variables using temporary variable with output using temporary variable 


Program
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr()
{
int a,b, temp;
printf("Enter the value of a:");
scanf("%d", &a);
printf("Enter the value of b:");
scanf("%d", &b);
temp=a;
a=b;
b=temp;
printf("\n Value of a after swap is :%d, a");
printf("\n Value of b after swap is: %d, b");
getch();

Changes for C++
If you want to write the same program in c++ apply the following changes to the program.
1.) change the header file from "#include<stdio.h>" to "#include<iostream>.h" 
2.)  for input & output use "cout<<" & "cin>>" instead of "printf" & "scanf".
3.) don't use "% "symbol and "&" sign in "cin>>" statement.


Click here to check the output

No comments:

Post a Comment

Laptops