Coding Questions asked in TCS Ninja

We strongly recommend to visit the following page here to learn Command Line Programming, before trying to solve questions below.

  1. Number of Questions – 1
  2. Time – 20 mins

Paid Section –

Free Section –

Consider the below series:
1,2,1,3,2,5,3,7,5,11,8,13,13,17,

This series is a mixture of 2 series fail the odd terms in this series form a Fibonacci series and all the even terms are the prime numbers in ascending order

Write a program to find the Nth term in this series

The value N in a positive integer that should be read from mm. The Nth term that is calculated by the program should be written to STDOUT Otherthan the value of Nth term , no other characters / string or message should be written to STDOUT.

For example, when N:14, the 14th term in the series is 17 So only the value 17 should be printed to STDOUT

Solution –

#include

void fibo(int);

void prime(int);

main()

{

int n,e;

scanf("%d",&n);

e=n/2;

if(n%2==0)

prime(e);

else

fibo(e+1);

}

void prime(int n)

{

int i,j,no,flag=0,count=0;

 for(i=1;i<=100;i++)

  {

flag=0;

      for(j=2;j<=i/2;j++)

      {

if(i%j==0)

flag=0;

else

flag=1;

        }

  if(flag==1)

   count++;

   if(count==n)

       {

           printf("%d\n",i);

           break;

       }

   }

   

}

void fibo(int n)

{

 int n0=0,n1=1,n2,i;

for(i=3;i<=n;i++)

{

   n2=n0+n1;

   n0=n1;

   n1=n2;

}

printf("%d",n2);

}

TCS Ninja Coding Questions and Answers are graded by the TCS Server and not by the human, when you compile the program, the system checks for the following to give you marks –

a. Correct output on various test cases
b. Partial output at most obvious cases
c. logical error present in the codes
d. is the program compilation done.

There are a lot of free sections given above if you want to study more, you can buy TCS Coding Questions Paid materials which are basically command line questions.