Advertisement
ara1123

digits

Oct 26th, 2017
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.29 KB | None | 0 0
  1. /*
  2. File Name: digits.c
  3.  
  4. Author: Anthony Ambrose
  5.  
  6. Class: Computational Problem Solving for Engineers
  7.  
  8. Section: 3.6
  9.  
  10. Program Intent: To seperate user input into its individual digits and see if the input is divisible by its individual digits.
  11.  
  12. Input Data: A positive integer
  13.  
  14. Output Data: Either a wrong input statement, or the input is or is not divisible by its digits. Also prints the distribution of numbers from 1-9.
  15. */
  16.  
  17. /*--------------------------- main function -------------------------------
  18. Purpose: The main function prompts the user to give a positive integer, and verifies that it is positive, else says wrong input and prompts another input from the user.
  19.  
  20. Returns: A signal to the OS.
  21. ---------------------------------------------------------------------------*/
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <math.h>
  25.  
  26. #define TRUE 1
  27. #define FALSE 0
  28.  
  29. void sepdigits(int n1);
  30. void print(int *var1, int var2);
  31. int digits_different(int *var1, int var2);
  32. int divisible(int a[], int var2, int n1);
  33.  
  34.  
  35. int main()
  36. {
  37.     int n2;
  38.     long int n1=0;
  39.    
  40.     printf("Enter a positive integer or 0 (zero) to end: ");
  41.     scanf("%ld", &n1);
  42.    
  43.    
  44.     while (n1 != 0)
  45.     {
  46.         if (n1 < 0)
  47.             printf("Wrong input");
  48.            
  49.         else
  50.             sepdigits(n1);
  51.        
  52.     printf("\nEnter a positive integer or 0 (zero) to end: ");
  53.     scanf("%ld", &n1);
  54.     }
  55.  
  56.     printf("\n\n****Program Terminated**** \n\n");
  57.     return 0;
  58. }
  59.  
  60.  
  61. /*------------------function sepdigits-----------------
  62. Purpose: To seperate the user input into its individual digits, arrange the digits in an array of size ten, and then the function is supposed to call on the other functions to print the rest of the output.
  63.  
  64. Returns: Returns nothing, it is a void function.
  65. -----------------------------------------------------*/
  66.  
  67. void sepdigits(int n1)
  68. {
  69.     int a[10] = {0};
  70.     int digit=0;
  71.     int num = n1;
  72.    
  73.     while (num > 0)
  74.     {
  75.         digit = num % 10;
  76.         ++a[digit];
  77.         num = num/10;
  78.     }
  79.    
  80.     print(a, 10);
  81.    
  82.     if (a[0] != 0)
  83.     {  
  84.         printf("\n\nWrong input for the second part. \n");
  85.         printf("Input should not contain zero. \n");
  86.     }
  87.    
  88.     else if (digits_different(a, 10) == FALSE)
  89.     {  
  90.         printf("\n\nWrong input for the second part. \n");
  91.         printf("Input should not contain each digit more than once. \n");
  92.        
  93.     }
  94.    
  95.     else if (divisible(a, 10, n1) == FALSE)
  96.         printf ("\n\n%ld is not divisible by its digits. \n", n1);
  97.    
  98.     else
  99.         printf("\n\n%ld divisible by its digits\n", n1);
  100.    
  101. }
  102.  
  103.  
  104.  
  105. /*-------------------------function print----------------
  106. Purpose: To print out the header of the output, and also to print the array from sepdigits.
  107.  
  108. Returns: Nothing, it is another void function.
  109. --------------------------------------------------------*/
  110.  
  111. void print (int *var1, int var2)
  112. {
  113.     int n, i;
  114.     n = 0;
  115.    
  116.     printf("\n\n");
  117.     printf("Digits:         0  1  2  3  4  5  6  7  8  9\n");
  118.     printf("Occurrences:    ");
  119.    
  120.         for (i = 0; i < var2; i++)
  121.         {
  122.             printf("%d  ", *var1);
  123.             var1++;
  124.         }
  125. }
  126.  
  127.  
  128. /*--------------------------------function digits_different----------------
  129. Purpose: The purpose of this function is to verify that the user input has different digits and no digit is repeated twice.
  130.  
  131. Returns: Either TRUE or FALSE to the sepdigits function.
  132. -------------------------------------------------------------------------*/
  133.  
  134. int digits_different(int *var1, int var2)
  135. {
  136.     int c1;
  137.    
  138.     for(c1 = 0; c1 < var2; c1++)
  139.     {
  140.         if (*var1 > 1)
  141.         {   return FALSE;
  142.         }    
  143.        
  144.         var1++;
  145.     }
  146.    
  147.     return TRUE;
  148. }
  149.  
  150.  
  151. /*----------------------------function divisible------------------
  152. Purpose: The purpose of this function is to see if the user input is divisible by its individual digits.
  153.  
  154. Returns: Either TRUE or FALSE to the sepdigits function.
  155. ----------------------------------------------------------------*/
  156.  
  157. int divisible(int a[], int var2, int n1)
  158. {
  159.     int c1=1;
  160.     int c2=0;
  161.    
  162.     for ( int c1 = 1; c1 < var2; c1++ )
  163.     {
  164.         if ( a[c1] != 0 && n1 % c1  != 0 )
  165.             return FALSE;
  166.     }
  167.    
  168.     return TRUE;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement