Count sundays coding problem

TCS NQT COUNT SUNDAYS CODING PROBLEM

Jack is always excited about Sunday. It is his favourite day when he gets to play all day. And goes cycling with his friends. So every time when the month starts he counts the number of Sundays he will get to enjoy. Considering the month can start with any day, be it Sunday, Monday… or so on, count the number of Sundays Jack will get to enjoy within n number of days.

**Input format**:

– First line: A three lower case characters string denoting the start of the month.

– Second line: A non-negative integer N, denoting the number of days.

**Output format**:

– First line: An non-negative integer M denoting the total number of Sundays from the start of the month within N days.

**Constraints:**

“`

1 <= N <= 100

“`

**Examples**

– Input:

“`

mon

13

“`

– Output: 2

– Explanation: The month start on mon(Monday). So the upcoming Sunday will arrive in the next 6 days. And then next Sunday in the next 7 days and so on. Now the total number of days is 13. It means 6 days to the first Sunday and then the remaining 7 days will end up on another Sunday. Total 2 Sundays may fall within 13 days.

Program in C

#include <stdio.h>
#include <string.h>
int main() {
    char day[4];
    int n;
    scanf("%s", day);
    scanf("%d", &n);
    if (!strcmp("mon", day))
        n -= 6;
    else if (!strcmp("tue", day))
        n -= 5;
    else if (!strcmp("wed", day))
        n -= 4;
    else if (!strcmp("thu", day))
        n -= 3;
    else if (!strcmp("fri", day))
        n -= 2;
    else if (!strcmp("sat", day))
        n -= 1;
    if (n >= 0)
        printf("%d ", n / 7 + 1);
    else
        printf("0");
    return 0;
}

Program in C++

#include <iostream>
using namespace std;
int main() {
    string day;
    int n;
    cin >> day;
    cin >> n;
    if ("mon" == day)
        n -= 6;
    else if ("tue" == day)
        n -= 5;
    else if ("wed" == day)
        n -= 4;
    else if ("thu" == day)
        n -= 3;
    else if ("fri" == day)
        n -= 2;
    else if ("sat" == day)
        n -= 1;
    cout << "n: " << n << endl;
    if (n >= 0)
        cout << n / 7 + 1;
    else
        cout << "0";
    return 0;
}

Program in JAVA

import java.util.Scanner;
class Main {
    public static int firstSunday(String day)
    {
        if ("mon".equals(day))
            return 6;
        else if ("tue".equals(day))
            return 5;
        else if ("wed".equals(day))
            return 4;
        else if ("thu".equals(day))
            return 3;
        else if ("fri".equals(day))
            return 2;
        else if ("sat".equals(day))
            return 1;
        return 0;
    }
    public static void main (String args[]) {
        Scanner in = new Scanner(System.in);
        String day = in.next();
        int n = in.nextInt();
        in.close();
        n = n - firstSunday(day);
        if (n >= 0)
            System.out.print(n / 7 + 1);
        else
            System.out.print("0");
    }
}

Program in Python

if __name__ == '__main__':
    day = input()
    n = int(input())
    if "mon" == day:
        n -= 6
    elif "tue" == day:
        n -= 5
    elif "wed" == day:
        n -= 4
    elif "thu" == day:
        n -= 3
    elif "fri" == day:
        n -= 2
    elif "sat" == day:
        n -= 1
    if n >= 0:
        print(int(n / 7 + 1), end = '')
    else:
        print("0", end = '')

FOR MORE TCS NQT CODING QUESTIONS 👉👉👉 CLICK HERE

1 thought on “TCS NQT COUNT SUNDAYS CODING PROBLEM”

  1. Pingback: TCS NQT CODING QUESTIONS WITH SOLUTIONS - GRAD JOB OPENINGS

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!