0% found this document useful (0 votes)
3 views

Assignment No

Uploaded by

ekayani566
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment No

Uploaded by

ekayani566
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment No.

01

Student id: BC230404271


Name: kinza Rafaqat
CS201: Introduction to Programming
Date: 07.11.24
Problem Statement:
Write a program that outputs your name and ID, checks if a year (derived by dropping the last
two digits from right and then extract the next four digits from the numeric part of the studentID
as explained below in step-by-step procedure) is a leap year or not, and then displays how many
days there are in the month (take input from user).
Note: If the year is divisible by 4 and not divisible by 100 or divisible by 400, print that it is a
leap year; otherwise, print that it is not a leap year.

Step by step procedure:

1. Name and ID Output:


o The program should begin by outputting the student's name and VUID. Make sure the
name and VUID are displayed at the very beginning of the output.
o Store the numeric part of VUID in a variable name studentID.
2. Leap Year Check:
o Instead of prompting the user for input, drop the last two digits from right and then
extract the next four digits from the numeric part of the studentID and use it as the year
to check whether it is a leap year.
o See the following examples:
1. For VUID bc123450000, 4500 will be the year. You will drop the last two digits
in the red and will take 4500 as year.
2. For VUID bc231201978, 2019 will be the year. You will drop the last two digits
in the red and will take 2019 as year
3. For VUID bc223489124, 4891 will be the year. You will drop the last two digits
in the red and will take 4891 as year
3. Month Selection and Days Calculation:
o Take input from the user and determine the number of days, if month is invalid (i-e: not
between 1 and 12) take appropriate action using if/else statement.
o Use switch statements to determine how many days there are in that month. February
should be handled differently for leap years (29 days) and non-leap years (28 days).

SOLUTION:

CODE
#include <iostream>
#include <string>

using namespace std;

int stringToInt(const string& str)


{ int number = 0;
for (size_t i = 0; i < str.length(); i++)
{ char c = str[i]; if (c >= '0'
&& c <= '9') { number = number
* 10 + (c - '0');
} else {
return -1;
}
}
return number;
}

int main() {
string name = "Sarim (VU Answer)"; // Replace with your name
string void = "BC230506781"; // Replace with your VUID string
studentID = vuid.substr(2); // Extract numeric part of VUID
cout << "Name: " << name << endl;
cout << "VUID: " << void << endl;

int idNumber = stringToInt(studentID);


int year = (idNumber / 100) % 10000;
bool isLeapYear = (year % 4 == 0 && (year
% 100 != 0 || year % 400 == 0)); if
(isLeapYear) {
cout << "The year " << year << " is a leap year." << endl;
} else {
cout << "The year " << year << " is not a leap year." << endl;
}

int month;
while (true) {
cout << "Enter the month (1-12), or -1 to stop: “;
cin >> month;

if (month == -1) {
break;
}

int days;
string monthName;

switch (month) {
case 1:
days = 31;
monthName = "January";
break; case 2:
days = isLeapYear ? 29 : 28;
monthName = "February";
break; case 3:
days = 31; monthName
= "March";
break; case 4:
days = 30;
monthName = "April";
break; case 5:
days = 31;
monthName = "May";
break; case 6:
days = 30; monthName
= "June"; break;
case 7: days = 31;
monthName = "July";
break; case 8:
days = 31; monthName
= "August";
break;
case 9:
days = 30;
monthName = "September";
break;
case 10:
days = 31;
monthName = "October";
break;
case 11:
days = 30;
monthName = "November";
break;
case 12:
days = 31;
monthName = "December";
break;
default:
cout << "Invalid month!" << endl;
continue;
}
cout << "Month " << month << " - " << monthName << " has " << days << " days." <<
endl;
}

return 0;
}

OUTPUT:

You might also like