pls help me banfa

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anarghya
    New Member
    • Oct 2006
    • 18

    pls help me banfa

    an integer is divisible by 11 if the sum of the digits in odd position
    equates to the sum of the digits in even position of the integer.Write
    a program in C to find whether a given integer is divisible by 11
    following the above rule.
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by anarghya
    an integer is divisible by 11 if the sum of the digits in odd position
    equates to the sum of the digits in even position of the integer.Write
    a program in C to find whether a given integer is divisible by 11
    following the above rule.
    Where are you having trouble?

    Comment

    • anarghya
      New Member
      • Oct 2006
      • 18

      #3
      my trouble is i donot construct this program?

      Comment

      • sivadhas2006
        New Member
        • Nov 2006
        • 142

        #4
        Hi,

        This may be help your need.
        This is one way to implement your logic.

        Code:
        #include <iostream.h>
        #include <string.h>
        #include <stdlib.h>
        
        int main(int argc, char* argv[])
        {
           int
              nInput = 0,
              nIndex = 0,
              nInputLength = 0,
              nOddDigitsTotal = 0,
              nEvenDigitsTotal = 0;      
           char
              szTemp[2],
              szInput[10];
        
           // Get the input.
           cout << "Enter the number : ";
           cin >> nInput;
        
           // Convert to string.
           itoa(nInput, szInput, 10);
        
           // Get the length of the string.
           nInputLength = strlen(szInput);
           szTemp[1] = 0;
           for(nIndex = 0; nIndex < nInputLength; nIndex++)
           {
              szTemp[0] = szInput[nIndex];
              if(nIndex % 2 == 1)
              {         
                 nOddDigitsTotal = nOddDigitsTotal + atoi(szTemp);
              }
              else
              {
                 nEvenDigitsTotal = nEvenDigitsTotal + atoi(szTemp);
              }      
           }
        
           // Check whether the odd digits total is e
           if(nOddDigitsTotal == nEvenDigitsTotal)
           {
              cout << "The " << nInput << " is divisible by 11.";
           }
           else
           {
              cout << "The " << nInput << " is not divisible by 11.";
           }   
           return 0;
        }
        Regards,
        M.Sivadhas.

        Comment

        Working...