Ellipses (...) used to emulate indentation... swap(int *a, int *b) {
... int temp;
... temp = *a;
... *a = *b;
... *b = temp;
}
You have to pass the address of the variables.void swap (int *pa, int *pb){...}
void swap(int& a, int& b ) { a^=b^=a^=b; }
To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;
substracion of any two number program in c
program to find maximum of two numbers using pointers
The required c program is given below /*Swapping(interchange) the two entered numbers*/ #include<stdio.h> main() { /*Without using third variable*/ int a,b,t; printf("Enter a:"); scanf("%d",&a); printf("Enter b:"); scanf("%d",&b); a=a+b; b=a-b; a=a-b; printf("\n After swapping without using third variable"); printf("\na=%d\nb=%d",a,b); }
// Note: ^ is the XOR operator a = a ^ b b = b ^ a a = a ^ b
a=a^b; b=a^b; a=a^b;
//lets assume a = 10; b = 20; a = a^b; b = a^b; a = a^b;
void swap(int &x, int &y) { x ^= y ^= x; } - or - void swap(int &x, int &y) { int t = x; x = y; y = t; }
// return the larger of any 2 numbers int larger (int a, int b) { return a>b?a:b; } // return the largest of any 3 numbers int largest (int a, int b, int c) { return larger (larger (a, b), c)); } // return the middle value of any 3 numbers int middle (int a, int b, int c) { if (a>b) a^=b^=a^=b; // swap a and b (b is now the larger of the two) if (b>c) b^=c^=b^=c; // swap b and c (c is now the largest of all three) return larger (a, b); // return the larger of a and b }
#include<iostream.h> #include<conio.h> void main() { int a, b, c; clrscr(); cout<<"enter the two numbers"; cin>>a; cin>b; c=a+b; cout<<"Addition of two numbers="<<c; getch(); }