Practice #1
Write a program that reads an integer between 0 and 1000 and multiplies all the digits in the integer. (0부터 1000까지 중 임의의 수를 입력 받아, 각 자리 숫자를 곱하는 프로그램을 작성하시오.) For example, if an integer is 932, the multiplication of all its digit is 932 = 54.
import java.util.Scanner;
public class Multiplication {
public static void main(String[]args) {
System.out.println("Enter an integer between 0 and 1000: ");
Scanner input = new Scanner(System.in);
int i=input.nextInt(); i
nt p=0;
if(i==1000) {
p=0; }
else if(i>=100&&i<1000) {
int a,b,c,d; a=i/100; b=i-(a100); c=b/10; d=b-(c10); p=acd; }
else if(i>=10&&i<100) {
int a,b; a=i/10; b=i-(a10); p=ab; }
else if(i>=0&&i<10) {
p=i; }
else {
}
System.out.println("The multiplication of all digits in "+i+" is "+p);
}
}
Practice #2
Write a program that reads three edges for a triangle and computes the perimeter if the input is valid. Otherwise, display that the input is invalid. The input is valid for triangle edges if the sum of every pair of two edges is greater than the remaining edge. (삼각형의 3개 변의 길이(실수형)를 사용자로부터 입력받아, 유효하면 삼각형의 둘레를 계산하는 프로그램을 작성하시오. 단, 삼각형의 3변은 모든 조합에 대해 2개 변의 합이 다른 한 변보다 길어야 유효하다.)
import java.util.Scanner;
public class Perimeter {
public static void main(String[]args) {
System.out.println("Enter three edges (length in double): ");
Scanner input = new Scanner(System.in);
double a=input.nextDouble();
double b=input.nextDouble();
double c=input.nextDouble();
double sum=0.0;
if(a+b<=c) {
System.out.println("Input is invalid\n");
}
else if(b+c<=a) {
System.out.println("Input is invalid\n");
}
else if(c+a<=b) {
System.out.println("Input is invalid\n");
}
else {
sum=a+b+c;
System.out.println("The perimeter is "+sum+"\n");
}
}
}