Number series question in Tcs


The points we covered in this article are mention below


  • Question
  • question analysis
  • solution
  • explanation 
  • exception

Question


TCS NQT NUMBER SERIES
TCS NQT NUMBER SERIES PROBLEM


print the following series

1 , 1 , 2 , 3 , 4 , 9 , 8 , 27 , 16 , 81 , 32 , 243 , 64 , 729 , 128 , 2187


Question analysis 

This series is a mixture of 2 series - all the odd term in series form 
a geometric series and all the even terms form yet another geometric series.
wap to print the series

In the odd places this series is produces a geometric series and in the even places of series produces a geometric series.


solution

#include<stdio.h>

int main()

{

    int i,n,a,b;

    printf("Enter the number :");

    scanf("%d",&n); //first ask user to enter the number

    for ( i = 1; i <= n; i++) //run the loop till the entered value

    {

        if (i%2!=0) //for the even places

        {

            if(i==1)

            a=1;

            else

            a=a*2;

            printf("%d  ",a);

        }

        else //for odd positions

        {

            if (i==2)

            b=1;

            else

            b=b*3;   

            printf("%d  ",b);                    

        }

    }

Explanation


  • first we required the header file then start of the main function.
  • Then we declare 4 variables , first the 'n' variable is for asking the user to enter then number to display the series till the number user enter. then we take 'a' variable for printing the even positions and take variable 'b' for printing the odd positions.
  • Then we run a for loop till the user entered number  and use the if condition for checking that (if user 11 then its start checking from 1 if condition check that 1%2 !=0 then its satisfy the condition and its come inside the if condition and check again if(i==1 ) this condition is satisfy so its print 1 in the first place.
  • then the next number is 2 and it check the condition if(i%2!=0) this condition is become false in this case so its come to the else part now and check  if(i==2) and condition satisfy so its print on the screen 1.
  • Then further conditions are satisfy similarly try yourself  .
  • If still any doubt watch out the given video with fully explanation.


Exceptional case

if it question ask you to print a specific position in the series.

Coding


#include<stdio.h>
int main()
{
    int i,n,a,b;
    printf("Enter the number :");
    scanf("%d",&n); //first ask user to enter the number
    for ( i = 0; i <= n; i++) //run the loop till the entered value
    {
        if (i%2!=0) //for the odd places
        {
            if(i==1)
            a=1;
            else
            a=a*2;
        }
        else //for even positions
        {
            if (i==2)
            b=1;
            else
            b=b*3;                       
        }
    }
    if(n%2!=0) //for even positions
    {
        printf("\n %d term of series is %d\t",n,a);
    }
    else //for odd positions
    {
        printf("\n%d term of series is %d\t",n,b);
    }
    return 0;   
}

Explanation


Here we also implement the same logic but here we run an another loop for printing the specific position that user wants , by the help same if - else condition.


Also check Out





  Q-2)  pointers in c




  

   Q-3) Vector Sort


Also check Out Projects for Resume

    click here

Projects on C++


Projects on C


Problems On C


Problems On C++



Feedback



  • That is for TCS NQT Number series problem  hope you enjoy  the article and understand this explanation clearly , if still have you any doubts please comment below and if any suggestion for us give us through the comment and please share the article with your friends if you like this.




THANKYOU!!