By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,233 Members | 1,880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,233 IT Pros & Developers. It's quick & easy.

pls help me banfa

P: 18
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.
Nov 8 '06 #1
Share this Question
Share on Google+
3 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
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?
Nov 8 '06 #2

P: 18
my trouble is i donot construct this program?
Nov 9 '06 #3

sivadhas2006
100+
P: 142
Hi,

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

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. int main(int argc, char* argv[])
  7. {
  8.    int
  9.       nInput = 0,
  10.       nIndex = 0,
  11.       nInputLength = 0,
  12.       nOddDigitsTotal = 0,
  13.       nEvenDigitsTotal = 0;      
  14.    char
  15.       szTemp[2],
  16.       szInput[10];
  17.  
  18.    // Get the input.
  19.    cout << "Enter the number : ";
  20.    cin >> nInput;
  21.  
  22.    // Convert to string.
  23.    itoa(nInput, szInput, 10);
  24.  
  25.    // Get the length of the string.
  26.    nInputLength = strlen(szInput);
  27.    szTemp[1] = 0;
  28.    for(nIndex = 0; nIndex < nInputLength; nIndex++)
  29.    {
  30.       szTemp[0] = szInput[nIndex];
  31.       if(nIndex % 2 == 1)
  32.       {         
  33.          nOddDigitsTotal = nOddDigitsTotal + atoi(szTemp);
  34.       }
  35.       else
  36.       {
  37.          nEvenDigitsTotal = nEvenDigitsTotal + atoi(szTemp);
  38.       }      
  39.    }
  40.  
  41.    // Check whether the odd digits total is e
  42.    if(nOddDigitsTotal == nEvenDigitsTotal)
  43.    {
  44.       cout << "The " << nInput << " is divisible by 11.";
  45.    }
  46.    else
  47.    {
  48.       cout << "The " << nInput << " is not divisible by 11.";
  49.    }   
  50.    return 0;
  51. }
  52.  
  53.  
  54.  
Regards,
M.Sivadhas.
Nov 9 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.