Syvum Home Page

Home > Discussions > For all activities

Discussion topic: Contributed Answer/Explanation to Q. 3

Viewing messages

To go to the homepage of this topic, click here.
Page 1 of 1
From: sasmadhjReply 1 of 19Reply
Subject: Contributed Answer/Explanation to Q. 3
   read a single staterment .arrange the words in ascending order  write a
   program in java

Posted at: Mon Feb 1 14:03:39 2010 (GMT)

From: sandeep1445Reply 2 of 19Reply
Subject: Contributed Answer/Explanation to Q. 2
   programme code in java

Posted at: Thu Feb 4 13:54:13 2010 (GMT)

From: mistressofmischiefReply 3 of 19Reply
Subject: Contributed Answer/Explanation to Q. 1
   
   class date<br />
   {<br />
   public void main(int dn,int y,int N)<br />
   {<br />
   String s[]={"st","nd","rd","th"};<br />
   String
   b[]={"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPT
   EMBER","OCTOBER","NOVEMBER","DECEMBER"};<br />
   while(dn!=0)<br />
   {<br />
   if(y%4==0)<br />
   {<br />
   int a[]={31,29,31,30,31,30,31,31,30,31,30,31};<br />
   int s1=31,i=1,d;<br />
   String p="";<br />
   while(s1<dn)<br />
   {<br />
   s1=s1+a[i];<br />
   ++i;<br />
   }<br />
   s1=s1-a[i-1];<br />
   d=dn-s1;<br />
   int m=i-1;<br />
   if(d==1||d==21||d==31)<br />
   p=s[0];<br />
   else if(d==2||d==22)<br />
   p=s[1];<br />
   else if(d==3)<br />
   p=s[2];<br />
   else<br />
   p=s[3];<br />
   System.out.println(+d+p+" "+b[m]+" "+y);<br />
   int n=dn+N;<br />
   if(n>366)<br />
   {<br />
   n=n-366;<br />
   y++;<br />
   }<br />
   s1=31;<br />
   i=1;<br />
   d=0;<br />
   String k="";<br />
   while(s1<n)<br />
   {<br />
   s1=s1+a[i];<br />
   ++i;<br />
   }<br />
   s1=s1-a[i-1];<br />
   d=n-s1;<br />
   m=i-1;<br />
   if(d==1||d==21||d==31)<br />
   k=s[0];<br />
   else if(d==2||d==22)<br />
   k=s[1];<br />
   else if(d==3)<br />
   k=s[2];<br />
   else<br />
   k=s[3];<br />
   if(m>11)<br />
   k=s[4];<br />
   System.out.println("DATE AFTER "+N+" DAYS IS "+d+k+" "+b[m]+" "+y);<br
   />
   }<br />
   else<br />
   if(y%4!=0)<br />
   {<br />
   int a[]={31,29,31,30,31,30,31,31,30,31,30,31};<br />
   int s1=31,i=1,d;<br />
   String p="";<br />
   while(s1<dn)<br />
   {<br />
   s1=s1+a[i];<br />
   ++i;<br />
   }<br />
   s1=s1-a[i-1];<br />
   d=dn-s1;<br />
   int m=i-1;<br />
   if(d==1||d==21||d==31)<br />
   p=s[0];<br />
   else if(d==2||d==22)<br />
   p=s[1];<br />
   else if(d==3)<br />
   p=s[2];<br />
   else<br />
   p=s[3];<br />
   System.out.println(+d+p+" "+b[m]+" "+y);<br />
   int n=dn+N;<br />
   if(n>365)<br />
   {<br />
   n=n-365;<br />
   y++;<br />
   }<br />
   s1=31;<br />
   i=1;<br />
   d=0;<br />
   String k="";<br />
   while(s1<n)<br />
   {<br />
   s1=s1+a[i];<br />
   ++i;<br />
   }<br />
   s1=s1-a[i-1];<br />
   d=n-s1-1;<br />
   m=i-1;<br />
   if(d==1||d==21||d==31)<br />
   k=s[0];<br />
   else if(d==2||d==22)<br />
   k=s[1];<br />
   else if(d==3)<br />
   k=s[2];<br />
   else<br />
   k=s[3];<br />
   if(m>11)<br />
   k=s[4];<br />
   System.out.println("DATE AFTER "+N+" DAYS IS "+(d+1)+k+" "+b[m]+"
   "+y);<br />
   }<br />
   break;<br />
   }<br />
   System.out.println("Invalid date.Try Again");<br />
   }<br />
   }<br />
   <br />

Posted at: Sun Feb 21 15:19:48 2010 (GMT)

From: prashissReply 4 of 19Reply
Subject: Contributed Answer/Explanation to Q. 2
   follow the instructions given along with the coding given below.
   import java.io.*;
   class matrix
   {
       public static void main()throws IOException
       {
	   int m,n,i,j;
	   BufferedReader br=new BufferedReader(new
   InputStreamReader(System.in));
	   System.out.println("Enter the values of m and n:");
	   m=Integer.parseInt(br.readLine());
	   n=Integer.parseInt(br.readLine());
	   int M[][]=new int[m][n];
	   System.out.println("Enter the elements to be entered into the
   matrix:");
   for(i=0;i<m;i++)
	   {
	       for(j=0;j<n;j++)
	       {
		   M[i][j]=Integer.parseInt(br.readLine());
	       }
	   }
	   System.out.println("Original Matrix:");
   for(i=0;i<m;i++)
	   {
	       for(j=0;j<n;j++)
	       {
		   System.out.print(M[i][j]+" ");
	       }
	       System.out.println();
	   }
	   int no=(m*2)+(n*2)-4;
	   int e[]=new int[no],k=0,t=0;
	   i=0;
   for(j=0;j<n;j++)
   {
	       e[k]=M[i][j];
	       k++;
	   }
	   i=m-1;
	   for(j=0;j<n;j++)
	   {
	       e[k]=M[i][j];
	       k++;
	   }
	   j=0;
	   for(i=1;i<m-1;i++)
	   {
	       e[k]=M[i][j];
	       k++;
	   }
	   j=n-1;
	   for(i=1;i<m-1;i++)
	   {
	       e[k]=M[i][j];
	       k++;
	   }
   for(i=0;i<k;i++)
	   {
	       for(j=0;j<k-1;j++)
	       {
		   if(e[j]>e[j+1])
		   {
		       t=e[j];
		       e[j]=e[j+1];
		       e[j+1]=t;
		   }
	       }
	   }
	   int sum=0;
   for(i=0;i<k;i++)
	   {
	       sum=sum+e[i];
	   }
	   i=0;
	   k=0;
   for(j=0;j<n;j++)
	   {
	       M[i][j]=e[k];
	       k++;
	   }
	   j=n-1;
	   for(i=1;i<m;i++)
	   {
	       M[i][j]=e[k];
	       k++;
	   }
	   i=m-1;
	   for(j=n-2;j>=0;j--)
	   {
	       M[i][j]=e[k];
	       k++;
	   }
	   j=0;
	   for(i=m-2;i>0;i--)
	   {
	       M[i][j]=e[k];
	       k++;
	   }
	   System.out.println("Rearranged Matrix:");
   for(i=0;i<m;i++)
	   {
	       for(j=0;j<n;j++)
	       {
		   System.out.print(M[i][j]+" ");
	       }
	       System.out.println();
	   }
	   System.out.println("Boundary Elements:");
   for(i=0;i<m;i++)
	   {
	       if(i==0||i==(m-1))
	       {
		   for(j=0;j<n;j++)
		   {
		       System.out.print(M[i][j]+" ");
		   }
		   System.out.println();
	       }
	       else
	       {
		   for(j=0;j<n;j++)
		   {
		       if(j==0||j==(n-1))
		       System.out.print(M[i][j]+" ");
		       else
		       System.out.print("	");
		   }
		   System.out.println();
	       }
	   }
	   System.out.println("Sum of boundary elements:"+sum);
       }
   }

Posted at: Sun Jan 16 12:11:11 2011 (GMT)

From: rushilpaulReply 5 of 19Reply         View replies (3)
Subject: Contributed Answer/Explanation to Q. 1
   
   #include <iostream>
   #include <sstream>
   using namespace std;
   
   bool checkleap(int y);
   string printdate(int n, int y);
   
   bool checkleap(int y)
   {
       return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
   }
   
   string printdate(int n, int y)
   {
       int year = y, month=1, days=n;
       y = checkleap(y) ? 1 : 0;
               
       while( n > 0)
       {
           if(month >= 13)
           {
               y = checkleap(++year) ? 1 : 0;
               month = 1;
           }
           days = n;
           if(month == 2) if(y==1) n -= 29;
                          else n -= 28;
           else if(month <= 7) if(month % 2 == 1) n -= 31;
                               else n -= 30;
           else
           {
               if(month % 2 == 0) n -= 31;
               else n -= 30;
           }
           month++;
       }
       string months[] = { "January", "February",
   "March", "April", "May", "June",
   "July", "August", "September",
   "October", "November", "December" };
       string app = "th";
       if( days % 10 == 1) app = "st";
       else if(days % 10 == 2) app = "nd";
       else if(days % 10 == 3) app = "rd";
       stringstream val;
       val << days << app << " " <<
   months[month-2] << " " << year;
       return val.str();
   }
   
   int main()
   {
       int n,y,after;
       cout << "Enter the number of days, year and additional
   number of days: ";
       cin >> n >> y >> after;
       bool err = n < 1 || n > 366;
       if(!checkleap(y)) if( n > 365) err = true;
       if(err) { cout << "Incorrect input." << endl;
   return 1; }
       cout << "Current Date: \t" << printdate(n,y)
   << endl << "Date after " << after <<
   " days: " << printdate(n+after,y) << endl;
       return 0;
   }

Posted at: Tue Jan 17 20:04:33 2012 (GMT)

From: rushilpaulReply 6 of 19Reply         View replies (2)
Subject: Contributed Answer/Explanation to Q. 3
   Look at the code and follow the algorithm written after it
   
    #include <iostream>
   #include <sstream>
   using namespace std;
   
   bool checkleap(int y);
   string printdate(int n, int y);
   
   bool checkleap(int y)
   {
       return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
   }
   
   string printdate(int n, int y)
   {
       int year = y, month=1, days=n;
       y = checkleap(y) ? 1 : 0;
               
       while( n > 0)
       {
           if(month > 12)
           {
               y = checkleap(++year) ? 1 : 0;
               month = 1;
           }
           days = n;
           if(month == 2) if(y==1) n -= 29;
                          else n -= 28;
           else if(month <= 7) if(month % 2 == 1) n -= 31;
                               else n -= 30;
           else
           {
               if(month % 2 == 0) n -= 31;
               else n -= 30;
           }
           month++;
       }
       string months[] = { "January", "February",
   "March", "April", "May", "June",
   "July", "August", "September",
   "October", "November", "December" };
       string app = "th";
       if( days % 10 == 1) app = "st";
       else if(days % 10 == 2) app = "nd";
       else if(days % 10 == 3) app = "rd";
       stringstream val;
       val << days << app << " " <<
   months[month-2] << " " << year;
       return val.str();
   }
   
   int main()
   {
       int n,y,after;
       cout << "Enter the number of days, year and additional
   number of days: ";
       cin >> n >> y >> after;
       bool err = n < 1 || n > 366;
       if(!checkleap(y)) if( n > 365) err = true;
       if(err) { cout << "Incorrect input." << endl;
   return 1; }
       cout << "Current Date: \t" << printdate(n,y)
   << endl << "Date after " << after <<
   " days: " << printdate(n+after,y) << endl;
       return 0;
   }  // end of program 
   
   
    
   
   
   -----------------------------ALGORITHM--------------------------------------
   --------
   
   
   The first seven months of the year have 31 and 30 days alternately with an
   exception of February.
   
   
   That means for all m <= 7, if m is divisble by 2, then no. of days is 30,
   else 31. and if m = 2, then check for leap year.
   
   
   This is very simple: (y is divisble by 4 but not by 100) or (y is divisible
   by 400).
   
   
   Continuing, months from 8 to 12  have 31 and 30 days alternately. Again
   check if m is divisible by 2. If yes then its 31 else 30. (this time it gets
   reversed since Jul and Aug, both have 31 days)
   
   
   Keep on subtracting the number of days in each month from variable days
   (which was the input), and check whether the number of remaining days are
   <= 0.
   
   
   If yes, then break out of the loop and the last positive value of variable
   "days" will be the date. (assign another variable the value of n
   just before changing value of "days")
   
   
   For months, keep a counter which starts from 1.
   
   
   As soon as months reaches a value > 12, assign it 1, increment year, and
   check for leap year again. 
   
   
    
   
   
   Put all of this in a function and call it twice.
   
   
   The second time, call it giving number of days = earlier no. of days +
   second inputted no. of days.  printdate( days + after )

Posted at: Tue Jan 17 20:35:50 2012 (GMT)

From: rushilpaulReply 7 of 19Reply
Subject: Contributed Answer/Explanation to Q. 3
   Look at the code and follow the algorithm written after it
   
   #include 
   #include 
   using namespace std;
   
   bool checkleap(int y);
   string printdate(int n, int y);
   
   bool checkleap(int y)
   {
   return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
   }
   
   string printdate(int n, int y)
   {
   int year = y, month=1, days=n;
   y = checkleap(y) ? 1 : 0;
   
   while( n > 0)
   {
   if(month > 12)
   {
   y = checkleap(++year) ? 1 : 0;
   month = 1;
   }
   days = n;
   if(month == 2) if(y==1) n -= 29;
   else n -= 28;
   else if(month <= 7) if(month % 2 == 1) n -= 31;
   else n -= 30;
   else
   {
   if(month % 2 == 0) n -= 31;
   else n -= 30;
   }
   month++;
   }
   string months[] = { "January", "February",
   "March", "April", "May", "June",
   "July", "August", "September",
   "October", "November", "December" };
   string app = "th";
   if( days % 10 == 1) app = "st";
   else if(days % 10 == 2) app = "nd";
   else if(days % 10 == 3) app = "rd";
   stringstream val;
   val << days << app << " " <<
   months[month-2] << " " << year;
   return val.str();
   }
   
   int main()
   {
   int n,y,after;
   cout << "Enter the number of days, year and additional number of
   days: ";
   cin >> n >> y >> after;
   bool err = n < 1 || n > 366;
   if(!checkleap(y)) if( n > 365) err = true;
   if(err) { cout << "Incorrect input." << endl; return
   1; }
   cout << "Current Date: \t" << printdate(n,y) <<
   endl << "Date after " << after << " days:
   " << printdate(n+after,y) << endl;
   return 0;
   }  // end of program 
   
   
    
   
   
   -----------------------------ALGORITHM--------------------------------------
   --------
   
   
   The first seven months of the year have 31 and 30 days alternately with an
   exception of February.
   
   
   That means for all m <= 7, if m is divisble by 2, then no. of days is 30,
   else 31. and if m = 2, then check for leap year.
   
   
   This is very simple: (y is divisble by 4 but not by 100) or (y is divisible
   by 400).
   
   
   Continuing, months from 8 to 12  have 31 and 30 days alternately. Again
   check if m is divisible by 2. If yes then its 31 else 30. (this time it gets
   reversed since Jul and Aug, both have 31 days)
   
   
   Keep on subtracting the number of days in each month from variable days
   (which was the input), and check whether the number of remaining days are
   <= 0.
   
   
   If yes, then break out of the loop and the last positive value of variable
   "days" will be the date. (assign another variable the value of n
   just before changing value of "days")
   
   
   For months, keep a counter which starts from 1.
   
   
   As soon as months reaches a value > 12, assign it 1, increment year, and
   check for leap year again. 
   
   
    
   
   
   Put all of this in a function and call it twice.
   
   
   The second time, call it giving number of days = earlier no. of days +
   second inputted no. of days. i.e. printdate( days + after )

Posted at: Tue Jan 17 20:50:11 2012 (GMT)

From: rushilpaulReply 8 of 19Reply
Subject: Contributed Answer/Explanation to Q. 1
   
   
   #include 
   #include 
   using namespace std;
   
   bool checkleap(int y);
   string printdate(int n, int y);
   
   bool checkleap(int y)
   {
   return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
   }
   
   string printdate(int n, int y)
   {
   int year = y, month=1, days=n;
   y = checkleap(y) ? 1 : 0;
   
   while( n > 0)
   {
   if(month > 12)
   {
   y = checkleap(++year) ? 1 : 0;
   month = 1;
   }
   days = n;
   if(month == 2) if(y==1) n -= 29;
   else n -= 28;
   else if(month <= 7) if(month % 2 == 1) n -= 31;
   else n -= 30;
   else
   {
   if(month % 2 == 0) n -= 31;
   else n -= 30;
   }
   month++;
   }
   string months[] = { "January", "February",
   "March", "April", "May", 
   "June", "July", "August",
   "September", "October", "November",
   "December"
   };
   string app = "th";
   if( days % 10 == 1) app = "st";
   else if(days % 10 == 2) app = "nd";
   else if(days % 10 == 3) app = "rd";
   stringstream val;
   val << days << app << " " <<
   months[month-2] << " " << year;
   return val.str();
   }
   
   int main()
   {
   int n,y,after;
   cout << "Enter the number of days, year and additional number of
   days: ";
   cin >> n >> y >> after;
   bool err = n < 1 || n > 366;
   if(!checkleap(y)) if( n > 365) err = true;
   if(err) { cout << "Incorrect input." << endl; return
   1; }
   cout << "Current Date: \t" << printdate(n,y) <<
   endl 
   << "Date after " << after << " days: "
   << 
   printdate(n+after,y) << endl;
   return 0;
   }  // end of program 
   
   
    
   
   
   -----------------------------ALGORITHM--------------------------------------
   --------
   
   
   The first seven months of the year have 31 and 30 days alternately with an
   exception of February.
   
   
   That means for all m <= 7, if m is divisble by 2, then no. of days is 30,
   else 31. and if m = 2, then check for leap year.
   
   
   This is very simple: (y is divisble by 4 but not by 100) or (y is divisible
   by 400).
   
   
   Continuing, months from 8 to 12  have 31 and 30 days alternately. Again 
   check if m is divisible by 2. If yes then its 31 else 30. (this time it 
   gets reversed since Jul and Aug, both have 31 days)
   
   
   Keep on subtracting the number of days in each month from variable days 
   (which was the input), and check whether the number of remaining days 
   are <= 0.
   
   
   If yes, then break out of the loop and the last positive value of 
   variable "days" will be the date. (assign another variable the
   value of n
   just before changing value of "days")
   
   
   For months, keep a counter which starts from 1.
   
   
   As soon as months reaches a value > 12, assign it 1, increment year, and
   check for leap year again. 
   
   
    
   
   
   Put all of this in a function and call it twice.
   
   
   The second time, call it giving number of days = earlier no. of days + 
   second inputted no. of days. i.e. printdate( days + after )

Posted at: Tue Jan 17 20:52:21 2012 (GMT)

From: rushilpaulReply 9 of 19Reply
Subject: Contributed Answer/Explanation to Q. 1
   
   
   #include <iostream>
   #include <sstream>
   using namespace std;
   
   bool checkleap(int y);
   string printdate(int n, int y);
   
   bool checkleap(int y)
   {
   return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
   }
   
   string printdate(int n, int y)
   {
   int year = y, month=1, days=n;
   y = checkleap(y) ? 1 : 0;
   
   while( n > 0)
   {
   if(month > 12)
   {
   y = checkleap(++year) ? 1 : 0;
   month = 1;
   }
   days = n;
   if(month == 2) if(y==1) n -= 29;
   else n -= 28;
   else if(month <= 7) if(month % 2 == 1) n -= 31;
   else n -= 30;
   else
   {
   if(month % 2 == 0) n -= 31;
   else n -= 30;
   }
   month++;
   }
   string months[] = { "January", "February",
   "March", "April", "May", 
   "June", "July", "August",
   "September", "October", "November",
   "December"
   };
   string app = "th";
   if( days % 10 == 1) app = "st";
   else if(days % 10 == 2) app = "nd";
   else if(days % 10 == 3) app = "rd";
   stringstream val;
   val << days << app << " " <<
   months[month-2] << " " << year;
   return val.str();
   }
   
   int main()
   {
   int n,y,after;
   cout << "Enter the number of days, year and additional number of
   days: ";
   cin >> n >> y >> after;
   bool err = n < 1 || n > 366;
   if(!checkleap(y)) if( n > 365) err = true;
   if(err) { cout << "Incorrect input." << endl; return
   1; }
   cout << "Current Date: \t" << printdate(n,y) <<
   endl 
   << "Date after " << after << " days: "
   << 
   printdate(n+after,y) << endl;
   return 0;
   }  // end of program 
   
   
    
   
   
   -----------------------------ALGORITHM--------------------------------------
   --------
   
   
   The first seven months of the year have 31 and 30 days alternately with an
   exception of February.
   
   
   That means for all m <= 7, if m is divisble by 2, then no. of days is 30,
   else 31. and if m = 2, then check for leap year.
   
   
   This is very simple: (y is divisble by 4 but not by 100) or (y is divisible
   by 400).
   
   
   Continuing, months from 8 to 12  have 31 and 30 days alternately. Again 
   check if m is divisible by 2. If yes then its 31 else 30. (this time it 
   gets reversed since Jul and Aug, both have 31 days)
   
   
   Keep on subtracting the number of days in each month from variable days 
   (which was the input), and check whether the number of remaining days 
   are <= 0.
   
   
   If yes, then break out of the loop and the last positive value of 
   variable "days" will be the date. (assign another variable the
   value of n
   just before changing value of "days")
   
   
   For months, keep a counter which starts from 1.
   
   
   As soon as months reaches a value > 12, assign it 1, increment year, and
   check for leap year again. 
   
   
    
   
   
   Put all of this in a function and call it twice.
   
   
   The second time, call it giving number of days = earlier no. of days + 
   second inputted no. of days. i.e. printdate( days + after )

Posted at: Tue Jan 17 20:55:23 2012 (GMT)

From: rushilpaulReply 10 of 19Reply
Subject: Contributed Answer/Explanation to Q. 1
   
   
   #include "iostream"
   
   
   #include "sstream"
   using namespace std;
   
   bool checkleap(int y);
   string printdate(int n, int y);
   
   bool checkleap(int y)
   {
   return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
   }
   
   string printdate(int n, int y)
   {
   int year = y, month=1, days=n;
   y = checkleap(y) ? 1 : 0;
   
   while( n > 0)
   {
   if(month > 12)
   {
   y = checkleap(++year) ? 1 : 0;
   month = 1;
   }
   days = n;
   if(month == 2) if(y==1) n -= 29;
   else n -= 28;
   else if(month <= 7) if(month % 2 == 1) n -= 31;
   else n -= 30;
   else
   {
   if(month % 2 == 0) n -= 31;
   else n -= 30;
   }
   month++;
   }
   string months[] = { "January", "February",
   "March", "April", "May", 
   "June", "July", "August",
   "September", "October", "November",
   "December"
   };
   string app = "th";
   if( days % 10 == 1) app = "st";
   else if(days % 10 == 2) app = "nd";
   else if(days % 10 == 3) app = "rd";
   stringstream val;
   val << days << app << " " <<
   months[month-2] << " " << year;
   return val.str();
   }
   
   int main()
   {
   int n,y,after;
   cout << "Enter the number of days, year and additional number of
   days: ";
   cin >> n >> y >> after;
   bool err = n < 1 || n > 366;
   if(!checkleap(y)) if( n > 365) err = true;
   if(err) { cout << "Incorrect input." << endl; return
   1; }
   cout << "Current Date: \t" << printdate(n,y) <<
   endl 
   << "Date after " << after << " days: "
   << 
   printdate(n+after,y) << endl;
   return 0;
   }  // end of program 
   
   
    
   
   
   -----------------------------ALGORITHM--------------------------------------
   --------
   
   
   The first seven months of the year have 31 and 30 days alternately with an
   exception of February.
   
   
   That means for all m <= 7, if m is divisble by 2, then no. of days is 30,
   else 31. and if m = 2, then check for leap year.
   
   
   This is very simple: (y is divisble by 4 but not by 100) or (y is divisible
   by 400).
   
   
   Continuing, months from 8 to 12  have 31 and 30 days alternately. Again 
   check if m is divisible by 2. If yes then its 31 else 30. (this time it 
   gets reversed since Jul and Aug, both have 31 days)
   
   
   Keep on subtracting the number of days in each month from variable days 
   (which was the input), and check whether the number of remaining days 
   are <= 0.
   
   
   If yes, then break out of the loop and the last positive value of 
   variable "days" will be the date. (assign another variable the
   value of n
   just before changing value of "days")
   
   
   For months, keep a counter which starts from 1.
   
   
   As soon as months reaches a value > 12, assign it 1, increment year, and
   check for leap year again. 
   
   
    
   
   
   Put all of this in a function and call it twice.
   
   
   The second time, call it giving number of days = earlier no. of days + 
   second inputted no. of days. i.e. printdate( days + after )

Posted at: Tue Jan 17 20:56:49 2012 (GMT)

From: rushilpaulReply 11 of 19Reply
Subject: Contributed Answer/Explanation to Q. 3
   Look at the code and follow the algorithm written after it
   
   #include "iostream"
   #include "sstream"
   
   
   using namespace std;
   
   bool checkleap(int y);
   string printdate(int n, int y);
   
   bool checkleap(int y)
   {
   return y % 400 == 0 || (y % 4 == 0 && y % 100 != 0);
   }
   
   string printdate(int n, int y)
   {
   int year = y, month=1, days=n;
   y = checkleap(y) ? 1 : 0;
   
   while( n > 0)
   {
   if(month > 12)
   {
   y = checkleap(++year) ? 1 : 0;
   month = 1;
   }
   days = n;
   if(month == 2) if(y==1) n -= 29;
   else n -= 28;
   else if(month <= 7) if(month % 2 == 1) n -= 31;
   else n -= 30;
   else
   {
   if(month % 2 == 0) n -= 31;
   else n -= 30;
   }
   month++;
   }
   string months[] = { "January", "February",
   "March", "April", "May", "June",
   "July", "August", "September",
   "October", "November", "December" };
   string app = "th";
   if( days % 10 == 1) app = "st";
   else if(days % 10 == 2) app = "nd";
   else if(days % 10 == 3) app = "rd";
   stringstream val;
   val << days << app << " " <<
   months[month-2] << " " << year;
   return val.str();
   }
   
   int main()
   {
   int n,y,after;
   cout << "Enter the number of days, year and additional number of
   days: ";
   cin >> n >> y >> after;
   bool err = n < 1 || n > 366;
   if(!checkleap(y)) if( n > 365) err = true;
   if(err) { cout << "Incorrect input." << endl; return
   1; }
   cout << "Current Date: \t" << printdate(n,y) <<
   endl << "Date after " << after << " days:
   " << printdate(n+after,y) << endl;
   return 0;
   }  // end of program 
   
   
    
   
   
   -----------------------------ALGORITHM--------------------------------------
   --------
   
   
   The first seven months of the year have 31 and 30 days alternately with an
   exception of February.
   
   
   That means for all m <= 7, if m is divisble by 2, then no. of days is 30,
   else 31. and if m = 2, then check for leap year.
   
   
   This is very simple: (y is divisble by 4 but not by 100) or (y is divisible
   by 400).
   
   
   Continuing, months from 8 to 12  have 31 and 30 days alternately. Again
   check if m is divisible by 2. If yes then its 31 else 30. (this time it gets
   reversed since Jul and Aug, both have 31 days)
   
   
   Keep on subtracting the number of days in each month from variable days
   (which was the input), and check whether the number of remaining days are
   <= 0.
   
   
   If yes, then break out of the loop and the last positive value of variable
   "days" will be the date. (assign another variable the value of n
   just before changing value of "days")
   
   
   For months, keep a counter which starts from 1.
   
   
   As soon as months reaches a value > 12, assign it 1, increment year, and
   check for leap year again. 
   
   
    
   
   
   Put all of this in a function and call it twice.
   
   
   The second time, call it giving number of days = earlier no. of days +
   second inputted no. of days. i.e. printdate( days + after )

Posted at: Tue Jan 17 20:58:24 2012 (GMT)

From: arnab_javaReply 12 of 19Reply         View replies (1)
Subject: Contributed Answer/Explanation to Q. 3
   This is the answer to Question-3 (String program).
   import java.io.*;
   import java.util.*;
   class Reverse
   {
       public static void main(String []args)throws IOException
       {
           BufferedReader br=new BufferedReader(new
   InputStreamReader(System.in));
           System.out.println("Enter the sentence");int
   t1,t2,n;
           String
   s=br.readLine(),x="",p="",q="";int k=0;
           int l=s.length();
           for(int i=0;i<l;i++)
           {
               char ch=s.charAt(i);
               if(Character.isUpperCase(ch));
               char chr=Character.toLowerCase(ch);
               x=x+chr;
           }
           StringTokenizer st=new StringTokenizer(x,". ");
           n=st.countTokens();
           String a[]=new String[n],t;
           while(st.hasMoreTokens())
           {
               a[k++]=st.nextToken();
           }
           for(int i=0;i<k;i++)
           {
               for(int j=0;j<(k-1);j++)
               {
                   t1=a[j].length();t2=a[j+1].length();
                   if(t1>t2)
                   {
                       t=a[j];
                       a[j]=a[j+1];
                       a[j+1]=t;
                   }
               }
           }
           p=p+a[0];
           int l1=p.length();
           for(int i=0;i<l1;i++)
           {
               char ch1=p.charAt(i);
               if(i==0)
               ch1=Character.toUpperCase(ch1);
               q=q+ch1;
           }
           System.out.print(q+" ");
           for(int i=1;i<k;i++)
           System.out.print(a[i]+" ");
       }
   }

Posted at: Tue Feb 21 11:59:04 2012 (GMT)

From: arnab_javaReply 13 of 19Reply
Subject: Contributed Answer/Explanation to Q. 3
   This is the answer to Question-3 (String program).
   import java.io.*;
   import java.util.*;
   class Reverse
   {
       public static void main(String []args)throws IOException
       {
           BufferedReader br=new BufferedReader(new
   InputStreamReader(System.in));
           System.out.println("Enter the sentence");int
   t1,t2,n;
           String
   s=br.readLine(),x="",p="",q="";int k=0;
           int l=s.length();
           for(int i=0;i<l;i++)
           {
               char ch=s.charAt(i);
               if(Character.isUpperCase(ch));
               char chr=Character.toLowerCase(ch);
               x=x+chr;
           }
           StringTokenizer st=new StringTokenizer(x,". ");
           n=st.countTokens();
           String a[]=new String[n],t;
           while(st.hasMoreTokens())
           {
               a[k++]=st.nextToken();
           }
           for(int i=0;i<k;i++)
           {
               for(int j=0;j<(k-1);j++)
               {
                   t1=a[j].length();t2=a[j+1].length();
                   if(t1>t2)
                   {
                       t=a[j];
                       a[j]=a[j+1];
                       a[j+1]=t;
                   }
               }
           }
           p=p+a[0];
           int l1=p.length();
           for(int i=0;i<l1;i++)
           {
               char ch1=p.charAt(i);
               if(i==0)
               ch1=Character.toUpperCase(ch1);
               q=q+ch1;
           }
           System.out.print(q+" ");
           for(int i=1;i<k;i++)
           System.out.print(a[i]+" ");
       }
   }

Posted at: Tue Feb 21 12:01:42 2012 (GMT)

From: arnab_javaReply 14 of 19Reply
Subject: Contributed Answer/Explanation to Q. 3
   This is the answer to Question-3 (String program).
   import java.io.*;
   import java.util.*;
   class Reverse
   {
       public static void main(String []args)throws IOException
       {
           BufferedReader br=new BufferedReader(new
   InputStreamReader(System.in));
           System.out.println("Enter the sentence");int
   t1,t2,n;
           String
   s=br.readLine(),x="",p="",q="";int k=0;
           int l=s.length();
           for(int i=0;i<l;i++)
           {
               char ch=s.charAt(i);
               if(Character.isUpperCase(ch));
               char chr=Character.toLowerCase(ch);
               x=x+chr;
           }
           StringTokenizer st=new StringTokenizer(x,". ");
           n=st.countTokens();
           String a[]=new String[n],t;
           while(st.hasMoreTokens())
           {
               a[k++]=st.nextToken();
           }
           for(int i=0;i<k;i++)
           {
               for(int j=0;j<(k-1);j++)
               {
                   t1=a[j].length();t2=a[j+1].length();
                   if(t1>t2)
                   {
                       t=a[j];
                       a[j]=a[j+1];
                       a[j+1]=t;
                   }
               }
           }
           p=p+a[0];
           int l1=p.length();
           for(int i=0;i<l1;i++)
           {
               char ch1=p.charAt(i);
               if(i==0)
               ch1=Character.toUpperCase(ch1);
               q=q+ch1;
           }
           System.out.print(q+" ");
           for(int i=1;i<k;i++)
           System.out.print(a[i]+" ");
       }
   }

Posted at: Tue Feb 21 12:03:22 2012 (GMT)

From: amit95Reply 15 of 19Reply
Subject: Contributed Answer/Explanation to Q. 1
   
   
   //simple to understand, i hope
   
   
   import java.util.*;
   
   
   class DateAhead
   
   
   {
   
   
       static String suffix(int m)
   
   
       {
   
   
           switch(m)
   
   
           {
   
   
               case 1:return "ST";
   
   
               case 2:return "ND";
   
   
               case 3: return "RD";
   
   
           }
   
   
           return "TH";
   
   
       }
   
   
       public static void main(String args[])
   
   
       {
   
   
           Scanner sc=new Scanner(System.in);
   
   
           System.out.println("Enter day number");
   
   
           int dn=sc.nextInt();
   
   
           System.out.println("Enter the year");
   
   
           int yy=sc.nextInt();
   
   
           System.out.println("Enter no. of days ahead");
   
   
           int N=sc.nextInt();
   
   
           String
   m[]={"January","February","March","April&
   quot;,"May","June","July","August",&
   quot;September","October","November","December
   "};
   
   
           int md[]={31,28,31,30,31,30,31,31,30,31,30,31}, td=365;
   
   
           if(isLeap(yy))
   
   
           {
   
   
               md[1]=29;
   
   
               td=366;
   
   
           }
   
   
           int mm=0,a=dn;
   
   
           while(true)
   
   
           {
   
   
               if((dn-md[mm])<0)
   
   
                   break;
   
   
               dn-=md[mm];
   
   
               ++mm;
   
   
           }
   
   
           System.out.println("The date is :
   "+dn+(((dn>3)&&(dn<21))?"th":suffix(dn%10))+&qu
   ot; "+m[mm]+" "+yy);
   
   
           mm=0;
   
   
           if((a+N)>td)
   
   
           {
   
   
               a=(a+N)-td;
   
   
               ++yy;
   
   
           }
   
   
           else
   
   
               a+=N;
   
   
           while(true)
   
   
           {
   
   
               if((a-md[mm])<0)
   
   
                   break;
   
   
               a-=md[mm];
   
   
               ++mm;
   
   
           }
   
   
               ++mm;
   
   
               System.out.println("The date after "+N+"
   days is :
   "+a+(((a>3)&&(a<21))?"th":suffix(a%10))+"
   "+m[mm-1]+" "+yy);
   
   
       }
   
   
       static boolean isLeap(int y)
   
   
       {
   
   
           if((y%100==0&&y%400==0)||(y%100!=0&&y%4==0))
   
   
               return true;
   
   
           return false;
   
   
       }
   
   
   }

Posted at: Fri Feb 8 15:52:58 2013 (GMT)

From: durgesh98Reply 16 of 19Reply
Subject: Contributed Answer/Explanation to Q. 1
   
   
   Answer:
   
   
    
   
   
    
   
   
   import java.util.*;
   
   
   class days
   
   
   {
   
   
       public static void main()
   
   
       {
   
   
           Scanner sc=new Scanner(System.in);
   
   
           System.out.println("DAY NUMBER");
   
   
           int day=sc.nextInt();
   
   
           System.out.println("YEAR");
   
   
           int year=sc.nextInt();
   
   
           System.out.println("DAY AFTER(N)");
   
   
           int day_after=sc.nextInt();
   
   
           int sum=0;
   
   
           int newdate=0;
   
   
           int p=0;
   
   
           String t="";
   
   
           int days=day;
   
   
           int m[]={31,29,31,30,31,30,31,31,30,31,30,31};
   
   
                   String
   years[]={"January","February","March","Ap
   ril","May","June","July","August&quo
   t;,"September","October","November","Dece
   mber"};
   
   
                   if(year%4==0)
   
   
                   {
   
   
                       m[1]=29;
   
   
                   }
   
   
                   else
   
   
                   {
   
   
                       m[1]=28;
   
   
                   }
   
   
                  
   if((day>=1&&day<=366)&&(year>=1000&&year<
   ;=9999)&&(day_after>=1&&day_after<=100))
   
   
                   {
   
   
                   for(int i=0;i<12;i++)
   
   
                   {
   
   
                       if(day>=0&&day<=m[i])
   
   
                       {
   
   
                           System.out.println(day+"
   "+years[i]+"  "+year);
   
   
                           break;
   
   
                          }
   
   
                          else
   
   
                          {
   
   
                              day=day-m[i];
   
   
                              p++;
   
   
                          }
   
   
                       }
   
   
                       System.out.println("DAY AFTER
   "+day_after+" DAYS");
   
   
                       days=days+day_after;
   
   
                       int k=0;
   
   
                       if(days>=366)
   
   
                       {
   
   
                           days=days-366;
   
   
                           k++;
   
   
                       }
   
   
                          for(int i=0;i<12;i++)
   
   
                   {
   
   
                       if(days>=0&&days<=m[i])
   
   
                       {
   
   
                           if(k!=0)
   
   
                           System.out.println(days+"
   "+years[i]+"  "+(year+1));
   
   
                           else
   
   
                           System.out.println(days+"
   "+years[i]+"  "+(year));
   
   
                           break;
   
   
                          }
   
   
                          else
   
   
                          {
   
   
                              days=days-m[i];
   
   
                          }
   
   
                       }
   
   
                   }
   
   
   }
   
   
   }

Posted at: Sun Dec 22 06:32:05 2013 (GMT)

From: durgesh98Reply 17 of 19Reply
Subject: Contributed Answer/Explanation to Q. 2
   import java.util.*; class matrix {	  public static void main()	{      
     Scanner sc =new Scanner(System.in);	 System.out.println("Enter
   Number of Rows");	     int m=sc.nextInt();	
   System.out.println("Enter Number of Columns");	

Posted at: Sun Dec 22 06:35:25 2013 (GMT)

From: ajmaReply 18 of 19Reply
Subject: Contributed Answer/Explanation to Q. 1
   
   
   import java.io.*;
   
   
   public class date
   
   
   {public void show()throws IOException
   
   
   {BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
   
   
    int flag,leap,i,num,mon1,day1,year1;
   
   
    int dpm[]={31,28,31,30,31,30,31,31,30,31,30,31};
   
   
    leap=0;
   
   
    flag=0; System.out.println("\nInput:");
   
   
    num=Integer.parseInt(buf.readLine());
   
   
    year1=num%100; num=num/100; mon1=num%100; num=num/100;
   
   
    day1=num;
   
   
    if(year1%100==0&&year1%400==0)
   
   
    leap=1;
   
   
    else
   
   
    if(year1%100!=0&&year1%4==0)
   
   
    leap=1;
   
   
    if(mon1==2&&leap==1&&dpm[mon1-1]>28)
   
   
    flag=1;
   
   
    else
   
   
     if(mon1==2&&leap==0&&dpm[mon1-1]>28)
   
   
     flag=1;
   
   
     else
   
   
     if(day1>dpm[mon1-1])
   
   
     flag=1;
   
   
     if(flag==1)
   
   
     {System.out.println("\nInvalid Date...");
   
   
         return;}
   
   
       leap=day1%10;
   
   
       System.out.print("\nOUTPUT:"+day1);
   
   
       switch(leap)
   
   
       { case 0:
   
   
           case 4:
   
   
           case 5:
   
   
           case 6:
   
   
           case 7:
   
   
           case 8:
   
   
           case 9:
   
   
           System.out.print("th ");
   
   
           break;
   
   
           case 1:
   
   
           System.out.print("st ");
   
   
           break;
   
   
           case 2:
   
   
           System.out.print("nd ");
   
   
           break;
   
   
           case 3:
   
   
           System.out.print("rd ");
   
   
           break;}
   
   
       switch(mon1)
   
   
      {case 1:
   
   
           System.out.print("january,");
   
   
           break;
   
   
            case 2:
   
   
           System.out.print("february,");
   
   
           break;
   
   
            case 3:
   
   
           System.out.print("march,");
   
   
           break;
   
   
            case 4:
   
   
           System.out.print("april,");
   
   
           break;
   
   
            case 5:
   
   
           System.out.print("may,");
   
   
           break;
   
   
            case 6:
   
   
           System.out.print("june,");
   
   
           break;
   
   
           case 7:
   
   
           System.out.print("july,");
   
   
           break;
   
   
            case  8:
   
   
           System.out.print("august,");
   
   
           break;
   
   
            case 9:
   
   
           System.out.print("september,");
   
   
           break;
   
   
            case 10:
   
   
           System.out.print("october,");
   
   
           break;
   
   
            case 11:
   
   
           System.out.print("november,");
   
   
           break;
   
   
            case 12:
   
   
           System.out.print("december,");
   
   
           break;}
   
   
       System.out.print(year1);}}
   
   
           

Posted at: Mon Feb 17 08:37:44 2014 (GMT)

From: cr7Reply 19 of 19Reply
Subject: Contributed Answer/Explanation to Q. 1
   
   
   import java.io.*;
   
   
   import java.util.*;
   
   
   class date
   
   
   {
   
   
    public static void main(String ar[])throws IOException
   
   
    {
   
   
     BufferedReader z=new BufferedReader(new InputStreamReader(System.in));
   
   
     int dd,mm,t,yy,N;
   
   
     String month="",month1="";
   
   
     System.out.print("Day number:  ");
   
   
     dd=Integer.parseInt(z.readLine());
   
   
     System.out.print("Year:  ");
   
   
     yy=Integer.parseInt(z.readLine());
   
   
     System.out.print("Days after(N):  ");
   
   
     N=Integer.parseInt(z.readLine());
   
   
     Date d=new Date(); //Using date class utility of JAVA which takes year 
   
   
                                     //as year-1900. Months
   and days start from 0 
   
   
     d.setDate(dd);
   
   
     d.setMonth(0);
   
   
     d.setYear(yy-1900);
   
   
     mm=d.getMonth();
   
   
   //The following if and else is just for printing name of month  
   
   
   if(mm==0)
   
   
     month="January";
   
   
     else if(mm==1)
   
   
     month="February";
   
   
     else if(mm==2)
   
   
     month="March";
   
   
     else if(mm==3)
   
   
     month="April";
   
   
     else if(mm==4)
   
   
     month="May";
   
   
     else if(mm==5)
   
   
     month="June";
   
   
     else if(mm==6)
   
   
     month="July";
   
   
     else if(mm==7)
   
   
     month="August";
   
   
     else if(mm==8)
   
   
     month="September";
   
   
     else if(mm==9)
   
   
     month="October";
   
   
     else if(mm==10)
   
   
     month="November";
   
   
     else if(mm==11)
   
   
     month="December";
   
   
     System.out.println(d.getDate()+"th "+month+"
   "+(d.getYear()+1900));
   
   
     Date d1=new Date(yy-1900,mm/12,dd+N);
   
   
     t=d1.getMonth();
   
   
     if(t>11)
   
   
     t=t/12;
   
   
   //Again the following if and else is just for printing name of month 
   
   
     if(t==0)
   
   
     month1="January";
   
   
     else if(t==1)
   
   
     month1="February";
   
   
     else if(t==2)
   
   
     month1="March";
   
   
     else if(t==3)
   
   
     month1="April";
   
   
     else if(t==4)
   
   
     month1="May";
   
   
     else if(t==5)
   
   
     month1="June";
   
   
     else if(t==6)
   
   
     month1="July";
   
   
     else if(t==7)
   
   
     month1="August";
   
   
     else if(t==8)
   
   
     month1="September";
   
   
     else if(t==9)
   
   
     month1="October";
   
   
     else if(t==10)
   
   
     month1="November";
   
   
     else if(t==11)
   
   
     month1="December";
   
   
     System.out.println("DATE AFTER "+N+"
   DAYS:"+d1.getDate()+"th "+month1+"
   "+(d1.getYear()+1900));
   
   
    }
   
   
   }

Posted at: Sun Feb 15 04:49:24 2015 (GMT)

Page 1 of 1

To post to this forum, you must be signed in as a Syvum member. Please sign in / register as a member.

Contact Info © 1999-2024 Syvum Technologies Inc. Privacy Policy Disclaimer and Copyright