└── candy program.java /candy program.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.Arrays; 3 | public class Candy 4 | { 5 | public static void main(String[] args) { 6 | Scanner s = new Scanner(System.in); 7 | 8 | System.out.println("Enter the candies"); 9 | int c=s.nextInt(); 10 | System.out.println("Enter the no of persons"); 11 | int p=s.nextInt(); 12 | int a[]= new int[p]; 13 | int i=0,j=0; 14 | while(c>0){ 15 | 16 | i=i+1; 17 | if(c>=i){ 18 | a[j%p]=a[j%p]+i; 19 | j++; 20 | c=c-i; 21 | } 22 | else{ 23 | a[j%p]=a[j%p]+c; 24 | c=0; 25 | } 26 | } 27 | System.out.println(Arrays.toString(a)); 28 | } 29 | } 30 | --------------------------------------------------------------------------------