- Buatlah program untuk menerima input bilangan bulat positif sembarang (jumlah digit tidak ditentukan) dan menampilkan kembali bilangan tersebut dengan urutan digit pertamanya diganti dengan 9. Contoh: input = 43521, output = 93521
import java.util.Scanner;
/**
* @fitrianadz
*/
public class Exercises {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int inputValue;
System.out.print("input n = ");
inputValue = input.nextInt();
int temp = 0; // to copy inputValue
int counter = 0; // to count digits
int result = 0;
int multiplier = 1;
/*
* condition (inputValue > 9) indicates that we don't need to process anymore
* when inputValue contains only 1 digit
*/
while (inputValue > 9) {
temp = inputValue;
inputValue = inputValue / 10;
int lastDigit = temp % 10;
if (counter == 0) {
result = lastDigit;
} else {
result = result + (lastDigit * multiplier);
}
multiplier = multiplier * 10;
counter++;
}
result = result + (9 * multiplier);
System.out.println(result);
}
}
2. Buatlah program untuk menerima input bilangan bulat positif sembarang (jumlah tidak ditentukan) dan menampilkan kembali bilangan tesebut dengan urutan digit pertamanya diganti ditukar dengan digit terakhir.
contoh:
input = 43521
output = 13524
import java.util.Scanner;
/**
* @fitrianadz
*/
public class Exercises {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int inputValue;
System.out.print("input n = ");
inputValue = input.nextInt();
int temp = 0; // to copy inputValue
int counter = 0; // to count digits
int prefix = 0;
int lastDigit = 0;
int multiplier = 0;
int result = 0;
while (inputValue != 0) {
temp = inputValue;
inputValue = inputValue / 10;
lastDigit = temp % 10;
if (inputValue != 0) {
if (counter == 0) {
multiplier = 10;
prefix = lastDigit;
} else if (counter > 0 && temp > 9) {
result = result + (lastDigit * multiplier);
multiplier = multiplier * 10;
}
counter++;
}
}
result = (prefix * multiplier) + result + lastDigit;
System.out.println(result);
}
}
3. Buatlah program untuk menerima input bilangan bulat positif n dan selanjutnya mengeluarkan output pola segitiga seperti contoh berikut.
input n = 4
output =
1
23
456
78910
import java.util.Scanner;
/**
* @author user
*/
public class Exercises {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int inputValue;
System.out.print("value = ");
inputValue = input.nextInt();
int counter = 0;
for (int i = 1; i <= inputValue; i++) {
for (int j = 1; j <= i; j++) {
counter++;
System.out.print(counter + " ");
}
System.out.println();
}
}
}
4. Hitunglah deret berikut ini untuk melakukan pendekatan terhadap nilai Pi
Pi = 4(1-1/3+1/5-1/7+1/9-1/11.........-1/(2i-1)+1/2i+1)
Tampilkan nilai Pi pada layar untuk nilai i yang diinput dari keyboard.
import java.util.Scanner;
/**
* @author user
*/
public class Exercises {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n;
System.out.print("input n = ");
n = input.nextInt();
double totalphi = 1.0;
int sum;
int minus;
System.out.print("phi =4(1");
for (int i = 2; i <= n; i++) {
minus = 2 * i - 1;
if (i % 2 == 1) {
totalphi = totalphi + (1.0 / minus);
System.out.print("+1/" + minus);
}
if (i % 2 == 0) {
totalphi = totalphi - (1.0 / minus);
System.out.print("-1/" + minus);
}
}
sum = 2 * n + 1;
if (n % 2 == 0) {
System.out.print("+1/" + sum + ") = " + 4.0 * (totalphi + (1.0 / sum)));
}
if (n % 2 == 1) {
System.out.print("-1/" + sum + ") = " + 4.0 * (totalphi - (1.0 / sum)));
}
System.out.println();
}
}






