C Program to Convert Date Given in BS to AD
As there is no direct formula or relation to convert date in (Bikram Sambat) BS to date in AD.
So, for simplicity we will assume difference between these two dates for today.
Today (At the time of this writing) in BS : 2071 - 12 - 18
and in AD it is : 2015 - 04 -01
So calculating difference,
Difference is : 56 years - 08 months - 17 days.
So logic here is taking BS date from user and subtracting above difference to get date in AD.
Here are three different program to convert date given in BS to AD this
1. First Program (Without using structure)
/* Program to convert BS to AD */
#include<stdio.h>
#include<conio.h>
void main()
{
int year, month, date;
clrscr();
/* Taking Input from User */
printf("Give BS date in year month and date:\n");
scanf("%d%d%d",&year,&month,&date);
/* Changing Date */
if(date > 17)
{
date -= 17;
}
else
{
date += 30;
--month;
date -= 17;
}
if(month > 8)
{
month -= 8;
}
else
{
month += 12;
--year;
month -= 8;
}
year -= 56;
/* Display Output */
printf("Date in AD is : %d-%d-%d\n",year, month, date);
getch();
}
2. Using Structure and Function
/* Program to convert BS to AD */
/* Using structure and user defined function */
#include<stdio.h>
#include<conio.h>
/* Defining Structure */
struct bs
{
int year;
int month;
int date;
};
/* Function Prototype */
struct bs convert(struct bs y);
void main()
{
/* Declaring structure type variables */
/* Here structure variable x is for taking
input and ad for holding returned structure */
struct bs x, ad;
clrscr();
/* Taking Input from User */
printf("Give BS date in yy-mm-dd format:\n");
scanf("%d-%d-%d",&x.year,&x.month,&x.date);
/* Calling function convert to change date */
ad = convert(x);
/* Display Output */
printf("Date in AD is : %d-%d-%d\n",ad.year,ad.month,ad.date);
getch();
}
struct bs convert( struct bs y)
{
if(y.date > 17)
{
y.date -= 17;
}
else
{
y.date += 30;
--y.month;
y.date -= 17;
}
if(y.month > 8)
{
y.month -= 8;
}
else
{
y.month += 12;
--y.year;
y.month -= 8;
}
y.year -= 56;
return(y);
}
Note: Notice '-' in scanf while taking input.
While entering date in bs you need to follow this format
i.e. 2072-1-1
3. Using Structure , User Defined function and Typedef
/* Program to convert BS to AD */
/* Using structure and user defined function and typedef */
#include<stdio.h>
#include<conio.h>
typedef struct
{
int year;
int month;
int date;
}bs;
/* Function Prototype */
bs convert(bs y);
void main()
{
/* Declaring structure type variables */
/* Here structure variable x is for taking
input and ad for holding returned structure */
bs x, ad;
clrscr();
/* Taking Input from User */
printf("Give BS date in yy-mm-dd format:\n");
scanf("%d-%d-%d",&x.year,&x.month,&x.date);
/* Calling function convert to change date */
ad = convert(x);
/* Display Output */
printf("Date in AD is : %d-%d-%d\n",ad.year,ad.month,ad.date);
getch();
}
/* Function definition */
bs convert( bs y)
{
if(y.date > 17)
{
y.date -= 17;
}
else
{
y.date += 30;
--y.month;
y.date -= 17;
}
if(y.month > 8)
{
y.month -= 8;
}
else
{
y.month += 12;
--y.year;
y.month -= 8;
}
y.year -= 56;
return(y);
}
Note: Notice '-' in scanf while taking input.
While entering date in bs you need to follow the format
i.e. 2072-1-1
Limitation: Since there is no direct relation between BS to AD most of the time the answer will be Incorrect.