Thread: Need some suggestions/Guidance

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    6

    Need some suggestions/Guidance

    Hello I'm currently working on C/++/# programming because I'm planning to attend college for Software Development in hopes of possibly working with some major and minor developers down the line and I was told by a mentor to come here. I'm currently working on a program that uses numbers divisible by 9 here's my code so far I can get it to go up to 8 numbers but one minor problem what if someone wants to just use 4-3 digits? is there anyway of possibly changing this around to suit that purpose without avoiding lowering numbers?
    Code:
    #include <stdio.h>
    
    
    #include <stdlib.h>
     
    /* Determines whether or not the users integer is divisible by 9 and displays all numbers withing integer*/
     
    int main(void)
    {
     
        int  num;  /* Users inputed integer */
        int d8; /* 8th digit of the number */
        int d7; /* 7th digit of the number */
        int d6; /* 6th digit of the number */
        int d5; /* 5th digit of the number */
        int d4; /* 4th digit of the number */
        int d3; /* 3rd digit of the number */
        int d2; /* 2nd digit of the number */
        int d1; /* 1st digit of the number */
         
        printf("please enter any digis to see if its divisable by 9. \n");
        scanf("%d", &num);
         
        if ( num % 9 >= 0)
           {
     
                   /* Peels off the integer's digits and assigns them to a variable*/
                 d8 = num % 10;
                 d7 = (num /100) % 10;
                 d6 = (num / 1000) % 10;
                 d5 = (num / 10000) % 10;
                 d4 = (num / 100000) % 10;
                 d3 = (num / 1000000) % 10;
                 d2 = (num / 10000000) % 10;
                 d1 = (num / 10000000) % 10;
                  
                 /*prints out the intger's digits starting with the rightmost digit */
                 printf("rightmost digit: %d\n", d8);
                 printf("%d\n", d7);
                 printf("%d\n", d6);
                 printf("%d\n", d5);
                 printf("%d\n", d4);
                 printf("%\n", d3);
                 printf("%d\n",d2);
                 printf("leftmost digit: %d\n", d1);
                  
                 /* Tells the user the number is divisible by 9 */
                 printf("your number is divisible by 9\n");
          }
       else
          {
                    /* Peels off the integer's digits and assigns them to a variable*/
                 d8 = num % 10;
                 d7 = (num /100) % 10;
                 d6 = (num / 1000) % 10;
                 d5 = (num / 1000) % 10;
                 d4 = (num / 10000) % 10;
                 d3 = (num / 10000) % 10;
                 d2 = (num / 10000) % 10;
                 d1 = (num / 100000) % 10;
    
    
                 /*prints out the intger's digits starting with the rightmost digit */
                 printf("rightmost digit: %d\n", d8);
                 printf("%d\n",  d7);
                 printf("%d\n",  d6);
                 printf("%d\n",  d5);
                 printf("%d\n",  d4);
                 printf("%\n",  d3);
                 printf("%d\n",  d2);
                 printf("leftmost digit: %d\n", d1);
                  
                 /* Tells the user the number isnt divisible by 9 */
                 printf("your number is not divisible by 9\n");
          }
           
            system("pause");
        return(0);
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    use an array and an index instead of discrete variables to hold the digits. strip off each rightmost digit in the loop until num == 0
    Code:
    int d[8];
    int index;
    ...
    
    while(num is nonzero) {
       d[iindex] = rightmost digit of current value of num
       num = shift num right so you have a new rightmost digit
       increment index
    }
    at this point, 'index' tells you how many digits you have

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    if ( num % 9 >= 0)
    The % operator is the modulus or remainder operator. If you wanted to know whether a number was divisible by 9, simply check the remainder is 0:
    Code:
    if (num % 9 == 0) {
        // number is divisible by 9
    }
    else {
        // number is not divisible by 9
    }
    However, I suppose that is not the method you wanted to use. Rather you want to use something like the "sum all the digits and if that number is divisible by 9, so is the original number" method. Basically, you repeat that until you are left with a 1-digit number. If that is 9, your original number was divisible by 9. If not, it wasn't.

    dmh2000 has a good idea for generally isolating each digit, however, if you are after something more like what I suggested, then I wouldn't bother with the array, or the d1...d8 variables you initially used. I think you could get away with two integers, num and digit_sum.

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    6
    Hmm thanks for the suggestions I'll let you know how this goes I would've replied sooner but I was busy with some family matters

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    6
    So I actually got it going well thanks to all the suggestions I've also managed to start working on it in an arrayed version as well
    Code:
    
    
    #include <stdio.h>
    #include <stdlib.h>
     
    /* Determines whether or not the users integer is divisible by 9 and displays all numbers withing integer*/
     
    int main(void)
    {
     
        int  num;  /* Users inputed integer */
    	int digit_sum; 
    	
    	
    	double answer;
         
        printf("please enter any digis to see if its divisable by 9. \n");
        scanf("%d", &num);
        
    	answer =(digit_sum , + num) / 9;
    
    
        if ( num % 9 >= 0)
           {
     
                   /* Peels off the integer's digits and assigns them to a variable*/
                digit_sum = num % 10;
    			
    			
                  
                 /*prints out the intger's digits starting with the rightmost digit */
                 printf("rightmost digit: %d\n", digit_sum);
    			 
                
                  
                 /* Tells the user the number is divisible by 9 */
                 printf("your number is divisible by 9\n");
          }
       else
          {
                 
                 
                 /* Tells the user the number isnt divisible by 9 */
                 printf("your number is not divisible by 9\n");
          }
    	answer = (digit_sum, +  num) / 9; 
           
            system("pause");
        return(0);
    }

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    answer =(digit_sum , + num) / 9;
    What are you trying to achieve with this line?

    Code:
    if ( num % 9 >= 0)
    This expression will always be true. % 9 returns the remainder of the division by 9 which is a number between 0 and 8.

    Bye, Andreas

  7. #7
    Registered User
    Join Date
    Jun 2013
    Posts
    6
    Quote Originally Posted by AndiPersti View Post
    Code:
    answer =(digit_sum , + num) / 9;
    What are you trying to achieve with this line?

    Code:
    if ( num % 9 >= 0)
    This expression will always be true. % 9 returns the remainder of the division by 9 which is a number between 0 and 8.

    Bye, Andreas
    You're losing me on this I'm trying to work over a program where a user can enter a number(s) to see if the number/digits are divisible by 9
    So going by your post I'm doing something wrong? I haven't ran any additional numbers yet to see if the program was wrong due to some family issues and internet issues but I'm open to any advice from you Andreas or numbers to test out on the program

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    answer =(digit_sum , + num) / 9;
    That line is syntactically correct, i.e. the compiler will accept it, however I'm quite certain it does not do what you want. Take a look at some examples of how the comma operator works in C. Your code is similar to the second example, i = (a, b); where b is stored in i. But in your case, a is digit_sum and b is + num. So the result of (digit_sum , + num) is just + num. But the + in this case is not addition (since you have a comma there), and instead is a unary +. It's just like making a value negative by prefixing it with a -, but instead it does not change the sign, it is ineffective. Thus, your expression is reduced to
    Code:
    answer = num / 9;
    The second point Andreas was making is that, no matter what value num has, num % 9 will always be a number between 0 and 8 inclusive. Think about when you learned to do division by hand, and you had remainders. If you divide by 9, you can only have remainders of 0, 1, 2, 3, 4, 5, 6, 7 or 8. If you have a remainder of 9 or more, you did something wrong (another whole 9 could go into the dividend). Thus, the expression
    Code:
    if (num % 9 >= 0)
    will always be true. Thus the conditional is pointless, and else part will never execute.

    Start with this:
    Write a function that takes a number and returns the sum of the digits. Test it like so:
    Code:
    int sum_digits(int num)
    {
        // your code here
    }
    
    int main(void)
    {
        printf("The sum of digits of 0 is %d\n", sum_digits(0));  // should be 0
        printf("The sum of digits of 123 is %d\n", sum_digits(123));  // should be 6
        printf("The sum of digits of 9876540 is %d\n", sum_digits(9876540));  // should be 39
        // come up with a few more tests you can verify by hand
        return 0;
    }
    Try to create that function, and post back if you have trouble. Once that function is done, we can work on checking if a number is divisible by 9. Note, this comes from the rules of divisibility: divisibility rules

    EDIT:
    Note that the rule for divisibility by 9 can be repeated. For example, if you sum the digits of the number
    18465822168759 --> 1+8+4+6+5+8+2+2+1+6+8+7+5+9 = 72
    If you can't remember whether 72 is divisible by 9, just repeat the process again, this time using 72:
    72 --> 7 + 2 = 9
    We know 9 is divisible by 9 (it's the only 1-digit number that is), so we know 18465822168759 is divisible by 9.

    That is why I'm having you start with just the digit sum function. It is fundamental to this problem.
    Last edited by anduril462; 06-17-2013 at 05:02 PM.

  9. #9
    Registered User
    Join Date
    Jun 2013
    Posts
    6
    Code:
    #include<stdio.h>#include<stdlib.h>
    int main(){
      int num,sum=0, pause ,r;
      printf("Enter a number: ");
      scanf("%d",&num);
      while(num){
          r=num%10;
          num=num/10;
          sum=sum+r;
      }
      printf("Sum of digits of number:  %d",sum);
    
    
      system("pause");
    
    
      return 0;
    }
    Okay so this is what I managed to work on with the program as far as adding up the sum of digits together .

    I don't know if this was exactly what you were talking about but I didn't feel right just coming back saying I didn't understand without at least having something done and working

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That's definitely what I was getting at. You have the logic down, I was just looking for you to put it into a function. Also, you can shorten up your function and get rid of the temp variable r:
    Code:
    int sum_digits(int num) {
        int sum = 0;
    
        while (num) {
            sum += num % 10;  // add the right-most digit to sum
            num /= 10;  // divide num by 10, eliminating the right-most digit
        }
    
        return sum;
    }
    Now you can use that function in your main logic. There are some hints in post #3 and my EDIT to post #8 for how to do this.

  11. #11
    Registered User
    Join Date
    Jun 2013
    Posts
    6
    Quote Originally Posted by anduril462 View Post
    That's definitely what I was getting at. You have the logic down, I was just looking for you to put it into a function. Also, you can shorten up your function and get rid of the temp variable r:
    Code:
    int sum_digits(int num) {
        int sum = 0;
    
        while (num) {
            sum += num % 10;  // add the right-most digit to sum
            num /= 10;  // divide num by 10, eliminating the right-most digit
        }
    
        return sum;
    }
    Now you can use that function in your main logic. There are some hints in post #3 and my EDIT to post #8 for how to do this.
    So wouldn't I need a Do function to start the while loop? That's where I'm lost I can do if, else but yeah

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Benevolence View Post
    So wouldn't I need a Do function to start the while loop? That's where I'm lost I can do if, else but yeah
    I don't know what a "Do function" is. Do you mean a do-while loop? Like this:
    Code:
    do {
        call sum_digits
        do some other stuff
    } while (some condition is true);
    If that's what you're asking about, you do not need a do-while loop. A do-while loop always executes at least once. But if the original number is already 1 digit, no need to do anything. Just use a regular while loop, which may not execute if the initial condition is already true, i.e. if the number is already one digit:
    Code:
    while (number has more than one digit) {
        sum_digits
        maybe some other stuff
    }

  13. #13
    Registered User
    Join Date
    Jun 2013
    Posts
    17
    cool

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Some Guidance
    By Ocean Chow in forum C Programming
    Replies: 6
    Last Post: 03-03-2013, 05:37 PM
  2. Need further guidance
    By jlimz in forum C Programming
    Replies: 3
    Last Post: 07-26-2010, 06:34 PM
  3. Little guidance please...
    By Jayhawk_26 in forum C Programming
    Replies: 5
    Last Post: 10-09-2006, 01:27 AM
  4. Guidance from anyone?
    By JJ1 in forum C++ Programming
    Replies: 3
    Last Post: 07-29-2003, 07:43 PM
  5. Not help so much as guidance.....
    By RobR in forum C++ Programming
    Replies: 0
    Last Post: 02-22-2002, 07:19 AM