Given the values of the variables x, y and z, write a program to rotate their values such that x has the value of y, y has the value of z, and z has the value of x

//E Balagurusamy, Programming in ANSI C,4E
//Exercise 3.1, p81

//Note:
//. comments with //scaff indicate scaffolding code to help during development of code
//. can be safely commented out/removed (in production code)
//HOW?
//We'll take one temp variable and store any specific variable in it
//i.e that variable can then be written over.
//Thus, cycling over till we reach the end, where temp replaces the next one.
#include <stdio.h>
#include <conio.h>
int main(void)
{
    char x='x', y='y', z='z';
    char temp='0';

    //let's choose z
    temp=z; //thus z can be safely written over
    z=x; //now x can be safely written over
    x=y; //now y
    y=temp; //and temp=z, therefore z is being placed in y, as required
    //output
    printf("\n\n===OUTPUT===");
    printf("\nx = %c",x);
    printf("\ny = %c",y);
    printf("\nz = %c",z);
    getch();
    return 0;
}

Comments