# TCS NQT Doctor Income Coding Problem

TCS NQT Doctor Income Coding Problem

We at gradjobopenings.com provide free job alerts of freshers job drives. In this website we list on campus job openings for freshers and off campus job openings for freshers and also work from home job openings. This is the best website to apply for off campus drive in India. Visit our website for government job alerts and private job alerts. We also list free interview notes and study materials, one of the best interview study website.comfortable to face the interviews:

A doctor has a clinic where he serves his patients. The doctor’s consultation fees are different for different groups of patients depending on their age. If the patient’s age is below 17. The fee is 200 INR. If the patient’s age is between 17 and 40, fees are 400 INR.

If the patient’s age is above 40, fees are 300 INR. Write a code to calculate earnings in a day for which one array/List of values representing the age of patients visited on that day is passed as input.

Note:

1. Age should not be zero or less than zero or above 120.

2. Doctors consult a maximum of 20 patients a day.

3. Enter age value (press ENTER without value to stop):

**Example 1:**

Input

“`

20

30

40

50

2

3

14

“`

Output

“`

Total income 2100 INR

“`

**Algorithm**

“`

1. Declare and initialise limit, age and income to 0.

2. While age != ” and limit < 20, do

if age < 0 or age > 120, do

print “INVALID INPUT”

continue

if age <= 17, do

income = income + 200

else if age <= 40, do

income = income + 400

else

income = income + 300

limit = limit + 1

3. Print Total income (income) INR

Program In C

``````#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char str[5];
int age, limit = 1, income = 0;
fgets(str, 5, stdin);
while(strcmp(str, "\n") && limit != 20)
{
age = atoi(str);
if (age < 0 || age > 120)
{
printf("INVALID INPUT");
continue;
}
if (age <= 17)
income += 200;
else if (age <= 40)
income += 400;
else
income += 300;
fgets(str, 5, stdin);
limit ++;
}
printf("Total income %d INR", income);
return 0;
}
``````

Program in C++

``````#include <iostream>

using std::cin;
using std::cout;
using std::string;
using std::stoi;

int main() {
int age, limit {1}, income {0};
string str;
getline(cin, str);
while (str.size() > 0 && limit != 20)
{
age = stoi(str);
if (age < 0 || age > 120)
{
cout << "INVALID INPUT";
continue;
}
if (age <= 17)
income += 200;
else if (age <= 40)
income += 400;
else
income += 300;
getline(cin, str);
limit++;
}
cout << "Total income " << income << " INR";
return 0;
}
``````

Program in Python

``````if __name__ == "__main__":
limit = 1
age = input()
income = 0
while age != '' and limit != 20:
if int(age) < 0 or int(age) > 120:
print("INVALID INPUT")
continue
if int(age) <= 17:
income += 200
elif int(age) <= 40:
income += 400
else:
income += 300
age = input()
limit += 1
print("Total income", income, "INR")
``````

Program in Java

``````import java.util.Scanner;
import java.util.regex.*;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Pattern delimiters = Pattern.compile(System.getProperty("line.separator")+"|\\s");
sc.useDelimiter(delimiters);
int age, income = 0, limit = 1;
while (sc.hasNextInt() && limit != 20){
age = sc.nextInt();
if (age < 0 || age > 120)
{
System.out.print("INVALID INPUT");
continue;
}
if (age <= 17)
income += 200;
else if (age <= 40)
income += 400;
else
income += 300;
limit ++;
}
System.out.print("Total income " + income + " INR");
}
}
``````