Jumat, 15 April 2011

Exchange value of 2 variables (Mempertukarkan nilai dari 2 peubah)

ALGORITHM  Exchange value of 2 variables

Exchange Program
{exchange value of A and B, the value of A and B read first.}

Declaration:
A, B, temp: integer

Algorithm:
read (A, B) {read value A and B advance}
exchange process {}

temp ← A {store the value of A in place temporary, temp}
A ← B {A can now be filled with a value of B}
B ← {temp fill B with an A which was originally stored in the temp}
write (A, B) {print value of A and B after the exchange}

PROGRAM:
# include "stdio.h"
main ()
{
/ * Declaration * /
int A, B, temp;

/ * Algorithm * /
/ * read value A and value B * /

printf ("A =?"); scanf ("% d ", & A);
printf ("B =?"); scanf ("% d ", & B);

/ * The process of exchange * /
temp = A;
A = B;
B = temp;

/ * write the value A and value B after the exchange * /
printf (A =% d \ n ", A);
printf (B =% d \ n ", B);

Tidak ada komentar:

Posting Komentar