This blog is under construction

Saturday, 6 July 2013

C program to swap two numbers using call by value

Write a C program to swap two numbers without using third variable.

  #include <stdio.h>

  /* swap two numbes without using third variable  */
  void swapTwoNumbers(int num1, int num2) {
        printf("\nValues before swapping:\n");
        printf("num1: %d  num2: %d\n", num1, num2);
        num1 = num1 * num2;
        num2 = num1 / num2;
        num1 = num1 / num2;
        printf("\nValues after swapping:\n");
        printf("num1: %d  num2: %d\n", num1, num2);

  int main() {
        int val1, val2;

        /* get the inputs from the user */
        printf("Enter your first input:");
        scanf("%d", &val1);
        printf("Enter your second input:");
        scanf("%d", &val2);

        /* swap two numbers using functions */
        swapTwoNumbers(val1, val2);
        return 0;

  jp@jp-VirtualBox:~/$ ./a.out 
  Enter your first input:100
  Enter your second input:200

  Values before swapping:
  num1: 100  num2: 200

  Values after swapping:
  num1: 200  num2: 100

1 comment:

  1. this link might be help you C program to swap two numbers