#include <stdio.h>
#include <stdlib.h>
void outputNormal( unsigned int output);
void outputData( char format, unsigned int output );
void outputWithCharacter( char format, unsigned int output );
void outputDecimal( int numberOfDecimal, unsigned int output );
int getNumberOfDigit( unsigned int number );
int getNthDigit( int position, unsigned int number );
void funny(char format, unsigned int output);
int main()
{
unsigned int output;
char format;
printf("Please enter the Format Character: ");
scanf("%c", &format );
printf("Please enter the output number: ");
scanf("%d", &output);
outputData( format, output );
}//end of main
void outputData(char format, unsigned int output)
{
if ( (format == ',') || (format == ' ') )
{
outputWithCharacter( format, output );
}
else if ((format == 'N')||(format == 'n'))
{
outputNormal(output);
}
else if (( format >= 49 ) /*ascII code 1 */
&& (format <= 57) /*ascII code 9 */ )
{
outputDecimal( (format - 48), output);
}
else
{
printf("Incorrect format code");
}
}//end of main
void outputNormal( unsigned int output )
{
printf("%d", output);
}//end of outputNormal
void outputWithCharacter( char format, unsigned int output )
{
unsigned int count;
int numberOfCharacters;
numberOfCharacters = getNumberOfDigit( output );
for( count = numberOfCharacters; count > 0; count -- )
{
printf("%d", getNthDigit(count,output));
if( ((count%3) == 1)&& (count > 1) )
{
printf("%c", format);
}
}//end of for loop
}//end of outputWithCharacter
void outputDecimal( int numberOfDecimal, unsigned int output )
{
int count;
printf("%d", output );
printf(".");
for( count = 0; count < numberOfDecimal; count ++ )
{
printf("0");
}
}//end of outputDecimal
int getNumberOfDigit( unsigned int number )
{
int numberOfDigit = 0;
unsigned int temp = number ;
while( temp > 0 )
{
temp = temp /10;
numberOfDigit = numberOfDigit + 1;
}
return numberOfDigit;
}//end of getNumberOfDigit
int getNthDigit( int position, unsigned int number )
{
int temp = number;
int tempHolder = 0;
int count;
for( count = 0 ; count < position; count ++ )
{
tempHolder = temp;
temp = temp /10;
}
temp = temp * 10;
return tempHolder - temp;
}//end of getNthDigit