Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- File name: lab_assignment_6_mattias_herrfurth.c
- Author: Mattias Herrfurth
- Section: CMPR.271.04
- Program intent: The purpose of this program is to split an integer into
- it's individual digits and list the number of occurences of each digit
- in the integer. The program will also check to see if each digit is
- different, as well as check for if the integer is divisible by
- each and every digit it contains, as long as the integer does not
- contain a zero.
- Input data: an integer.
- Output data: a table listing the number of occurences of each digit in
- the integer, and whether or not the integer is divisible by each of its
- digits if the integer does not contain a zero.
- */
- /*--------------------
- Include section
- --------------------*/
- #include <stdio.h>
- #include <math.h>
- int TRUE=1;
- int FALSE=0;
- /*--------------------
- Function prototypes
- --------------------*/
- void split(long long int n);
- void table(int a[],int s);
- int diff(int a[],int s);
- int div(long long n,int a[],int s);
- /*----------main function----------
- Purpose: This function will receive an integer input from the user,
- check for whether or not it is positive, and call upon the other
- functions to perform operations with the inputted integer.
- ---------------------------------*/
- int main()
- {
- long long n=1;
- while (n!=0)
- {
- printf("\n\nEnter a positive integer with 10 or less digits, or enter 0 (zero) to terminate the program: ");
- scanf("%lld",&n);
- if (n==0)
- {
- break;
- }
- else if (n>9999999999)
- {
- printf("\nInput must be 10 digits or less");
- }
- else if (n<0)
- {
- printf("\nIncorrect input.");
- }
- else
- {
- split(n);
- }
- }
- printf("Goodbye.\n");
- return 0;
- }
- //this function will split the integer up into individual digits
- void split(long long int n)
- {
- int a[10]={0},s=0;
- long long int x=n,d;
- while (x>0)
- {
- d=x%10;
- a[s]=d;
- x=x/10;
- s++;
- }
- /*int g;
- for(g=0;g<10;g++)
- {
- printf("%d ",a[g]);
- }*/
- table(a,s);
- int c=0;
- while (c<s)
- {
- if (a[c]==0)
- {
- printf("\nWrong input for the second part.\nInput should not contain a zero.\n");
- break;
- }
- c++;
- }
- if (diff(a,s)==FALSE)
- {
- printf("\nWrong input for the second part.\nInput should not contain any digit more than once.\n");
- }
- else if (div(n,a,s)==TRUE)
- {
- printf("\n%lld is divisible by its digits.\n",n);
- }
- }
- /*this function will print a table of the amount of occurences for each
- digit in an integer*/
- void table(int a[],int s)
- {
- printf("Digits: 0 1 2 3 4 5 6 7 8 9\nOccurs: ");
- int m,dig=0,res;
- while (dig<10)
- {
- res=0;
- for(m=0;m<s;++m)
- {
- if (a[m]==dig)
- {
- res++;
- }
- }
- dig++;
- printf("%d ",res);
- if (dig==10)
- {
- break;
- }
- }
- }
- //this function will test the integer for if any of its digits repeat
- int diff(int a[],int s)
- {
- unsigned int c=0;
- int m;
- while (c<10)
- {
- for(m=0;m<s;m++)
- {
- if(a[c]==a[m] && c!=m)
- {
- return (FALSE);
- }
- }
- c++;
- }
- return (TRUE);
- }
- //this function tests for if the integer is divisible by each digit
- int div(long long n,int a[],int s)
- {
- int c;
- for(c=0;c<s;c++)
- {
- if (n%a[c]!=0)
- {
- return (FALSE);
- }
- }
- return (TRUE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement