upsidedown

oops3_b

Feb 10th, 2012
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include<iostream.h>
  2. void swap(int *,int *);
  3. void swap(float *,float *);
  4. void swap(char *,char *);
  5.  
  6. int main()
  7. {
  8.     int a,b,c;
  9.     float x,y;
  10.     char p,q;
  11.     cout<<"enter a choice\n";
  12.     cout<<"1.To swap two integers\n";
  13.     cout<<"2.To swap of two floats\n";
  14.     cout<<"3.To swap of two characters\n";
  15.     cin>>a;
  16.     switch(a)
  17.     {
  18.     case 1:
  19.         cout<<"enter two inegers:\n";
  20.         cin>>b>>c;
  21.         cout<<"value before swapping b= "<<b<<" c= "<<c<<endl;
  22.         swap(&b,&c);
  23.         cout<<"value after swapping b= "<<b<<" c= "<<c<<endl;
  24.         break;
  25.  
  26.     case 2:
  27.         cout<<"enter two floating point numbers:\n";
  28.         cin>>x>>y;
  29.         cout<<"value before swapping x= "<<x<<" y= "<<y<<endl;
  30.         swap(&x,&y);
  31.         cout<<"value after swapping x= "<<x<<" y= "<<y<<endl;
  32.         break;
  33.    
  34.     case 3:
  35.         cout<<"enter two characters:\n";
  36.         cin>>p>>q;
  37.         cout<<"value before swapping p= "<<p<<" q= "<<q<<endl;
  38.         swap(&p,&q);
  39.         cout<<"value after swapping p= "<<p<<" q= "<<q<<endl;
  40.         break;
  41.  
  42.     default:
  43.         cout<<"invalod operator\n";
  44.         break;
  45.     }
  46.     return 0;
  47. }
  48.  
  49.  
  50. void swap(int *a,int *b)
  51. {
  52.     *a=*a+*b;
  53.     *b=*a-*b;
  54.     *a=*a-*b;
  55. }
  56.  
  57. void swap(float *a,float *b)
  58. {
  59.     *a=*a+*b;
  60.     *b=*a-*b;
  61.     *a=*a-*b;
  62. }
  63.  
  64. void swap(char *a,char *b)
  65. {
  66.     *a=*a+*b;
  67.     *b=*a-*b;
  68.     *a=*a-*b;
  69. }
  70.  OUTPUT:
  71. enter a choice
  72. 1.To swap two integers
  73. 2.To swap of two floats
  74. 3.To swap of two characters
  75. 1
  76. enter two inegers:
  77. 75
  78. 84
  79. value before swapping b= 75 c= 84
  80. value after swapping b= 84 c= 75
  81. Press any key to continue
  82.  
  83. enter a choice
  84. 1.To swap two integers
  85. 2.To swap of two floats
  86. 3.To swap of two characters
  87. 2
  88. enter two floating point numbers:
  89. 42.6
  90. 15.4
  91. value before swapping x= 42.6 y= 15.4
  92. value after swapping x= 15.4 y= 42.6
  93. Press any key to continue
  94.  
  95. enter a choice
  96. 1.To swap two integers
  97. 2.To swap of two floats
  98. 3.To swap of two characters
  99. 3
  100. enter two characters:
  101. f
  102. h
  103. value before swapping p= f q= h
  104. value after swapping p= h q= f
  105. Press any key to continue
Advertisement
Add Comment
Please, Sign In to add comment