Taking user input for number of pairs and swapping them

Hello,

I am writing a basic program that takes number of pairs as input and user has to enter pairs of numbers. Output will be swapped pairs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include <stdio.h>
int main()
{
  int x, y, p, i;
  printf ("How many pairs?\n");
  scanf ("%d", &p);
  for(i=1; i<=p; i++)
  {
	 printf ("Enter two values:\n");
	 scanf("%d %d",&x,&y);

	 x = x + y;  
	 y = x - y;  
	 x = x - y;
	 


 
  printf("\n");
  printf("After Swapping: x = %d, y = %d\n", x, y);
  }
   
  return 0;
}


The program works as intended but it provides output after every single input. I want it to take all the inputs together and show the output at the end.

Example:

I/p:

3 (number of pairs)

2 3
4 5
6 7

O/p
3 2
5 4
7 6

How do I do this?
Last edited on
closed account (E0p9LyTq)
Not possible without using a third, temporary variable, or other means of storage such as writing to and reading from a disc file.

1
2
3
4
5
6
   printf("Enter two values:\n");
   scanf("%d %d",&x,&y);

   int temp = x;
   x = y;
   y = temp;
Last edited on
Topic archived. No new replies allowed.