[java] program untuk menghitung x^y

METNUM

|Tugas METNUM: Halaman 130 no.6

Buatlah program yang meminta 2 buah bilangan x dan y, lalu menampilkan hasil xy.

Jawab

//irwananwar.wordpress.com
import java.util.Scanner;
public class loop6_pangkat {
	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		System.out.printf("====X^y====\n");
		System.out.printf("Input X : ");
		int x=s.nextInt();
		System.out.printf("Input y : ");
		int y=s.nextInt();
		int hasil=x;
		for(int i=1;i<y;i++)
			hasil=hasil*x;
		System.out.printf("%d^%d = %d",x,y,hasil);
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s