/*********************************************************************/
/* Author: B. Alex Bridges                                           */
/* File: lab02.c                                                     */
/* Class: CPSC-202 Lab Section 3                                     */
/* Project: Lab 2 - Math Trick                                       */
/* Purpose: This program will interact with the user to perform a    */
/*          simple math trick.                                       */
/*********************************************************************/

/* INCLUDES */
#include <stdio.h>

/* SUB FUNCTION(S) */
int sf_SumDigits (int Number);


/*********************************************************************/
/* MAIN FUNCTION                                                     */
/*********************************************************************/
int main ( )
{
	/* LOCAL VARIABLES */
	int Number=0, Sum=0;
	
	/* FUNCTION */
	/* 0. BEGIN */
	printf ("\n");
	printf ("============== \n");
	printf ("= Math Trick = \n");
	printf ("============== \n");
	printf ("\n");
	
	/* 1. Ask the user to enter a positive integer */
	printf ("Please enter a positive integer: ");
	scanf ("%d", &Number);
	printf ("\n"); /* Blank Line */
	
	/* 2. Determine the sum of the digits in the number */
	printf ("The sum of the digits in %d <=> ", Number);
	Sum = sf_SumDigits (Number);
	printf ("%d \n", Sum);
	printf ("\n"); /* Blank Line */
	
	/* 3. Subtract the sum from the original number */
	printf ("The sum (%d) subtracted from %d <=> ", Sum, Number);
	Number -= Sum;
	printf ("%d \n", Number);
	printf ("\n"); /* Blank Line */
	
	/* 4. Ask the user to re-arrange the digits */
	printf ("Please enter %d with the digits re-arranged: ", Number);
	scanf ("%d", &Number);
	printf ("\n"); /* Blank Line */
	
	/* 5. Add 25 to the number */
	printf ("25 addeded to %d <=> ", Number);
	Number += 25;
	printf ("%d \n", Number);
	printf ("\n"); /* Blank Line */
	
	/* 6. Ask the user to remove a non-zero digit from the number */
	printf ("Please enter %d with a non-zero digit removed: ", Number);
	scanf ("%d", &Number);
	printf ("\n"); /* Blank Line */
	
	/* 7. Determine the sum of the digits in the number */
	printf ("The sum of the digits in %d <=> ", Number);
	Sum = sf_SumDigits (Number);
	printf ("%d \n", Sum);
	printf ("\n"); /* Blank Line */
	
	/* 8. Subtract 7 from the sum of the digits */
	printf ("7 subtracted from %d <=> ", Sum);
	Number = Sum - 7;
	printf ("%d \n", Number);
	printf ("\n"); /* Blank Line */
	
	/* 9. Subtract the result from the next highest multiple of 9 */
	printf ("%d subtracted from the next highest multiple of 9 <=> ", Number);
	if (Number % 9 != 0)
	Number = (Number + (9 - (Number % 9))) - Number;
	printf ("%d \n", Number);
	printf ("\n"); /* Blank Line */
	
	/* 10. END */
	printf ("     Congratulations! Your final Math Trick result is '%d' \n", Number);
	printf ("\n");
	return (0);
} /* main */


/*********************************************************************/
/* SUB FUNCTION(S)                                                   */
/*********************************************************************/
/*************************************************************************/
/* Purpose:  Determine the sum of the digits for a number                */
/* Input: a number                                                       */
/* Output: the sum of the digits                                         */
/* Assumptions: The number has already been assigned a value             */
/*************************************************************************/
int sf_SumDigits (int Number)
{
	/* LOCAL VARIABLES */
	int Temp=0, Sum=0;
	
	/* SUB FUNCTION */
	while (Number >= 10)
	{
		Temp = Number % 10;
		if (Temp != 0)
		{
			Sum += Temp;
			Number -= Temp;
		} /* if */
		else
			Number = Number / 10;
	} /* while */
	Sum += Number;
	return (Sum);	
} /* sf_SumDigits */
