PROGRAM TO ADD THE REVERSED NUMBER OF THE DATA:
STATEMENT:
A pair of numbers (X and Y) will be passed as input. The program must reverse the numbers and find the sum S. Then the sum S must be reversed and printed as output.
- If any leading zeroes are obtained while reversing any of the numerical values they should be discarded.
PROGRAM:
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int sum = reverse(x) + reverse(y);
System.out.println(reverse(sum));
}
private static int reverse(int x) {
StringBuilder sb = new StringBuilder(String.valueOf(x));
return Integer.parseInt(sb.reverse().toString());
}
}
Input:
305
794
Output:
1
Explanation:
305 and 794 when reversed are 503 and 497.
503+497 = 1000.
1000 when reversed is 1 which is printed as output.
PROGRAM TO PRINT THE NUMBER OF STEPS NEEDED TO FILL A CAN WITH WATER;
STATEMENT:
Two cans are with capacity X and Y liters. The program must determine the number of steps required to obtain exactly Z litres of liquid in one of the cans.
At the beginning both cans are empty. The following operations are counted as "steps".
- emptying a vessel,
- filling a vessel,
- pouring liquid from larger can to the smaller, without spilling, until one of the cans is either full or empty.
If it is not possible to obtain Z liters exactly then the output must be -1.
PROGRAM:
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
//Larger can is made as x
if (x < y) {
int temp = y;
y = x;
x = temp;
}
int fillCount = -1, currCount = 1; //currCount is made 1 to fill x
boolean isSmallerCanFilled = false; //y will be the smaller can
for (; x >= z; x -= y) {
if (x == z) {
fillCount = currCount;
break;
} else {
if (isSmallerCanFilled) {
//We need to empty smaller can in step 1 and fill from larger can as step 2
currCount += 2;
} else {
isSmallerCanFilled = true;
//Only filling from larger to smaller can as step 1. This happens only one time.
currCount += 1;
}
}
}
System.out.print(fillCount);
}
}
Input:
5
2
3
Output:
2
Explanation:
Here X=5, Y=2
Step 1: Pour 5 liters of liquid into 5 liter can
Step 2: Pour 2 liters from 5 liter can into 2 liter can.
Now the 5 liter can will have 3 liters which is Z. Hence 2 steps are required.
STATEMENT:
A pair of numbers (X and Y) will be passed as input. The program must reverse the numbers and find the sum S. Then the sum S must be reversed and printed as output.
- If any leading zeroes are obtained while reversing any of the numerical values they should be discarded.
PROGRAM:
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int sum = reverse(x) + reverse(y);
System.out.println(reverse(sum));
}
private static int reverse(int x) {
StringBuilder sb = new StringBuilder(String.valueOf(x));
return Integer.parseInt(sb.reverse().toString());
}
}
Input:
305
794
Output:
1
Explanation:
305 and 794 when reversed are 503 and 497.
503+497 = 1000.
1000 when reversed is 1 which is printed as output.
PROGRAM TO PRINT THE NUMBER OF STEPS NEEDED TO FILL A CAN WITH WATER;
STATEMENT:
Two cans are with capacity X and Y liters. The program must determine the number of steps required to obtain exactly Z litres of liquid in one of the cans.
At the beginning both cans are empty. The following operations are counted as "steps".
- emptying a vessel,
- filling a vessel,
- pouring liquid from larger can to the smaller, without spilling, until one of the cans is either full or empty.
If it is not possible to obtain Z liters exactly then the output must be -1.
PROGRAM:
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
//Larger can is made as x
if (x < y) {
int temp = y;
y = x;
x = temp;
}
int fillCount = -1, currCount = 1; //currCount is made 1 to fill x
boolean isSmallerCanFilled = false; //y will be the smaller can
for (; x >= z; x -= y) {
if (x == z) {
fillCount = currCount;
break;
} else {
if (isSmallerCanFilled) {
//We need to empty smaller can in step 1 and fill from larger can as step 2
currCount += 2;
} else {
isSmallerCanFilled = true;
//Only filling from larger to smaller can as step 1. This happens only one time.
currCount += 1;
}
}
}
System.out.print(fillCount);
}
}
Input:
5
2
3
Output:
2
Explanation:
Here X=5, Y=2
Step 1: Pour 5 liters of liquid into 5 liter can
Step 2: Pour 2 liters from 5 liter can into 2 liter can.
Now the 5 liter can will have 3 liters which is Z. Hence 2 steps are required.
No comments:
Post a Comment