In this post we will see how to solve coding questions asked in TCS off campus recruitment. Solved programming questions asked in TCS off campus recruitments.
TCS Off Campus Coding Questions
Question:
Banks have been introducing various innovative deposit schemes to expand their services to customers. One of the schemes is known as the rainy day fund, where a person should invest a certain amount every year. The investment can be done under the following conditions.
The amount invested in the year y1 in the first cycle should be equal to RS 1
The amount invested every year should be exactly RS 1 more than that in the previous cycle.
Once the period of 7 years is over, the investment cycle will start from y1 again, which means y1, y2, y3, y4, y5, y6, y7, y1, y2, y3, y4…so on. In this case, the amount invested in any year in this cycle will be RS 1 more than the investment in the corresponding year in the previous cycle.
Suppose in the first cycle of 7 years, the investment is y1=1, y2=2, y3=3, y4=4, y5=5, y6=6, y7=7. In the second cycle, it should be y1=2, y2=3, y3=4……..y7=8.
The task here is to help the investor to calculate the total amount of money he/she has invested in the “N” years, where N indicates the number of years.
Example 1:
Input:
8 value of N
Output:
Explanation:
From the inputs given above
Number of years he has invested is : 8 years
Cycle 1
Y1=1
Y2=2
Y3=3
Y4=4
Y5=5
Y6=6
Y7=7
After 7 years, y8 will be y1 of the second cycle
Cycle 2
Y1=2 (1 more than the corresponding previous year)
The total amount invested is:
Cycle 1 + cycle 2
Y1+y2+y3+y4+y5+y6+y7+y1
1+2+3+4+5+6+7+2= 30
Explanation:
Total number of years are 8. For 7 years 1 cycle will be completed. So for 1 cycle the investment will be 1+2+3+4+5+6+7= 28. Now after 7 years cycle 2 will start where investment is Rs 1 more than previous cycle, so y1= 2.
cycle1 + cycle 2 = 28+2 = 30.
Algorithm for solving
- Get the number of Years N, which indicates investment years.
- Using for loop store the cycle1 investments in array
- For cycle 2 featch the corresponding cycle 1 elements and add Rs 1.
- At the end all all the elements of cycle 1 and cycle 2 elements to get the total investment done by investors.
- Congrats you just solved a coding question, Click Here for more such coding questions
JAVA CODE
import java.util.*; class problem32 { public static void main(String[] args) { System.out.println("Enter the number of years "); Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int k = 0; int res = 0; int array1[] = new int[N]; for (int i = 0; i < N; i++) { if (i < 7) { array1[k] = i + 1; k = k + 1; } else { array1[i] = array1[N - i - 1] + 1; } } for (int i = 0; i < array1.length; i++) { res = res + array1[i]; } System.out.println("Total investment done in " + N + "years is :" + res); } }
Watch below video to get step by step explanation for above java code
Pingback: L&T off campus coding question solved - Is It Actually