Q:Suppose you purchase 100 shares of stock named ‘X’ in each of January, April and September and sell 100 shares of stocks in each of June and November. The prices per share in these months were:
a. January – INR 10
b. April – INR 30
c. June – INR 20
d. September – INR 50
e. November – INR 30
(i) Show that with FIFO accounting (i.e. you are adding your shares to queue) results in a gain of Rs 1000 (10*100)
(ii) Show that with LIFO accounting (i.e. you are adding your shares to stack) results in a loss of Rs 3000 (30*100)
(iii) In total, you are purchasing 300 shares and selling 200 shares. The 100 shares that you still own do not enter the calculation of loss and profit.
Code:
#include<stdio.h>
#include<stdlib.h>
struct share
{
int amount;
struct share *next;
}*start=NULL;
void purchase_queue_stack();
void sell_queu();
void sell_stack();
void display();
typedef enum abc
{Jan=1,April,June,Sept,Nov} ;
void main()
{ int ch,sp;
char str='y';
printf("\nPrices per Share:\n1:Jan INR 10\n2:April INR 30\n3:June INR 20\n4:Sept INR 50\n5:NOv INR 30\n\nplease Select one from following\n1:Queue(FIFO) Accounting\n2:Stack(LIFO) Accounting");
scanf("%d",&ch);
switch(ch)
{
case 1:
do{
printf("\nDo you want to purchase or sell\n1 for purchase\t 2 for sell");
scanf("%d",&sp);
if(sp==1)
{purchase_queue_stack();
}
if(sp==2)
{
if(start==NULL)
{ printf("\nQueue is empty");
}
else
sell_queu();
}
printf("\n Do you want next operation press y");
scanf(" %c",&str);
}while(str=='y');
break;
case 2:
do{
printf("\nDo you want to purchase of sell\n1 for purchase\t 2 for sell");
scanf("%d",&sp);
if(sp==1)
{
purchase_queue_stack();//insertion in both stack and queue are same
}
if(sp==2)
{
if(start==NULL)
{ printf("\nStack is empty");
}
else
sell_stack();
}
printf("\n Do you want next operation press y");
scanf(" %c",&str);
}while(str=='y');
break;
default:
printf("\nwrong selection");
}
}
void purchase_queue_stack()
{
struct share *node,*ptr;
ptr=start;
int amt=0;
int month;
printf("\nEnter the month no");
scanf("%d",&month);
if(month==1)
amt=10*100;
else if(month==2||month==5)
amt=30*100;
else if(month==3)
amt=20*100;
else if(month==4)
amt=50*100;
else printf("\n price not given ");
node=(struct share*)malloc(sizeof(struct share));
node->amount=amt;
if(start==NULL)
{
node->next=start;
start=node;
}
else
{ while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=node;
node->next=NULL;
}
display();
}
void display()
{
struct share *ptr;
ptr=start;
printf("\n Now List is::\n\n");
while(ptr!=NULL)
{
printf("%d\t",ptr->amount);
ptr=ptr->next;
}
}
void sell_queu()
{ struct share *tmp;
tmp=start;
int amt=0;
int month;
static int profit=0,loss=0;
printf("\nEnter the month no");
scanf("%d",&month);
if(month==1)
amt=10*100;
else if(month==2||month==5)
amt=30*100;
else if(month==3)
amt=20*100;
else if(month==4)
amt=50*100;
else printf("\n price not given ");
if(amt>start->amount)
{
profit=profit+(amt-start->amount);
printf("\n*****************************");
printf("\nPurchase amount=%d\nSell amount of month =%d",start->amount,amt);
printf("\nProfit of this month=%d",amt-start->amount);
printf("\n*****************************");
}
else
{
loss=loss+(start->amount -amt);
printf("\n*****************************");
printf("\nPurchase amount=%d\nSell amount=%d",start->amount,amt);
printf("\nLoss of this month=%d",start->amount -amt);
printf("\n***************************");
}
if(profit>=loss)
printf("\n\nFinal profit=%d",profit-loss);
else
printf("\n\nFinal loss=%d",loss-profit);
printf("\n****************************");
start=start->next;
free(tmp);
display();
}
void sell_stack()
{ struct share *tmp,*ptr;
ptr=start;
int amt=0;
int month;
static int profit=0,loss=0;
printf("\nEnter the month no");
scanf("%d",&month);
if(month==1)
amt=10*100;
else if(month==2||month==5)
amt=30*100;
else if(month==3)
amt=20*100;
else if(month==4)
amt=50*100;
else printf("\n price not given ");
while(ptr->next->next!=NULL)
ptr=ptr->next;
tmp=ptr->next;
ptr->next=NULL;
if(amt>tmp->amount)
{
profit=profit+(amt-tmp->amount);
printf("\n*****************************");
printf("\nPurchase amount=%d\nSell amount of month =%d",tmp->amount,amt);
printf("\nProfit of this month=%d",amt-tmp->amount);
printf("\n*****************************");
}
else
{
loss=loss+(tmp->amount -amt);
printf("\n*****************************");
printf("\nPurchase amount=%d\nSell amount=%d",tmp->amount,amt);
printf("\nLoss of this month=%d",tmp->amount -amt);
printf("\n***************************");
}
if(profit>=loss)
printf("\n\nFinal profit=%d",profit-loss);
else
printf("\n\nFinal loss=%d",loss-profit);
printf("\n****************************");
free(tmp);
display();
}
Output:
FIFO Accounting
Prices per Share:
1:Jan INR 10
2:April INR 30
3:June INR 20
4:Sept INR 50
5:NOv INR 30
please Select one from following
1:Queue(FIFO) Accounting
2:Stack(LIFO) Accounting 1
Do you want to purchase or sell
1 for purchase 2 for sell 1
Enter the month no 1
Now List is::
1000
Do you want next operation press y y
Do you want to purchase or sell
1 for purchase 2 for sell 1
Enter the month no 2
Now List is::
1000 3000
Do you want next operation press y y
Do you want to purchase or sell 1 for purchase 2 for sell 2
Enter the month no 3
*****************************
Purchase amount=1000
Sell amount of month =2000
Profit of this month=1000
*****************************
Final profit=1000
****************************
Now List is::
3000
Do you want next operation press y y
Do you want to purchase or sell 1 for purchase 2 for sell 1
Enter the month no 4
Now List is::
3000 5000
Do you want next operation press y y
Do you want to purchase or sell 1 for purchase 2 for sell 2
Enter the month no 5
*****************************
Purchase amount=3000
Sell amount=3000
Loss of this month=0
***************************
Final profit=1000
****************************
Now List is::
5000
Do you want next operation press y
LIFO Accounting
Prices per Share:
1:Jan INR 10
2:April INR 30
3:June INR 20
4:Sept INR 50
5:NOv INR 30
please Select one from following
1:Queue(FIFO) Accounting
2:Stack(LIFO) Accounting 2
Do you want to purchase or sell
1 for purchase 2 for sell 1
Enter the month no 1
Now List is::
1000
Do you want next operation press y y
Do you want to purchase or sell 1 for purchase 2 for sell 1
Enter the month no 2
Now List is::
1000 3000
Do you want next operation press y y
Do you want to purchase or sell
1 for purchase 2 for sell 2
Enter the month no 3
*****************************
Purchase amount=3000
Sell amount=2000
Loss of this month=1000
***************************
Final loss=1000
****************************
Now List is::
1000
Do you want next operation press y y
Do you want to purchase or sell 1 for purchase 2 for sell 1
Enter the month no 4
Now List is::
1000 5000
Do you want next operation press y y
Do you want to purchase or sell
1 for purchase 2 for sell 2
Enter the month no 5
*****************************
Purchase amount=5000
Sell amount=3000
Loss of this month=2000
***************************
Final loss=3000
****************************
Now List is::
1000 Do you want next operation press y n
Explanation:
No comments:
Post a Comment