TCS NQT - Coding Ability

Test Number 7


1. Find the 15th term of the series?

0,0,7,6,14,12,21,18, 28

Explanation : In this series the odd term is increment of 7 {0, 7, 14, 21, 28, 35 – – – – – – }

And even term is a increment of 6 {0, 6, 12, 18, 24, 30 – – – – – – }

         

#include <stdio.h>
int main()
{
int i, n, a=0, b=0;
printf("enter number : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
a = a + 7;
}
else
{
b = b + 6;
}
}
if(n%2!=0)
{
printf("%d term of series is %d\t",n,a-7);
}
else
{
printf("%d term of series is %d\t",n,b-6);
}
return 0;
}

        

#include
using namespace std;
int main()
{
//initialising variables
int n,d;
cout<<"Enter the position: ";
//user input
cin>>n;
//logic to find nth element of the series
if(n==1||n==2)
{
cout<<0;
return 0;
}
else if(n%2==0)
{
n=n/2;
d=6;
}
else
{
n=n/2+1;
d=7;
}
//logic ends here
//printing output
cout<<(n-1)*d;
return 0;
}

       

class Main
{
public static void main(String[] args)
{
int a = 7, b = 0,c;
System.out.println("Series :");
for(int i = 1 ; i < 8 ; i++)
{
c = a * b;
System.out.print(c+" "+(c-b)+" ");
b++;
}
c = a * b;
System.out.println(c);
System.out.print("15th element of the series is = "+c);
}
}

num = int(input('enter the number: '))

a=0

b=0

for i in range(1,num+1):

if(i%2!=0):

a= a+7

else:

b = b+6

if(num%2!=0):

print(' {} term of series is {}'.format(num,a-7))

else:

print('{} term of series is {}'.format(num,b-6))





";

2.Consider the following series: 1, 1, 2, 3, 4, 9, 8, 27, 16, 81, 32, 243, 64, 729, 128, 2187 …

This series is a mixture of 2 series – all the odd terms in this series form a geometric series and all the even terms form yet another geometric series. Write a program to find the Nth term in the series.

The value N in a positive integer that should be read from STDIN. The Nth term that is calculated by the program should be written to STDOUT. Other than value of n th term,no other character / string or message should be written to STDOUT. For example , if N=16, the 16th term in the series is 2187, so only value 2187 should be printed to STDOUT.

You can assume that N will not exceed 30.

#include<stdio.h>
int main()
{
int i, n, a=1, b=1;
printf("enter number : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
a = a * 2;
}
else
{
b = b * 3;
}
}
if(n%2!=0)
{
printf("\n%d term of series is %d\t",n,a/2);
}
else
{
printf("\n%d term of series is %d\t",n,b/3);
}
return 0;
}

#include
#include
using namespace std;
int main()
{
//initialising variables
int n,r,term;
cout<<"Enter the position: ";
//user input
cin>>n;
//logic to find nth element of the series
if(n==1||n==2)
{
cout<<1;
return 0;
}
else if(n%2==0)
{
n=n/2-1;
r=3;
}
else
{
n=n/2;
r=2;
}
//logic ends here
//printing output
cout<<(int)(pow(r,n));
return 0;
}

import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//input value of n
System.out.print("Enter the value of n : ");
int n = sc.nextInt();
int a = 1, b = 1;
//statement for even value of n
if(n % 2 == 0)
{
for(int i = 1 ; i <= (n-2) ; i = i+2)
{
a = a * 2;
b = b * 3;
}
System.out.print(n+" element of the series is = "+b);
}
//statement for odd value of n
else
{
for(int i = 1 ; i < (n-2) ; i = i+2)
{
a = a * 2;
b = b * 3;
}
a = a * 2;
System.out.print(n+" element of the series is = "+a);
}
}
}

n = int(input('enter the number: '))

a= 1

b= 1

for i in range(1, n+1):

if(i%2!=0):

 

a = a*2

else:

b = b*3

if(n%2!=0):

print('\n{} term of series is {}\t'.format(n,a/2))

else:

print('\n{} term of series is {}\t'.format(n,a/2))