Saturday, December 8, 2012

Java, finding A,B,& C values





a.       Given the following pseudocode, what are the final values of variables A, B, and C? Write Java code in Java for the same problem.

Start
A = 2
B = 4
C = 10
while C > 6
perform ChangeBandC
endwhile
if A = 2 then
perform ChangeA
endif
if C = 10 then
perform ChangeA
else
perform ChangeBandC
endif
print A, B, C
Stop

ChangeBandC
B = B + 1
C = C - 1
Return

ChangeA
A = A + 1
B = B - 1
Return


 JAVA code:
 public class ABC {

    static int a = 2, b = 4, c = 10;

    public static void main(String args[]) {
        while (c>6) {
            changeBandC();
        }
        if (a == 2)
        {
            changeA();
        }
        if (c==10)
        {
            changeA();
        }
        else
        {
            changeBandC();
        }
        System.out.println("A no is  " +a + ";  B no is " + b+ " ; and C no is "+c);
    }

    public static void changeA() {
        a = a + 1;
        b = b - 1;

    }

    public static void changeBandC() {
        b = b + 1;
        c = c - 1;
    }
}



Result:






No comments:

Post a Comment