This blog is under construction

Monday 10 June 2013

C program to swap two numbers

Step 1: Start
Step 2: Declare variables a, b and temp
Step 3: Read variables a and b
Step 4: temp<-a
Step 5: a<-b
Step 6: b<-temp
Step 7: Print the values of a and b
Step 8: Stop


 
  /* C program to swap two numbers */
  #include 
  int main() {
        int a, b, temp;

        printf("Enter the value for a:");
        /* get the value for 'a' from the user */
        scanf("%d", &a);

        printf("Enter the value for b:");
        /* get the value for 'b' from the user */
        scanf("%d", &b);

        /* store the value of a  in temp */
        temp = a;
        /* store the value of b in a */
        a = b;
        /* store the value in temp to b */
        b = temp;

        /* print the results */
        printf("Result after swapping:\n");
        printf("Value of a: %d\n", a);
        printf("Value of b: %d\n", b);
        return 0;
  }


  Output:
  Enter the value for a: 10
  Enter the value for b: 20
  Value of a: 20
  Value of b: 10



1 comment: