Tuesday 13 September 2016

Python code for Age Calculator

from datetime import datetime
now = datetime.now()
ty = now.year
tm = now.month
td = now.day
ans = "y"
while ans == 'y' or ans == 'Y':
    dob = raw_input("Enter your date of birth in dd/mm/yyyy format:")
    l = dob.split("/")
    bd = int(l[0])
    bm = int(l[1])
    by = int(l[2])
    if bm != 2 or bd != 29:
        if tm == 1 or tm == 2 or tm == 4 or tm == 6 or tm == 8 or tm == 9 or tm == 11:
            d = 31
        elif tm == 3:
            if ty % 4 == 0 and (ty % 100 != 0 and ty % 400 == 0):
                d = 29
            else:
                d = 28
        else:
            d = 30
        if by <= ty:
            def date():
                if td > bd:
                    ad = td-bd
                elif td == bd:
                    ad = 0
                else:
                    n = bd-td
                    ad = d-n
                return ad
            if tm > bm:
                if td > bd:
                    am = tm-bm
                else:
                    am = tm-bm-1
                ay = ty-by
                date()
            elif tm == bm:
                date()
                if date() == 0:
                    print "Happy Birthday"
                    am = 0
                    ay = ty-by
                else:
                    if bd < td:
                        am = 0
                        ay = ty-by
                    else:
                        am = 11
                        ay = ty-by-1
            else:
                date()
                ch = bm-tm
                if bd < td:
                    am = 12-ch
                    ay = ty-by
                else:
                    am = 11-ch
                    ay = ty-by-1
            print "\nYou are", ay, "years", am, "months and", date(), "days old."
        else:
            print "Invalid Birth year"
    else:
        print "So you are a leap year baby na,Sorry I can't calculate your age,may be 1/4th of your present age, hahahaha!"
    ans = raw_input("\nDo you wish to continue(y/n)?\n ->>")
    print "...................................................\n\n"

Saturday 13 August 2016

C++ code to find the age

#include<iostream.h>
#include<conio.h>
#include<math.h>
int bd , bm ,by ,td , ty , tm , am , ay , ad , ch , lp , op , days;
char c;
void nofdays()
{
lp=ay/4;
op=ay-lp;
days=(lp*366)+(op*355)+(am*30)+ad;
cout<<"\n It has been"<<days<<since you were  born";
}

void inputdata()
{
td=14;
tm=8;
ty=2016;
cout<<"\n Enter your date of birth,                   
cout<<"\n\n Day : ";
cin>>bd;
cout<<"\n Month : ";
cin>>bm;
cout<<"\n Yearr : ";
cin>>by;
}

void day()
{
if (td>bd)
ad=td-bd;
else if(bd==td)
{
cout<<"\n Happy Birthday";
ad=0;
}
else
{
int n=bd-td;
ad=30-n;
}}

void call()
{
if(tm>bm)
{
am=tm-bm;
ay=ty-by;
}

else
{
ch=bm-tm;

if(bd<td)
am=12-ch;

else
am=11-ch;
ay=ty-by-1;
}}

void display()
{
if(by>ty)
{
cout<<"\n Invalid birthyear";
}
else
{
cout<<"\n YOU ARE  "<<ay<<" YEARS "<<am<<" MONTHS and "<<ad<< " DAYS OLD";
}}

void main()
{
clrscr();
do
{
inputdata();
call();
day();
display();
nofdays();
cout<<"\n \t\t\t\t   Do you wish to continue(y/n)?";
cin>>c;
}while(c=='y');
getch();
}



**************
This coding is solely of my logic. Here I face problem of updating the current date. I would be thankful if you have any suggestions for that or for anything that I could improve.
Thanks.







Code in C++ to find difference,product,quotient and remainder of two numbers

I suggest all of you to read the below code. These four operations can easily be performed by using the correct operator(i.e, + , - , * , / , %)etc.

To find the difference,    change     s=a-b;
To find the product,        change      s=a*b;
To find the quotient,       change       s=a/b;
To find the remainder,    change        s=a%b;

NOTE: These statements should only be used in the place of s=a+b; . And don't use all of these operations at once or if you want to print all the operations at once , give s=a+b;k=a-b;
etc... i.e, assign these operations to different variables and print them respectively.

Code in C++ to find the sum of two numbers

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,s;
cout<<"\n Enter the first number : ";
cin>>a;
cout<<"\n Enter the second number : ";
cin>>b;
s=a+b;
cout<<"\n The sum is : "<<s;
getch();
}