C Program to swap two numbers without third variable || C Programs

Code -

 int main()    
int a=10, b=20;      
printf("Before swap a=%d b=%d",a,b);      
a=a+b;//a=30 (10+20)   
b=a-b;//b=10 (30-20)   
a=a-b;//a=20 (30-10)   
printf("\nAfter swap a=%d b=%d",a,b);    
return 0;  

Output -

Before swap a=10 b=20
After swap a=20 b=10

Related Posts

See All

Program of Random Number in C || C Programs

Code - #include <stdio.h> #include <conio.h> #include <stdlib.h> void main() { // use rand() function to generate the number printf (" The random number is: %d", rand()); pri

Getchar() function in C || C Programs

Code - #include <stdio.h> #include <conio.h> void main() { char c; printf ("\n Enter a character \n"); c = getchar(); // get a single character printf(" You have passed ")

  • 1024px-Telegram_logo.svg
  • download
  • YouTube