TCS NQT - Coding Ability

Test Number 8


1.Consider the below series :

0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8

This series is a mixture of 2 series all the odd terms in this series form even numbers in ascending order and every even terms is derived from the previous  term using the formula (x/2)

Write a program to find the nth term in this 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 the value of the nth term no other characters /strings or message should be written to STDOUT.

For example if n=10,the 10 th term in the series is to be derived from the 9th term in the series. The 9th term is 8 so the 10th term is (8/2)=4. Only the value 4 should be printed to STDOUT.

You can assume that the n will not exceed 20,000.


         

#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)
{
if(i>1)
a = a + 2;
}
else
{
b = a/2;
}
}

if(n%2!=0)
{
printf("%d",a);
}
else
{
printf("%d",b);
}

return 0;
}

    

#include<iostream>

using namespace std;

int main()
{
int i, n, a=0, b=0;
cout << "enter number : ";
cin >> n;


for(i=1;i<=n;i++)
{
if(i%2!=0)
{
if(i>1)
a = a + 2;
}
else
{
b = a/2;
}
}

if(n%2!=0)
{
cout << a;
}
else
{
cout << b;
}

return 0;
}

        

import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a = 0, b = 0;
if(n % 2 == 0)
{
for(int i = 1 ; i <= (n-2) ; i = i+2)
{
a = a + 2;
b = a / 2;
}
System.out.print(b);
}
else
{
for(int i = 1 ; i < (n-2) ; i = i+2)
{
a = a + 2;
b = a / 2;
}
a = a + 2;
System.out.print(a);
}
}
}

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

a=0
b=0

for i in range(1,n+1):
if(i%2!=0):
a= a+2
else:
b= b+1

if(n%2!=0):
print('{}'.format(a-2))
else:
print('{}'.format(b-1))





";

2.1. The program will recieve 3 English words inputs from STDIN

These three words will be read one at a time, in three separate line
The first word should be changed like all vowels should be replaced by %
The second word should be changed like all consonants should be replaced by #
The third word should be changed like all char should be converted to upper case
Then concatenate the three words and print them
Other than these concatenated word, no other characters/string should or message should be written to STDOUT

For example if you print how are you then output should be h%wa#eYOU.

You can assume that input of each word will not exceed more than 5 chars

#include <stdio.h>
#include <string.h>
int main()
{
char a[10], b[10], c[10];
int i,j;
int x, y, z;

scanf("%s",a);
scanf("%s",b);
scanf("%s",c);

x = strlen(a);
y = strlen(b);
for(i=0;i<x;i++)
{
if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u')
{
a[i] = '%';
}
}
for(j=0;j<y;j++)
{
if(b[j]=='b'||b[j]=='c'||b[j]=='d'||b[j]=='f'||b[j]=='g'||b[j]=='h'||b[j]=='j'||b[j]=='k'||b[j]=='l'||
b[j]=='m'||b[j]=='n'||b[j]=='p'||b[j]=='q'||b[j]=='r'||b[j]=='s'||b[j]=='t'||b[j]=='v'||b[j]=='w'||
b[j]=='x'||b[j]=='y'||b[j]=='z')
{
b[j] = '#';
}

if(b[j]=='B'||b[j]=='C'||b[j]=='D'||b[j]=='F'||b[j]=='G'||b[j]=='H'||b[j]=='J'||b[j]=='K'||b[j]=='L'||
b[j]=='M'||b[j]=='N'||b[j]=='P'||b[j]=='Q'||b[j]=='R'||b[j]=='S'||b[j]=='T'||b[j]=='V'||b[j]=='W'||
b[j]=='X'||b[j]=='Y'||b[j]=='Z')
{
b[j] = '#';
}
}
z=0;
while (c[z] != '\0') {
if (c[z] >= 'a' && c[z] <= 'z')
{
c[z] = c[z] - 32;
}
z++;
}
printf("%s%s%s",a,b,c);
}

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
char a[10], b[10], c[10];
int i,j;
int x, y, z;

cin >> a;
cin >> b;
cin >> c;

x = strlen(a);
y = strlen(b);
for(i=0;i<x;i++)
{
if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u')
{
a[i] = '%';
}
}
for(j=0;j<y;j++)
{
if(b[j]=='b'||b[j]=='c'||b[j]=='d'||b[j]=='f'||b[j]=='g'||b[j]=='h'||b[j]=='j'||b[j]=='k'||b[j]=='l'||
b[j]=='m'||b[j]=='n'||b[j]=='p'||b[j]=='q'||b[j]=='r'||b[j]=='s'||b[j]=='t'||b[j]=='v'||b[j]=='w'||
b[j]=='x'||b[j]=='y'||b[j]=='z')
{
b[j] = '#';
}

if(b[j]=='B'||b[j]=='C'||b[j]=='D'||b[j]=='F'||b[j]=='G'||b[j]=='H'||b[j]=='J'||b[j]=='K'||b[j]=='L'||
b[j]=='M'||b[j]=='N'||b[j]=='P'||b[j]=='Q'||b[j]=='R'||b[j]=='S'||b[j]=='T'||b[j]=='V'||b[j]=='W'||
b[j]=='X'||b[j]=='Y'||b[j]=='Z')
{
b[j] = '#';
}
}
z=0;
while (c[z] != '\0') {
if (c[z] >= 'a' && c[z] <= 'z')
{
c[z] = c[z] - 32;
}
z++;
}
cout << a << b << c;

return 0;
}

public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter three words : ");

String s1 = sc.next();
String s2 = sc.next();
String s3 = sc.next();

int l1 = s1.length();
int l2 = s2.length();

String str1 = "";
String str2 = "";
String str3 = "";
char c;
for(int i = 0 ; i < l1 ; i++)
{
c = s1.charAt(i);
if(c == 'A' || c == 'a' || c == 'E' ||
c == 'e' || c == 'I' || c == 'i' || c == 'O' || c == 'o' || c == 'U' || c == 'u')
str1 = str1 + "%";
else
str1 = str1 + c;
}
for(int i = 0 ; i < l2 ; i++)
{
c = s2.charAt(i);
if((c >= 'A' && c <= 'Z')||(c >= 'a' && c <= 'z'))
{
if(c == 'A' || c == 'a' || c == 'E' || c == 'e' ||
c == 'I' || c == 'i' || c == 'O' || c == 'o' || c == 'U' || c == 'u')
str2 = str2 + c;
else
str2 = str2 + "#";
}
else
str2 = str2 + c;
}
str3 = s3.toUpperCase();
System.out.println(str1+str2+str3);
}
}