TCS Coding Questions with Solutions

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 Other than 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:

Command Line Program to Check if a Number is Prime or Not

public class Main{
public static void main(String[] args) {
int num = 29;
boolean flag = false;
for(int i = 2; i <= num/2; ++i)
{
// condition for nonprime number
if(num % i == 0)
{
flag = true;
break;
}
}
if (!flag)
System.out.println(num + ” is a prime number.”);
else
System.out.println(num + ” is not a prime number.”);
}
}

Command Line Program to Reverse a Number

public class ReverseNumber {
public static void main(String[] args) {
int num = 1234, reversed = 0;
while(num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println(“Reversed Number: ” + reversed);
}
}

Reverse without in built Functions

import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
int i,j,n,sum=0,k;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the no”);
n=sc.nextInt();
while(n!=0)
{
k=n%10;
sum=sum*10+k;
n=n/10; //System.out.println(sum);
}
System.out.println(sum);
}
}

GCD of three numbers

import java.util.*;
public class Main {
public static int hcf(int a, int b) {
if (b == 0)
return a;
else
return hcf(b, a % b);
}
public static int hcf(int a, int b, int c) {
return hcf(hcf(a, b), c);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(“Enter Number 1: “);
int num1 = input.nextInt();
System.out.print(“Enter Number 2: “);
int num2 = input.nextInt();
System.out.print(“Enter Number 3: “);
int num3 = input.nextInt();
int hcfOfNumbers = Main.hcf(num1, num2, num3);
System.out.println(“HCF of three numbers ” + num1 + “,” + num2
+ ” and ” + num3 + ” is: ” + hcfOfNumbers);
}
}

Second Largest Number in an Array using Command Line

Programming
public class Main{
public static int getSecondLargest(int[] a, int total){
int temp;
for (int i = 0; i < total; i++)
{
for (int j = i + 1; j < total; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a[total-2];
}
public static void main(String args[]){
int a[]={1,2,5,6,3,2};
int b[]={44,66,99,77,33,22,55};
System.out.println(“Second Largest: “+getSecondLargest(a,6));
System.out.println(“Second Largest: “+getSecondLargest(b,7));
}
}

Sum of odd number in given range using Command Line Programming

#include<stdio.h>
int main()
{
int i, Min, Max, SumEven = 0, SumOdd = 0;
printf(“\n Please Enter the Min, and Max Limit Values : \n”);
scanf(“%d %d”, &Min, &Max);
for(i = Min; i <= Max; i++)
{
if ( i%2 == 0 )
{
SumEven = SumEven + i;
}
else
{
SumOdd = SumOdd + i;
}
}
printf(“\n Sum of Even Numbers between %d and %d = %d”, Min, Max,
SumEven);
printf(“\n Sum of Odd Numbers between %d and %d = %d”, Min, Max,
SumOdd);
return 0;
}

Write a program to convert a number to its binary equivalent and print the bit representation of the number

#include<stdio.h>
void binary(unsigned int);
void main() {
unsigned int num;
printf(“Enter Decimal Number : “);
scanf(“%u”,&num);
binary(num);
}
void binary(unsigned int num) {
unsigned int mask=0x80000000;
printf(“\nThe Binary Number is :”);
while(mask > 0){
if((num & mask) == 0 )
printf(“0”);
else
printf(“1”);
mask = mask >> 1;
}
printf(“\n”);
}

Write a program to multiply a number by 8 without using the * operator

#include<stdio.h>
int main()
{
int product = 0, a, count;
scanf(“%d”, &a);
for(count = 0; count < 8; count++)
{
product = product + a;
}
printf(“%d\n”, product);
return 0;
}

Provide a fast way to multiply a number by 31
#include <stdio.h>
void main()
{
int T,n;
scanf(“%d”,&T);
n=T;
n=n<<5;
n=n-T;
printf(“%d”,n);
}

Write a function to calculate the length of a string without using strlen function using Command Line Programming

#include <stdio.h>
int main()
{
char s[1000];
int i;
printf(“Enter a string: “);
scanf(“%s”, s);
for(i = 0; s[i] != ‘\0’; ++i);
printf(“Length of string: %d”, i);
return 0;
}

Write a function that reads a sentence into a string and splits the sentence into words using library functions

#include <stdio.h>
#include <string.h>
int main() {
char sentence[100];
char *p;
printf(“\nPlease enter a sentence :”);
gets(sentence);
p = strtok(sentence, “\t”);
while ( p != NULL ){
printf(“%s\n”, p);
p = strtok(NULL, “\t”);
}
return 0;
}

Write a function that removes all duplicate spaces from a sentence. Your program should ensure that only one space exists between words

import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
String str;
int i,j,c=0;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the String”);
str=sc.nextLine();
System.out.println(str.replaceAll(“\\s+”,” “));
}
}

Write a function that uses character handling library functions to determine the number of upper case, lower case, digits, spaces and punctuation characters in the specified text.

import java.io.*;
class Main
{
public static void main(String args[])
{
String str = “#GeeKs01fOr@gEEks07”;
int upper = 0, lower = 0, number = 0, special = 0;
for(int i = 0; i < str.length(); i++)
{
char ch = str.charAt(i);
if (ch >= ‘A’ && ch <= ‘Z’)
upper++;
else if (ch >= ‘a’ && ch <= ‘z’)
lower++;
else if (ch >= ‘0’ && ch <= ‘9’)
number++;
else
special++;

}

System.out.println(“Lower case letters : ” + lower);
System.out.println(“Upper case letters : ” + upper);
System.out.println(“Number : ” + number);
System.out.println(“Special characters : ” + special);
}
}

Write a Program to remove vowels from a string?

public class Main {
public static void main(String[] args) {
String string = “Welcome to Candid Java
Programming”;
System.out.println(“Input String : “+string);
string = string.replaceAll(“[AaEeIiOoUu]”, “”);
System.out.println(string);
}
}

Write a C program which will check whether a given number N is a Prime or Not. If the Number N is a Prime, then find it’s square root and print that value to the STDOUT as floating point number with exactly 2 decimal precision.

  • If the number is not Prime, then print the value 0.00 to STDOUT.
  • The given number will be positive non zero integers and it will be passed
  • to the program as the first command-line argument.
  • Other than floating point No other information should be printed to STDOUT.

import java.util.*;
public class PrimeSqr{
static void primesqr(int num){
boolean res=false;
for(int i=2;i<=num/2;i++){
if(num%i==0){
res=true;
break;
}
}
if(res==false){
System.out.printf(“Square Root of the prime number is
:%.2f”,Math.sqrt(num));
}
else{
System.out.println(“Sorry!! your number is not a prime”);
}
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int number=sc.nextInt();
primesqr(number);
}
}

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here