Epic Systems Interview Question
Software Engineer / Developerspublic class DateFormat {
public void dates(String date1, String date2) {
int dd1 = Integer.parseInt(date1.substring(0, 2));
int dd2 = Integer.parseInt(date2.substring(0, 2));
int mm1 = Integer.parseInt(date1.substring(2, 4)) - 1;
int mm2 = Integer.parseInt(date2.substring(2, 4)) - 1;
int yy1 = Integer.parseInt(date1.substring(4));
int yy2 = Integer.parseInt(date2.substring(4));
int[] noOfDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
boolean flag = false;
int count = 0;
if (yy1 == yy2 && mm1 == mm2) {
System.out.println("count ::" + Math.abs(dd2 - dd1 + 1));
return;
}
for (int i = yy1; i <= yy2; i++) {
if ((yy1 % 4 == 0 && yy1 % 100 != 0) || (yy1 % 400 == 0)) {
noOfDays[1] = 29;
} else {
noOfDays[1] = 28;
}
for (int month = 0;; month++) {
if (!flag) {
while (month != mm1) {
month++;
}
flag = true;
count = noOfDays[mm1] - dd1;
System.out.println("count >>" + count);
continue;
}
if (month > 11) {
break;
}
if (i == yy2 && month == mm2) {
count += noOfDays[month] - dd2;
count++;
break;
}
count += noOfDays[month];
System.out.println("count >>" + count);
}
}
System.out.println("count :" + count);
}
public boolean palindrome(String mmdd, String year) {
return true;
}
public static void main(String[] args) {
// TODO code application logic here
DateFormat date = new DateFormat();
date.dates("01012015", "31012015");
}
}
import java.util.*;
public class DateDifference {
public static void main(String args[]){
DateDifference difference = new DateDifference();
}
DateDifference() {
Calendar cal1 = new GregorianCalendar();
Calendar cal2 = new GregorianCalendar();
cal1.set(2008, 8, 1);
cal2.set(2008, 9, 31);
System.out.println("Days= "+daysBetween(cal1.getTime(),cal2.getTime()));
}
public int daysBetween(Date d1, Date d2){
return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
}
}
Correct me if I am wrong...
and I think it works only for e>b...
Since the Date in the question starts from Jan 1, it shouldnt be a prob..
#include<stdio.h>
void main(int args)
{
int a=27,b=5,c=2010;
int d=27,e=8,f=2011,day,g,h,i,year;
int days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int sum=0;
for(i=b+1;i<e;i++)
{
sum=sum+days[i];
}
year=(f-c)*365;
g=(days[b]-a)+d+sum+year;
printf("%d",g);
}
Correct me if I am wrong...
and I think it works only for e>b...
Since the Date in the question starts from Jan 1, it shouldnt be a prob..
#include<stdio.h>
void main(int args)
{
int a=27,b=5,c=2010;
int d=27,e=8,f=2011,day,g,h,i,year;
int days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int sum=0;
for(i=b+1;i<e;i++)
{
sum=sum+days[i];
}
year=(f-c)*365;
g=(days[b]-a)+d+sum+year;
printf("%d",g);
}
aaaaahh... sooooo close!!!
but u forgot to check to see if the end date's also in Jan, like if the end date's Jan 20th... just add an if-condition before u calc 'g', and u should be fine :)
#include<stdio.h>
int main()
{
int year,day,month,i,y_diff,m_diff,d_diff;
year=2011;
month=02;
day=13;
y_diff=year-2001;
y_diff=y_diff*365;
printf("The number of years between the given date and 2001 is %d\n",y_diff);
m_diff=month-01;
printf("the number of months difference is %d",m_diff);
for(i=1;i<=m_diff;i++)
{
if(i==2)
day=day+28;
else if(i==8)
day=day+31;
else if((i/2)==0)
day=day+30;
else
day=day+31;
}
day=day+y_diff+1;
printf("The number of days between the dates is %d",day);
}
I am a novice in programming. Hope my submission is useful
#include <iostream>
#include <string>
using namespace std;
/* Function declarations */
int calculateDays(int month, bool isLeap);
bool isLeapYear(int year);
/* Function definitions */
int calculateDays(int month, bool isLeap)
{
switch(month)
{
case 1: return 31;
break;
case 2: {if(isLeap) return 29;
else return 28;}
break;
case 3: return 31;
break;
case 4: return 30;
break;
case 5: return 31;
break;
case 6: return 30;
break;
case 7: return 31;
break;
case 8: return 31;
break;
case 9: return 30;
break;
case 10: return 31;
break;
case 11: return 30;
break;
case 12: return 31;
break;
default : return -1;
break;
}
}
bool isLeapYear(int year)
{
if(year%400==0)
return true;
else if(year%100==0)
return false;
else if(year%4==0)
return true;
else
return false;
}
int main()
{
char key;
int month,date,year;
int totalDays=0;
cout<<"Enter month- date - year"<<endl;
cin>> month;
cin>>date;
cin>>year;
bool isLeap=isLeapYear(year);
if(month-1!=0)
{
for(int i=1; i<=(month-1); i++)
{
totalDays+=calculateDays(i,isLeap);
}
totalDays+=date;
}
else
totalDays+=date;
cout<<"total days ="<<totalDays<<endl;
cin>>key;
return 0;
}
/*
* main.cpp
*
* Created on: Feb 26, 2011
* Author: Oshamajik
*
*/
// Here is a C++ version
// Comments are welcome
#include<iostream>
#include<string.h>
#include <stdlib.h>
using namespace std;
int main()
{
int myArray [3] ={1, 1, 2010}; // Given Date
char str[16];
bool userDate = false;
int dd, mm, yy;
int leapYearAdjst = 0; // Adjustment for leap years in between
int numDays; // This variable will count the no of days in between
do
{
cout << "Enter the date in the format: mm/dd/yyyy\t";
cin.get(str, 16); // Get user date
char * splitter; // Pointer to Split
int dates[10]; // Array to hold the dates
int i = 0;
splitter = strtok (str, "/"); // Split string into tokens (first call)
while (splitter != NULL)
{
dates[i] = atoi (splitter); // Convert char to int and store in dates
splitter = strtok (NULL, "/"); // Split string into tokens
i++;
}
// Now the array dates has
dd = dates[1]; // date in the 1st position
mm = dates[0]; // month in the 2nd position
yy = dates[2]; // year in the 3rd position
if ((mm >= 0 && mm <= 12) && (dd >= 0 && dd <= 31) && (yy >= 2010))
userDate = true;
else
{
cout << "Sorry that was a wrong entry!\n\n";
cin.clear();
cin.ignore(16, '\n');
}
}while (!userDate);
if (yy == 2010)
numDays = 0;
else
numDays = (yy - 2010) * 365; // Simply Years * 365 days
for (int j = 2010; j <= yy; j++) // Check for leap years in between
{
if ((j % 400 == 0) | (j % 4 == 0 && j % 100 != 0))
{
leapYearAdjst++;
if (j == yy && !(mm > 2))
// It has to be after February for that year to count
leapYearAdjst--;
}
}
switch (mm) // Series of switch statements
{
case 1:
numDays += dd - myArray[1];
break;
case 2:
numDays += 31 + dd - myArray[1];
break;
case 3:
numDays += 31 + 28 + dd - myArray[1];
break;
case 4:
numDays += 31*(2) + 28 + dd - myArray[1];
break;
case 5:
numDays += 31*(2) + 28 + 30 + dd - myArray[1];
break;
case 6:
numDays += 31*(3) + 28 + 30 + dd - myArray[1];
break;
case 7:
numDays += 31*(3) + 28 + 30*(2) + dd - myArray[1];
break;
case 8:
numDays += 31*(4) + 28 + 30*(2) + dd - myArray[1];
break;
case 9:
numDays += 31*(5) + 28 + 30*(2) + dd - myArray[1];
break;
case 10:
numDays += 31*(5) + 28 + 30*(3) + dd - myArray[1];
break;
case 11:
numDays += 31*(6) + 28 + 30*(3) + dd - myArray[1];
break;
case 12:
numDays += 31*(6) + 28 + 30*(4) + dd - myArray[1];
break;
}
// Finally the output
cout << "\nNo of days in between " << mm << "/" << dd << "/" << yy << " and 1/1/2010 is " << (numDays + leapYearAdjst) << ".\n";
}
Coming straight from the same man again... Njoi n Cheers!!! Reliability 101%
#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#define MAX 50
int main()
{
char str[MAX];
char month[MAX];
char date[MAX];
int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int num_of_days=0;
int i=0,j=0,k=0;
int flag=0;
printf("\nEnter the date (if you want to enter 5th June type 06-05): ");
gets(str);
//Function to split the string into month and date despite user typing more than one "-" or giving unwanted spaces
while(str[i]!='\0')
{
if(isalnum(str[i]))
{
if(flag==1)
{
date[k]=str[i];
k++;
}
if(flag==0)
{
month[j]=str[i];
j++;
}
i++;
}
if((str[i]=='-' || str[i]==' '))
{
if(j>0)
flag=1;
i++;
}
}
date[k]='\0';
month[j]='\0';
/*printf("\n%s",str);
printf("\n%s",month);
printf("\t%d",atoi(month));
printf("\n%s",date);*/
i=1;
for(i=1;i<atoi(month);i++)
num_of_days+=days[i];
num_of_days+=atoi(date);
printf("\n\nThe number of days between 01-01 and %s-%s (inclusive) are - %d",month,date,num_of_days);
getch();
return 0;
}
// considering only 28 days in feb
initial_date = 01;
initial_month = 01;
initial_year = 2011; // lets say
target_date //
target_month //
target_year //
days = 0;
days += (target_year - initial_year )*365;
day = {0,31,28,31,30,31,30,31,31,30,31,30,31};
while(target_month > initial_month)
{
days += day[initial_month];
initial_month++;
}
days += target_date -01;
cout<<days;
how about this (in c#)
static void Main(string[] args)
{
Console.WriteLine("Please enter a date in the Given Format: mm/dd/yyyy");
String date = Console.ReadLine();
String[] splitDate = date.Split('/');
int month = Int16.Parse(splitDate[0]);
int day = Int16.Parse(splitDate[1]);
int year = Int16.Parse(splitDate[2]);
//constraints..
if ((month <= 0) || (month > 12))
Console.WriteLine("Please enter a valid month");
else if ((day <= 0) || (day > 31))
Console.WriteLine("Please enter a valid day");
else if (year <= 0)
Console.WriteLine("Please enter a valid year");
//we can add some more constraints .. such as february has a maximum of 29 days in a leap year
else
dayCalculator(month, day, year);
Console.ReadLine();
}
public static void dayCalculator(int m, int d, int y)
{
int[] daysOfmonth;
int days = 0;
//decide number of days in February
if ((y % 4 == 0) && ((y % 100 != 0) || (y % 400 == 0)))
{
daysOfmonth = new int[] {31,29,31,30,31,30,31,31,30,31,30,31 };
}
else daysOfmonth = new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
for (int i = 1; i < m; i++)
{
days = days + daysOfmonth[i - 1];
}
days = days + d;
Console.WriteLine("total days: " + days);
}
Correct me if I am wrong!!!!
int main(){
int daysinmonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
string date,day,month;
cout<<"Enter current date: (dd-mm)";
cin>>date;
day.append(date,0,2);
month.append(date,3,4);
int ndays = atoi(day.c_str());
int nmonths = atoi(month.c_str());
int numberofdays = daysinmonth[nmonths-1]-ndays;
while (nmonths<12){
numberofdays += daysinmonth[nmonths];
nmonths++;
}
cout<<"Total number of days remaining in the current month is: " << numberofdays << endl;
return 0;
}
int main(){
int daysinmonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
string date, day, month, year;
cout<<"Enter current date: (dd-mm-yyyy)";
cin>>date;
day.append(date,0,2);
month.append(date,3,4);
year.append(date,6,9);
int ndays = atoi(day.c_str());
int nmonths = atoi(month.c_str());
int nyears = atoi(year.c_str());
if (nyears%4==0){
daysinmonth[1]=29;
}
int numberofdays = daysinmonth[nmonths-1]-ndays;
while (nmonths<12){
numberofdays += daysinmonth[nmonths];
nmonths++;
}
cout<<"Total number of days remaining in the current month is: " << numberofdays << endl;
return 0;
}
<pre lang="" line="1" title="CodeMonkey34231" class="run-this">import java.util.Calendar;
class DateDiff {
public static void main(String[] args) {
// Instances of Calendars
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
try{ //Check for null inputs,input of the form dd/mm/yyyy or wrong user input
if(args[0]!="" && args[1]!="" && args[2]!= "" && args[3]!="" && args[4]!="" && args[5]!="")
{
int a = args[0].length();
int b = args[1].length();
int c = args[2].length();
int d = args[3].length();
int e = args[4].length();
int f = args[5].length();
if((a >=1 && a<=2 )&& (b >=1 && b<=2 )&& (c >=1 && c<=4 )&& (d >=1 && d<=2 )&& (e >=1 && e<=2 )&& (f >=1 && f<=4 ))
{
int date1 = Integer.parseInt(args[0]);
int month1 = Integer.parseInt(args[1]);
int year1 = Integer.parseInt(args[2]);
int date2 = Integer.parseInt(args[3]);
int month2 = Integer.parseInt(args[4]);
int year2 = Integer.parseInt(args[5]);
long diff;
// Condition that checks whether
// date is within 1 to 31
// month is within 1 to 12
// year is within 1 to 9999
if( (date1>=1 && date1<=31)&& (date2>=1 && date2<=31)&&(month1>=1 && month1<=12)&& (month2>=1 && month2<=12)&&(year1>=1 && year1<=9999)&& (year2>=1 && year2<=9999)) {
// Set date for calendar field (year,month,date)
cal1.set(year1, month1, date1);
cal2.set(year2, month2, date2);
// Get the represented date in milliseconds
long millisec1 = cal1.getTimeInMillis();
long millisec2 = cal2.getTimeInMillis();
if(millisec2>millisec1){
// Calculate difference in milliseconds
diff = millisec2 - millisec1;
}
else{
diff = millisec1-millisec2;
}
// Calculate difference in days
long Days = diff / (24 * 60 * 60 * 1000);
System.out.println("No of days between two dates: " + Days + " days.");
}
else
{
System.err.println("Please make sure that the input is of the form dd/mm/yyyy. ");
System.err.println(" Date input may have exceeded the bounds(1-31) or");
System.err.println("Month seems less than 1 or greater than 12 or");
System.err.println("Year input may be less than 1 or greater than 9999");
}
}
else
{
System.err.println("Input must be of the form dd/mm/yyyy. Please make sure!");
}
}
}
catch(Exception e)
{
System.err.println("Error in the input - one of the inputs is null. Please verify");
}
}
}</pre><pre title="CodeMonkey34231" input="yes">
02 06 2010
08 07 2011</pre>
/*Take a date from the user in format mm/dd/yyyy.
Get the number of days between the 1st January of entered date and the entered date.
So if the user enters 23march2010. Print the number of days between 1 January 2010 and 23 March 2010.
*/
#include<iostream>
#include<stdio.h>
using namespace std;
void findday(unsigned int mm,unsigned int dd,unsigned int yyyy);
int main()
{
findday(7,13,1991);
findday(7,14,1994);
//system("PAUSE");
return 0;
}
void findday(unsigned int mm,unsigned int dd,unsigned int yyyy)
{
unsigned int mnth[]={31,28,31,30,31,30,31,31,30,31,30,31};
unsigned int i,days=0;
printf("Entered date is mm-dd-yyyy :%d-%d-%d",mm,dd,yyyy);
//check if its leap year
/*logic is its leap year if its divisible by both 4 and 100
or its divisible by 4 but not by hundered*/
if(yyyy%400 ==0 || (yyyy%100 != 0 && yyyy%4 == 0))
mnth[1]=29;
if(((mm>0 )&&(mm<12))&&(dd<=mnth[mm-1]))
{
for(i=0;i<mm-1;i++)
days+=mnth[i];
days+=dd;
printf("\nNo of days %d",days);
}
else
printf("\nInvalid date");
}
int year=2012;
int month=2;
int day=29;
int noDays = 0;
int normal[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int leap[] = {0,31,29,31,30,31,30,31,31,30,31,30,31};
if (month != 1) {
for (int i = 0; i < month; i++) {
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
// if leap year
System.out.println("leap");
noDays = noDays + leap[i];
} else {
noDays = noDays + normal[i];
}
}
noDays += day;
System.out.println(noDays);
}
else{
System.out.println(day);
}
#include<iostream>
using namespace std;
struct date
{
int d;
int m;
int y;
};
int noOfDays(date d1,date d2);
int main()
{
date d1;
date d2;
cout<<"Enter Date -1:"<<endl;
cin>>d1.d;
cin>>d1.m;
cin>>d1.y;
cout<<"Enter Date -2:"<<endl;
cin>>d2.d;
cin>>d2.m;
cin>>d2.y;
cout<<noOfDays(d1,d2);
}
int noOfDays(date d1,date d2)
{
int day=0;
//To calculate days due to no of years
for(int i=d1.y+1;i<d2.y;i++)
{
if(i%4==0)
day=day+366;
else
day=day+365;
}
//To calculate days due to no of months
if(d1.y==d2.y)
{
for(int i=d1.m+1;i<d2.m;i++)
{
switch(i)
{
case 2:
if(d1.y%4==0)
day=day+29;
else
day=day+28;
break;
case 9:
day=day+30;
break;
case 4:
day=day+30;
break;
case 6:
day=day+30;
break;
case 11:
day=day+30;
break;
default:
day=day+31;
}
}
}
else
{
for(int i=d1.m+1;i<=12;i++)
{
switch(i)
{
case 2:
if(d1.y%4==0)
day=day+29;
else
day=day+28;
break;
case 9:
day=day+30;
break;
case 4:
day=day+30;
break;
case 6:
day=day+30;
break;
case 11:
day=day+30;
break;
default:
day=day+31;
}
}
for(int i=1;i<d2.m;i++)
{
switch(i)
{
case 2:
if(d2.y%4==0)
day=day+29;
else
day=day+28;
break;
case 9:
day=day+30;
break;
case 4:
day=day+30;
break;
case 6:
day=day+30;
break;
case 11:
day=day+30;
break;
default:
day=day+31;
}
}
}
//To calculate no of days.
if(d1.m==d2.m && d1.y==d2.y)
{
day=day+(d2.d-d1.d+1);
}
else
{
if(d1.m==2||d1.m==4||d1.m==6||d1.m==11)
day=day+(30-d1.d+1);
else
day=day+(31-d1.d+1);
day=day+d2.d;
}
return day;
}
import java.util.*;
public class daysBetween {
public static void main(String args[]){
System.out.println("year");
Scanner sc=new Scanner(System.in);
int y=sc.nextInt();
System.out.println("month");
int m=sc.nextInt();
System.out.println("day");
int d=sc.nextInt();
System.out.println("date is:"+y+"/"+m+"/"+d);
int sum=0;
int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(m==1){
sum=d;
}else{
for(int i=0;i<m-1;i++)
{
sum=sum+days[i];
}
sum=sum+d; }
if(y%400==0||(y%4==0&&y%100!=0)){
sum=sum+1;
}
System.out.print("The number of days are"+sum);
}
}
I have considered that the answer for 01 01 yyyy will be 0 and for 01 02 yyyy will be 1. That is I have excluded 1st Jan in the count and included the end date. Here is a working code in C++:
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
int ans=0, dd, mm, yyyy, i, mon[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
cin>>mm>>dd>>yyyy;
for (i=0; i<(mm-1); i++)
{
ans+=mon[i];
}
ans+=dd;
if (mm>2 &&(yyyy%400==0 || (yyyy%4==0 && yyyy%100!=0)))
{
ans+=1;
}
cout<<ans-1;
return 0;
}
package validpwdspoiledkey;
import java.util.ArrayList;
public class key {
public static void check(String op, String ip)
{
boolean flag = true;
ArrayList<Character> arl = new ArrayList<Character>();
char spoiledkey = '8';
char[] a = op.toCharArray();
for(int i=0; i<a.length ; i++){
if(a[i] != spoiledkey ){
arl.add(a[i]);
}
}
if(arl.size() != ip.length()){
flag = false;
} else {
for(int j=0;j<ip.length();j++){
if(flag){
if(arl.get(j) != ip.charAt(j)){
flag = false;
} else{
flag = true;
}
}
}
} if(flag){
System.out.println("Valid");
} else {
System.out.println("Invalid");
}
}
public static void main(String[] args){
check("1868486","164");
}
}
Works like a charm.
posted this is on the wrong link, this code is for this problem:
In 1-9 keypad one key is not working. If some one enters a password then not working key will not be entered. You have given expected password and entered password. Check that entered password is valid or not Ex: entered 164, expected 18684
public class DateDifference1 {
public static void main(String[] args) {
int normal[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int leap[] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// Start date is always 1st of Jan and the year is same as the input year.
// Hence here its 1/1/2016
//2016 is leap year.
int year = 2016;
int month = 7;
int day = 29;
int numberOfDays = 0;
if (month == 1) {
System.out.println("Number of days: " + day);
} else {
for (int i = 1; i < month; i++) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
numberOfDays = numberOfDays + leap[i];
} else {
numberOfDays = numberOfDays + normal[i];
}
}
numberOfDays = numberOfDays + day;
System.out.println("Number of days: " + numberOfDays);
}
}
}
if month number is less than or equal to 7, it will keep changing between 31 and 30, after 7th month it changes between 30 and 31 (except for feb)
days in month:
31 (28/29) 31 30 31 30 31 31 30 31 30 31
#include <iostream>
using namespace std;
int main()
{
int i, d, m, y, sum;
cin>>d>>m>>y;
bool leap = (y%4 == 0 && (y%100 != 0 || y%400 == 0));
sum = d;
for(i=1; i<m; i++) {
if(i == 2)
sum+=(28 + leap);
else
sum+=(30 + ((i > 7) + i%2)%2);
}
cout<<sum<<endl;
return 0;
}
/*Find no. of days between Jan1st and entered date*/
#include <iostream>
int findDays(int year, int month, int day)
{
int monthSize[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int leap =(year%4 == 0 && year%100 != 0)||(year%400 == 0);
// initialize the sum of days
int sum = 0;
// for days in Jan
if (month == 1) {
sum = day;
}
// for dates in Feb
if(month == 2) {
sum = monthSize[0]+day;
}
// for month starting from Mar
else {
// count the previous days excluding the current month
for(int i = 0; i<month-1;++i) {
sum += monthSize[i];
}
sum += day+leap;
}
return sum;
}
int main()
{
int result = findDays(2000, 4, 1);
std::cout << result<<"\n";
return 0;
}
/*Find no. of days between Jan1st and entered date*/
#include <iostream>
int findDays(int year, int month, int day)
{
int monthSize[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int leap =(year%4 == 0 && year%100 != 0)||(year%400 == 0);
// initialize the sum of days
int sum = 0;
// for days in Jan
if (month == 1) {
sum = day;
}
// for dates in Feb
if(month == 2) {
sum = monthSize[0]+day;
}
// for month starting from Mar
else {
// count the previous days excluding the current month
for(int i = 0; i<month-1;++i) {
sum += monthSize[i];
}
sum += day+leap;
}
return sum;
}
int main()
{
int result = findDays(2000, 4, 1);
std::cout << result<<"\n";
return 0;
}
/*Find no. of days between Jan1st and entered date*/
#include <iostream>
int findDays(int year, int month, int day)
{
int monthSize[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int leap =(year%4 == 0 && year%100 != 0)||(year%400 == 0);
// initialize the sum of days
int sum = 0;
// for days in Jan
if (month == 1) {
sum = day;
}
// for dates in Feb
if(month == 2) {
sum = monthSize[0]+day;
}
// for month starting from Mar
else {
// count the previous days excluding the current month
for(int i = 0; i<month-1;++i) {
sum += monthSize[i];
}
sum += day+leap;
}
return sum;
}
int main()
{
int result = findDays(2000, 4, 1);
std::cout << result<<"\n";
return 0;
}
#include <iostream>
int findDays(int year, int month, int day)
{
int monthSize[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int leap =(year%4 == 0 && year%100 != 0)||(year%400 == 0);
// initialize the sum of days
int sum = 0;
// for days in Jan
if (month == 1) {
sum = day;
}
// for dates in Feb
if(month == 2) {
sum = monthSize[0]+day;
}
// for month starting from Mar
else {
// count the previous days excluding the current month
for(int i = 0; i<month-1;++i) {
sum += monthSize[i];
}
sum += day+leap;
}
return sum;
}
int main()
{
int result = findDays(2000, 4, 1);
std::cout << result<<"\n";
return 0;
}
#include <iostream>
int findDays(int year, int month, int day)
{
int monthSize[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int leap =(year%4 == 0 && year%100 != 0)||(year%400 == 0);
// initialize the sum of days
int sum = 0;
// for days in Jan
if (month == 1) {
sum = day;
}
// for dates in Feb
if(month == 2) {
sum = monthSize[0]+day;
}
// for month starting from Mar
else {
// count the previous days excluding the current month
for(int i = 0; i<month-1;++i) {
sum += monthSize[i];
}
sum += day+leap;
}
return sum;
}
int main()
{
int result = findDays(2000, 4, 1);
std::cout << result<<"\n";
return 0;
}
- chirag April 15, 2011