public static int sum(int n){
int sum=0;
while(n>0){
int lastDigit = n%10;
sum= sum+lastDigit;
n=n/10;
}
return sum;
}
public static void main(String[] args) {
int n=78555;
int res=sum(n);
System.out.println(res);
}
Write a Java method to compute the sum of the digits in an integer.(Hint: Approach this question in the following way:a.Take a variable sum = 0b.Find the last digit of the numberc.Add it to the sumd.Repeat a & b until the number becomes 0 )
int sum=0;
while(n>0){
int lastDigit = n%10;
sum= sum+lastDigit;
n=n/10;
}
return sum;
}
public static void main(String[] args) {
int n=78555;
int res=sum(n);
System.out.println(res);
}
Write a Java method to compute the sum of the digits in an integer.(Hint: Approach this question in the following way:a.Take a variable sum = 0b.Find the last digit of the numberc.Add it to the sumd.Repeat a & b until the number becomes 0 )
❤1
Discussion about JAVA Programming
Screenshot 2024-03-14 180339.png
Today's Problem Solution.png
100.9 KB
Solution of this Problem 🎯
Today's problem ❓❔
.Assign stalls to K cows to maximize the minimum distance between them
Given a sorted array arr[] consisting of N integers which denote the position of a stall. You are also given an integer K which denotes the number of aggressive cows. You are given the task of assigning stalls to K cows such that the minimum distance between any two of them is the maximum possible.
Examples:
Input: N = 5, K = 3, arr[] = {1, 2, 4, 8, 9}
Output: 3
Explanation: We can place cow 1 at position 1, cow 2 at position 4 and cow 3 at position 9. So, the maximum possible minimum distance between two cows is 3.
Input: N = 6, K = 4, arr[] = {6, 7, 9, 11, 13, 15}
Output: 2
Explanation: We can place cow 1 at position 6, cow 2 at position 9 and cow 4 at position 15. So, the maximum possible minimum distance between two cows is 2.
.Assign stalls to K cows to maximize the minimum distance between them
Given a sorted array arr[] consisting of N integers which denote the position of a stall. You are also given an integer K which denotes the number of aggressive cows. You are given the task of assigning stalls to K cows such that the minimum distance between any two of them is the maximum possible.
Examples:
Input: N = 5, K = 3, arr[] = {1, 2, 4, 8, 9}
Output: 3
Explanation: We can place cow 1 at position 1, cow 2 at position 4 and cow 3 at position 9. So, the maximum possible minimum distance between two cows is 3.
Input: N = 6, K = 4, arr[] = {6, 7, 9, 11, 13, 15}
Output: 2
Explanation: We can place cow 1 at position 6, cow 2 at position 9 and cow 4 at position 15. So, the maximum possible minimum distance between two cows is 2.