#include <stdio.h>
#include <math.h>

double atof();
double pow();

main(argc, argv)
int argc;
char *argv[];
{
char tmp[200];
double withamt =0;
double interest = 0;
double numwiths = 0;
double yrstor = 0;
double pvoa = 0;
double pvalue = 0;
double fvoa = 0;

if ((argc == 2) && ((argv[1][0] == 'p') || (argv[1][0] == 'P')))
	{
	 fprintf(stdout,"Enter years to retirement................. ");
	 fgets(tmp,100,stdin);
	 yrstor = atof(tmp);
	 fprintf(stdout,"Enter annual interest rate <%%>............ ");
	 fgets(tmp,100,stdin);
	 interest = atof(tmp) / 100;
	 fprintf(stdout,"Enter number of years investment \n");
	 fprintf(stdout,"annuity should last in retirement......... ");
	 fgets(tmp,100,stdin);
	 numwiths = atof(tmp);
	 fprintf(stdout,"Enter amount wanted per year \n");
	 fprintf(stdout,"during retirement <$>..................... ");
	 fgets(tmp,100,stdin);
	 withamt = atof(tmp);
	 fprintf(stdout,"\n");
	}
else if ((argc == 2) && ((argv[1][0] == 'v') || (argv[1][0] == 'V')))
	{
	 fprintf(stdout,"retire: Version 1.00 \n");
	 fprintf(stdout,"        Copyright 1987 by Michael Brochstein\n");
	 fprintf(stdout,"        All rights reserved.\n");
	}
else if (argc != 5) 
	{
	 fprintf(stderr,"Usage: retire p \n");
	 fprintf(stderr,"       retire v \n");
	 fprintf(stderr,"       retire <years to retirement> <annual ");
	 fprintf(stderr,"interest rate>\n");
	 fprintf(stderr,"              <years in retirement> ");
	 fprintf(stderr,"<amount wanted per year in retirement>\n");
	}
else if (argc == 5)
	{
	 yrstor = atof(argv[1]);
	 interest = atof(argv[2]) / 100;
	 numwiths = atof(argv[3]);
	 withamt = atof(argv[4]);
	}

if ((argc == 5) 
	|| ((argc == 2) && ((argv[1][0] == 'p') || (argv[1][0] == 'P'))))
	{

	 pvoa = 1 / pow(1+interest,numwiths); 
	 pvoa = (1 - pvoa) / interest;
	 pvoa *= withamt;

	 fprintf(stdout,"Amount needed on retirement day............. %14.2f\n",
		pvoa);
	 fprintf(stdout,"\n");
	 
	 pvalue = 1 / pow(1+interest,yrstor);
	 pvalue *= pvoa;

	 fprintf(stdout,"Option 1: Lump sum needed to invest today... %14.2f\n",
		pvalue);

	 fvoa = (pow(1+interest,yrstor) - 1) / interest; 
	 fvoa = pvoa / fvoa;
	 
	 fprintf(stdout,"Option 2: Amount to be invested yearly\n");
	 fprintf(stdout,"          until retirement.................. %14.2f\n",
		fvoa);
	 
	}
}
