Coding Question 1

In a puzzle game, there are five blocks. Each block is assigned with some repeating numbers.



The number 1 starts from block-1, 2 on block-2, 3 on block -3, 4 on block-4 and 5 on block-5. Again 6 on block-4 and so on.

Here we observe a pattern, the numbers 10, 8 and 2 are on block-2 , 3,7 and 11 on the block 3 and so on.

 

Given a positive integer N. The task is to find the correct block to which the number assigned.




Example 1:

Input:

3   (Value of N)

Output:

3    (Block number )


Example 2:

Input:

13   (Value of N)

Output:

5    (Block number )

Code In JAVA:


import java.util.*;

class Main

{

    public static void main(String[] args)

    {

              Scanner sc=new Scanner(System.in);

              int n=sc.nextInt();

              int r=n%8;

              if(r==0)

                   System.out.println(2);

             else if(r<5)

                  System.out.println(r);

             else

                  System.out.println(10-r);

    }

}


Code in C:

#include <stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    int r;
    r=n%8;
    if(r==0)
    printf("2");
    else if(r<5)
    printf("%d",r);
    else
    printf("%d",(10-r));
    return 0;
}


Output:

13

5


 

Comments