Soal dan solusi Algoritma: Menukar Isi Variabel Tanpa Menggunakan Variabel Bantuan

Posted on

Tuliskan program untuk menukar isi dua buah variabel tanpa menggunakan variabel bantuan.

Solusi Python:

# Program tukar isi variabel
a, b = input("Masukkan a, b:")

a = a + b
b = a - b
a = a - b
print "Nilai a: %d b: %d" % (a,b)

Tampilan:

Solusi C:

// program menukar isi variabel tanpa variabel bantuan

#include <stdio.h>
int main() {

int a, b;

printf("Masukkan a b: ");
scanf("%d %d", &a, &b);
a = a + b;
b = a - b;
a = a - b;

printf("Nilai a, b: %d, %d \n", a, b);

return 0;
}

Tampilan:

Gravatar Image
Program Studi Teknik Informatika, Universitas Ma Chung, Malang - www.machung.ac.id

Leave a Reply

Your email address will not be published. Required fields are marked *