Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.56 KB | None | 0 0
  1. /*
  2. File name: lab_assignment_6_mattias_herrfurth.c
  3.  
  4. Author: Mattias Herrfurth
  5.  
  6. Section: CMPR.271.04
  7.  
  8. Program intent: The purpose of this program is to split an integer into
  9. it's individual digits and list the number of occurences of each digit
  10. in the integer. The program will also check to see if each digit is
  11. different, as well as check for if the integer is divisible by
  12. each and every digit it contains, as long as the integer does not
  13. contain a zero.
  14.  
  15. Input data: an integer.
  16.  
  17. Output data: a table listing the number of occurences of each digit in
  18. the integer, and whether or not the integer is divisible by each of its
  19. digits if the integer does not contain a zero.
  20. */
  21.  
  22. /*--------------------
  23. Include section
  24. --------------------*/
  25. #include <stdio.h>
  26. #include <math.h>
  27. int TRUE=1;
  28. int FALSE=0;
  29. /*--------------------
  30. Function prototypes
  31. --------------------*/
  32. void split(long long int n);
  33. void table(int a[],int s);
  34. int diff(int a[],int s);
  35. int div(long long n,int a[],int s);
  36.  
  37. /*----------main function----------
  38. Purpose: This function will receive an integer input from the user,
  39. check for whether or not it is positive, and call upon the other
  40. functions to perform operations with the inputted integer.
  41. ---------------------------------*/
  42. int main()
  43. {
  44.    long long n=1;
  45.    while (n!=0)
  46.    {
  47.       printf("\n\nEnter a positive integer with 10 or less digits, or enter 0 (zero) to terminate the program: ");
  48.       scanf("%lld",&n);
  49.       if (n==0)
  50.       {
  51.          break;
  52.       }
  53.       else if (n>9999999999)
  54.       {
  55.          printf("\nInput must be 10 digits or less");
  56.       }
  57.       else if (n<0)
  58.       {
  59.          printf("\nIncorrect input.");
  60.       }
  61.       else
  62.       {
  63.          split(n);
  64.       }
  65.    }
  66.    printf("Goodbye.\n");
  67.    return 0;
  68. }
  69.  
  70. //this function will split the integer up into individual digits
  71. void split(long long int n)
  72. {
  73.    int a[10]={0},s=0;
  74.    long long int x=n,d;
  75.    while (x>0)
  76.    {
  77.       d=x%10;
  78.       a[s]=d;
  79.       x=x/10;
  80.       s++;
  81.    }
  82.    /*int g;
  83.    for(g=0;g<10;g++)
  84.    {
  85.       printf("%d ",a[g]);
  86.    }*/
  87.    table(a,s);
  88.    int c=0;
  89.    while (c<s)
  90.    {
  91.       if (a[c]==0)
  92.       {
  93.          printf("\nWrong input for the second part.\nInput should not contain a zero.\n");
  94.          break;
  95.       }
  96.       c++;
  97.    }
  98.    if (diff(a,s)==FALSE)
  99.    {
  100.       printf("\nWrong input for the second part.\nInput should not contain any digit more than once.\n");
  101.    }
  102.    else if (div(n,a,s)==TRUE)
  103.    {
  104.       printf("\n%lld is divisible by its digits.\n",n);
  105.    }
  106. }
  107.  
  108. /*this function will print a table of the amount of occurences for each
  109. digit in an integer*/
  110. void table(int a[],int s)
  111. {
  112.    printf("Digits:     0   1   2   3   4   5   6   7   8   9\nOccurs:     ");
  113.    int m,dig=0,res;
  114.    while (dig<10)
  115.    {
  116.       res=0;
  117.       for(m=0;m<s;++m)
  118.       {
  119.          if (a[m]==dig)
  120.          {
  121.             res++;
  122.          }
  123.       }
  124.       dig++;
  125.       printf("%d   ",res);
  126.       if (dig==10)
  127.       {
  128.          break;
  129.       }
  130.    }
  131. }
  132.  
  133. //this function will test the integer for if any of its digits repeat
  134. int diff(int a[],int s)
  135. {
  136.    unsigned int c=0;
  137.    int m;
  138.    while (c<10)
  139.    {
  140.       for(m=0;m<s;m++)
  141.       {
  142.          if(a[c]==a[m] && c!=m)
  143.          {
  144.             return (FALSE);
  145.          }
  146.       }
  147.       c++;
  148.    }
  149.    return (TRUE);
  150. }
  151.  
  152. //this function tests for if the integer is divisible by each digit
  153. int div(long long n,int a[],int s)
  154. {
  155.    int c;
  156.    for(c=0;c<s;c++)
  157.    {
  158.       if (n%a[c]!=0)
  159.       {
  160.          return (FALSE);
  161.       }
  162.    }
  163.    return (TRUE);
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement