Thread: need little help in understanding a program code.

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    12

    need little help in understanding a program code.

    The program checks for repeated numbers.

    Code:
    #include<stdbool.h>
    #include<stdio.h>
    
    int main (void)
    {
    
    bool digit_seen[10]={false};
    
    int digit ;
    long n;
    
    printf("Enter a number:");
    scanf("%ld", &n);
    
    
    while(n>0)
    
    {
    
    digit=n%10;
    
    if ( digit_seen[digit] )
    break;
    
    digit_seen[digit] =  true;
    n/=10;
    }
    
    if (n>10)
    printf("Repeated numbers\n");
     else
    printf("No Repeated numbers\n");
    
    
    return 0;
    
    }
    here is the part of the code which i m finding little hard to understand--

    especially this part.
    ----------------------------------------------------------------------------
    if ( digit_seen[digit] )
    break;
    ----------------------------------------------------------------------------
    Code:
    while(n>0)
    
    {
    
    digit=n%10;
    
    if ( digit_seen[digit] )
    break;
    
    digit_seen[digit] =  true;
    n/=10;
    }

    thanks for helping.

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Try following a number like 9892, or your own number choice, through the code logic.
    Last edited by gemera; 04-19-2015 at 08:44 PM.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Take for example the entered number is 353.
    The array digit_seen[10] is initialized with flase which is 0 hence it is
    Code:
    digit_seen[10] = {0};
    First condtion while(n>0) hence 353 satisfies it enters into the loop.
    digit = n % 10 = 353 % 10 = 3 is the remainder.
    Next condition
    if(digit_seen[digit])
    break;
    this evaluates to false as digit_seen[3] is false initially.

    digit_seen[digit] = true; this evaluates to
    digit_seen[3] = 1; ------------(1)
    next statement n/=10; this evaluates to n = 353/10 = 35. The while loop continues.

    similary for 35,

    Next n will be 3.

    In this case also while(n >0) satisfies and hence goes into the loop.
    here if(digit_seen[digit]) evaluates to digit_seen[3] = 1 from the equation (1) hence this condition satisfies and executes the break statement and comes out of the while loop.
    since the value of n is 3 which is less than 10 it prints "No Repeated Numbers". But actually i am finding the repeated numbers. Please verify the logic in the last condition it is if(n > 10).

  4. #4
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    It appears to be a slightly wonky copy from somewhere like here.

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    12
    Thanks for the explanation Satya

    and yes, you are right, it's a typing mistake.i am sorry regarding it.

    i wrote (n>10) instead of (n>0) ...
    Code:
    if (n>0)
    Code:
    printf("Repeated numbers\n");
     else
    printf("No Repeated numbers\n");
    
    
    return 0;
    
    }
    
    


    thats the actually code.

  6. #6
    Registered User
    Join Date
    Feb 2014
    Posts
    12
    @gemera: the code seems to be exactly same. :P

    but i didnt copy it from that website. i m learning c from the book C:A modern approach by k.n king.
    and this code is written as an example to understand how arrays works in chapter 8-"ARRAYS" of this book.



    THANKS for the help guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding this code
    By jim_0 in forum C++ Programming
    Replies: 2
    Last Post: 09-07-2014, 07:02 AM
  2. Help understanding this code
    By jim_0 in forum C++ Programming
    Replies: 1
    Last Post: 11-27-2013, 08:45 AM
  3. Need help understanding this code
    By sameertelkar in forum C Programming
    Replies: 11
    Last Post: 01-04-2013, 07:36 AM
  4. Need Help in Understanding this Code
    By Linked_List in forum C Programming
    Replies: 3
    Last Post: 09-04-2012, 09:32 PM
  5. Help understanding a Code
    By shiroaisu in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2010, 10:05 AM