# TCS Coding Questions Using Java

### Question 1 :

Checking if a given year is leap year or not.

#### Code :

`/*Java program to check whether a year entered by user is a leap year or not and a leap year is a yearwhich is completely divisible by 4,but the year should not be a century year except it is divisible by 400*/import java.util.Scanner;//class declarationpublic class LeapYear{	//main method declaration	public static void main(String[] args)	{		//scanner class declaration        Scanner sc=new Scanner(System.in);		//input from year from user		System.out.println("Enter a Year");		int year = sc.nextInt();        boolean leap = false;		//condition for year divisible by 4        if(year % 4 == 0)        {	//condition for century year, that leap year can be a century year if the year is divisible by 400             if( year % 100 == 0)            {                // year is divisible by 400, hence the year is a leap year                if ( year % 400 == 0)                    leap = true;                else                    leap = false;            }            else                leap = true;        }        else            leap = false;		//if leap is true, then year is leap year, else year is not a leap year        if(leap)            System.out.println(year + " is a leap year.");        else            System.out.println(year + " is not a leap year.");    }}`

#### Output :

`Enter a Year20162016 is a leap year.Enter a Year20002000 is a leap year.Enter a Year15001500 is not a leap year.Enter a Year19981998 is not a leap year.`

### Question 2 :

Write a code to check whether a  number is prime or not.

Condition : Use function check() to find whether entered number is positive or negative. If number is negative then ask the user to re-enter the number, and if the number is positive then pass the number as a parameter to prime() and check whether number is prime or not?

#### Code :

`/*Java program to check whether a number entered by user is prime or not for only positive numbers, if the number is negative then ask the user to re-enter the number*/ //Prime number is a number which is divisible by 1 and another by itself only.import java.util.Scanner;class CheckPrime{	public static void main(String[] args)	{		Scanner sc = new Scanner(System.in);		//input a number from user		System.out.println("Enter the number to be checked : ");		int n = sc.nextInt();		//create object of class CheckPrime		CheckPrime ob=new CheckPrime();		//calling function with value n, as parameter		ob.check(n);	}	//function for checking number is positive or negative 	void check(int n)	{		if(n<0)			System.out.println("Please enter a positive integer");		else			prime(n);	}	//function for checking number is prime or not 	void prime(int n)	{		int c=0;		for(int i=2;i<n;i++)		{			if(n%i==0)			++c;		}		if(c>=1)			System.out.println("Entered number is not a prime number");		else			System.out.println("Entered number is a prime number");	}}`

#### Output :

`Enter the number to be checked :5Entered number is a prime numberEnter the number to be checked :12Entered number is not a prime numberEnter the number to be checked :-3Please enter a positive integer`

### Question 3 :

Using a method, pass two variables and find the sum of two numbers.

Test case:

Number 1 : 20

Number 2 : 20.38

Sum = 40.38

There were a total of 4 test cases. Once you compile 3 of them will be shown to you and 1 will be a hidden one. You have to display error message if numbers are not numeric.

#### Code :

`import java.util.Scanner;class addition{	public static void main(String[] args)	{		Scanner sc = new Scanner(System.in);		System.out.print("Number 1 : ");						int num1 = sc.nextInt();		System.out.print("Number 2 : ");						float num2 = sc.nextFloat();		float sum = num1 + num2;		System.out.println("Sum = "+sum);	}}		`

#### Output :

`Number 1 : 12Number 2 : 20.38Sum = 32.379997Number 1 : 34Number 2 : 15.667Sum = 49.667`

### Question :4

Find the 15th term of the series?

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

#### Code :

`//Java program to find 15th element of the seriesclass series1{	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);	}}`

#### Output :

`Series :0       0       7       6       14      12      21      18      28      24      35      30      42      36      4915th element of the series is = 49`

### Question : 5

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.

#### Code :

`//Java program to find nth element of the seriesimport java.util.Scanner;class series2{	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);		}	}}`

#### Output :

`Enter the value of n : 1414 element of the series is = 729`

### Question : 6

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.

#### Code :

`//Java program to find nth element of the seriesimport java.util.Scanner;class series5{	public static void main(String[] args)	{		Scanner sc = new Scanner(System.in);		System.out.print("Enter the value of n : ");						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(n+" element of the series is = "+b);		}		else		{			for(int i = 1 ; i < (n-2) ; i = i+2)			{				a = a + 2;				b = a / 2;			}			a = a + 2;			System.out.print(n+" element of the series is = "+a);		}	}}`

#### Output :

`Enter the value of n : 1010 element of the series is = 4Enter the value of n : 77 element of the series is = 6`

### Question : 7

The program will recieve 3 English words inputs from STDIN

1. These three words will be read one at a time, in three separate line
2. The first word should be changed like all vowels should be replaced by \$
3. The second word should be changed like all consonants should be replaced by #
4. The third word should be changed like all char should be converted to upper case
5. 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

#### Code :

`import java.util.*;class wordsarrange{	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);	}}`

#### Output :

`Enter three words :howareyouh\$wa#eYOUEnter three words :Indiaisgreat\$nd\$i#GREATEnter three words :In2dia1Is56Gre8at\$n2d\$\$1I#56GRE8AT`