- Prepare
All Platforms Programming Aptitude Syllabus Interview Preparation Interview Exp. Off Campus - Prime Video
- Prime Mock

- Interview Experience
- Prime VideoNew
- Prime Mock
- Interview Prep
- Nano Degree
- Prime Video
- Prime Mock

# TCS Coding Question in Python(Shivangi Gupta)

### Prog 1. Program in Python to check if the year taken as input is a leap or not.

#python program to check if a year number taken from the user is a leap year or not, using nested if-else.num = int(input("Enter the year you want to check if is leap year or not: "))#take input year from the user to check if it is a leap yearif(num%4 == 0):#check if the number is divisible by 4 and if true move to next loopif(num%100 == 0):#check if the input year is divisible by 100 and if true move to next loopif(num%400 == 0):print("The year {} is a leap year".format(num))#the input year is divisible by 4, 100 and 400, hence leap year.else:print("The year {} is Not a leap year".format(num))else:print("The year {} is a leap year".format(num))#if the number is divisible by both 4 and 100 it is a leap yearelse:print("The year {} is Not a leap year".format(num))#if the input num is not divisible by 4 then it can not be a leap year altogether.

**Explanation:**

To check whether a year is leap or not

- We first divide the year by 4.
- If it is not divisible by 4 then it is not a leap year.
- If it is divisible by 4 leaving remainder 0.

- We divide the year by 100
- If it is not divisible by 100 then it is a leap year.
- If it is divisible by 100 leaving remainder 0 .

- We divide the year by 400
- If it is not divisible by 400 then it is a leap year.
- If it is divisible by 400 leaving remainder 0

- Then it is a leap year.

### Prog 2. Write a code to check whether no is prime or not. Condition use function check() to find whether entered no is positive or negative ,if negative then enter the no, And if yes pas no as a parameter to prime() and check whether no is prime or not?

def prime(n):

if n > 1:

for i in range(2, n):

if (n % i) == 0:

print(n, "is not a prime number")

break

else:

print(n, "is a prime number")

num = int(input("enter a number: "))

if (num > 0):

prime(num)

else:

print("please enter a positive number")

**Explanation:**

- First we take a number input from the user as ‘num’.
- Then we check if the number is positive or negative.
- If the number is positive, a function ‘prime()’ is called which checks if the input number is prime or not.
- If the number is negative, a message is printed to enter a positive number.

### Prog 3. Find the 15th term of the series?

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

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))

**Note : **15th term of the series is **49.**

**Explanation :**

- In this series the odd term is an increment of 7 {0, 7, 14, 21, 28, 35 – – – – – }.
- And the even term is an increment of 6 {0, 6, 12, 18, 24, 30 – – – – – – }.

### Prog 4. Find the nth term of the series.

#### 1 , 1 , 2 , 3 , 4 , 9 , 8 , 27 , 16 , 81 , 32 , 243 ,_ _ _ _ _ _ _

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))

**Explanation:**

- In this series, the odd term is a multiple of 2 starting from 1 {1, 2, 4, 8, 16, 32, 64- – – – -}.
- And even term is a multiple of 3 starting from 1 {1, 3, 9, 27, 81, 243- – – – – }.

### Prog 5. Consider the below series. 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.

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

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

a=0

b=0

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

if(i%2!=0):

a= a+1

else:

b= b+2

if(n%2!=0):

print('{}'.format(a-1))

else:

print('{}'.format(b-2))

**Explanation:**

- The program can be solved from another approach used in this code.
- In this series the even term is the increment of 1 starting from 0 {0, 1, 2, 3, 4, 5, 6, 7, 8- – – – – – – }
- And odd term is the increment of 2 starting from 0 {0, 2, 4, 6, 8, 10, 12, 14, 16- – – – – – }

Login/Signup to comment