Chocolates - c - Spicy Coders

Recent

Thursday, July 13, 2017

Chocolates - c



There are n children sitting in a circle. All of them are numbered in the clockwise order: the child number 2 sits to the left of the child number 1, the child number 3 sits to the left of the child number 2, ..., the child number 1 sits to the left of the child number n.

Malini has m chocolates. Malini stands in the middle of the circle and starts giving the chocolates to the children starting from child number 1 and moving clockwise. The child number i gets i chocolates. If Malini can't give the current child the required number of chocolates, then Malini takes the remaining chocolates and the process ends. Determine by the given n and m how many chocolates Malini will get in the end.

Input Format
The first line contains two integers n and m (1 ≤ n ≤ 50, 1 ≤ m ≤ 104) — the number of children and the number of chocolates correspondingly.

Output Format
If the input is invalid, print “Invalid Input”
For valid inputs, print the number of chocolates Malini ended up with.

Coding:

#include<stdio.h>
int main()
{
          int n,m,i;
          scanf("%d",&n);
          scanf("%d",&m);
          if(n<1||m<1||n>50||m>104){
                   printf("Invalid Input");
          }
          else
         {
              i=1;
              while(1){
                             if(m-i<0)
                             break;
                             m=m-i;
                             i++;
                             if(i>n)
                             i=1;
                   }
                   printf("%d",m);
                   }
                   return 0;
}


2 comments:

  1. I just couldn't leave your website before telling you that I truly enjoyed the top quality info you present to your visitors? Will be back again frequently to check up on new posts. galaxy chocolate price

    ReplyDelete