As Salem said... break your code in small portions. Here's my implementation with comments for your study:
Code:
// cobalt.c
//
// Compile and link:
//
// $ gcc -O2 -o cobalt cobalt.c # add -lm if you use exp().
//
#include <stdio.h>
#include <stdlib.h> // ony for EXIT_* symbols.
// FIX: Don't need this with precalculated constant.
//#include <math.h>
// Instead of exp(-0.693)/5.272, this C is precalculated printing
// this expression with 60 decimal places in double precision:
//
// double C = exp(-0.693)/5.272;
// printf( "C=%.60f\n", C );
//
#define C 0.09485462740815016335904630295772221870720386505126953125
int main ( void )
{
int year;
double initial;
printf ( "Initial amount of Cobalt (grams)> " );
fflush( stdout ); // previous print doesn't have a final '\n',
// so, it's prudent to flush stdout.
// Check for your inputs!
if ( scanf ( "%lf", &initial ) != 1 )
{
error:
fprintf( stderr, "Wrong amount.\n" );
return EXIT_FAILURE;
}
if ( initial <= 0.0 )
goto error;
printf ( " Years Amount Decay\n" );
// "yyyyyy aaaaaa.aa dddddd.dd\n"
// "% 6d % 9.2f % 9.2f"
// Calculate and show decay year by year.
for ( year = 0; year <= 5; year++ )
{
double remain, decay;
decay = C * year;
remain = initial * ( 1.0 - decay );
// We can format the output directly on the format argument.
// no need to use ' ' as arguments! See comment above, after printing
// the 'header'.
printf ( "% 6d % 9.2f % 9.2f\n", year, remain, decay );
}
return EXIT_SUCCESS;
}
[]s
Fred