Users Online

· Guests Online: 114

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

Swapping of two numbers without third variable

Swapping of two numbers without third variable

 

 

You can also swap two numbers without using third variable. In that case C program will be as follows:

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.    int a, b;
  6.    
  7.    printf("Input two integers (a & b) to swap\n");
  8.    scanf("%d%d", &a, &b);
  9.    
  10.    a = a + b;
  11.    b = a - b;
  12.    a = a - b;
  13.  
  14.    printf("a = %d\nb = %d\n",a,b);
  15.    return 0;
  16. }

Output of program:
Swap numbers C program output

To understand the logic choose the variables 'a' and 'b' as '7' and '9' respectively and then do what is being done by the program. You can choose any other combination of numbers as well. Sometimes it's an excellent way to understand a program.

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.72 seconds
10,268,534 unique visits