C++ Practical
Pointer Handling Snippet-12
Swap Integers Using Pointers
Question: Write a function using pointers to swap two integers. *
Answer:
// Question: Write a program using pointers to swap two integers. * #include <iostream.h> #include <conio.h> void swap_using_pointers(int *, int *); void main() { clrscr(); int a,b; cout<<"\n\nEnter first integer : "; cin >> a; cout<<"\n\nEnter second integer : "; cin>>b; swap_using_pointers(&a,&b); cout<<"\n\nNow value of first integer = "<<a; cout<<"\n\nNow value of second integer = "<<b; getch(); } void swap_using_pointers(int *a,int *b) { int temp; temp=*a; *a=*b; *b=temp; }
Program Output:
No comments:
Post a Comment
We love to hear your thoughts about this post!
Note: only a member of this blog may post a comment.