Challenge: Share the Sweets
Completed
Read the number of sweets and the number of friends. Print how many sweets each friend gets and how many are left over.
Input
Two whole numbers: sweets, then friends.
Output
Each friend gets: <n> Left over: <n>
Example
Input
17 5
Output
Each friend gets: 3 Left over: 2
Editor
Java
Hints
int / int throws away the decimal part – exactly what we want here.
% gives the remainder.
Solution explanation
sweets / friends is integer division and sweets % friends is what's left over.
Show solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sweets = sc.nextInt();
int friends = sc.nextInt();
System.out.println("Each friend gets: " + (sweets / friends));
System.out.println("Left over: " + (sweets % friends));
}
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module