Follow This Blog For more... 😊

Program to accept two values of variables a & b and Swap their values.| Using C language| very Basic| C Program series 06|

👨‍💻Program to accept two values of variables a & b and  Swap their values.

Code:


    //Program to accept two values of a & b and swap their values.

    #include<stdio.h>
    main()
    {
        printf("Program to accept two values of a & b and swap their values.\n");
       
        int a,b,c;//Using three variables.(This program can be made using only two variables using another logic.)
       
        printf("Enter the value of a:");
        scanf("%d",&a);
       
        printf("Enter the value of b:");
        scanf("%d",&b);
       
        printf("\nBEFORE swap a & b: a=%d, b=%d",a,b);
       
        c=a;//Here, we stroed value of 'a' in 'c'.
        a=b;//Then change value of 'a' by 'b'.
        b=c;//Then change value of 'b' by 'c'(now 'c' is older value of a).
       
        printf("\nAFTER swap a & b: a=%d, b=%d",a,b);
    }


Output:



👉Click for more C Program series Questions.[Questions list]

Comments

Popular Posts