Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #70147

Re: A finalized program

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.lang.c
Subject Re: A finalized program
Date 2015-09-10 10:39 +0000
Organization Freie Universitaet Berlin
Message-ID <d5d4vrFlpkpU1@mid.uni-berlin.de> (permalink)
References <d93ca264-e41a-4f82-8c09-cd87731b2eed@googlegroups.com> <d586scFdva0U1@mid.uni-berlin.de> <e714d908-b358-471c-bf66-03f1be205cd9@googlegroups.com>

Show all headers | View raw


Alla _ <modelling.data@gmail.com> wrote:
> On Tuesday, September 8, 2015 at 5:41:49 PM UTC+4, Jens Thoms Toerring wrote:
> > Alla _ <modelling.data@gmail.com> wrote:
1> Sorry, I should have entered comments, but I
> hoped that the description of the task would be 
> suffice.
> The card_number can contain numbers, some of which
> when multiplied by two will give only one digit, and others
> will have two digits. If the result has only one digit, then
> I just sum up these digits; but if the result has 2 digits,
> I need to sum these two digits (products' digits), as 
> specified in the task. This is how it's done:

Thanks, didn't understand that. But then you still need to
check for non-digits the user may have entered.

With the Luhn algorithm (and if it's clear the input only
contains digits) the summation could be done in a single
loop with e.g.

    int sum = 0;
    size_t i = strlen( card_number );
    while ( i > 0 )
    {
        sum += card_number[ --i ] - '0';
        if ( i > 0 )
        {
            int n = 2 * card_number[ --i ] - '0';
            if ( n <= 9 )
                sum += n;
            else
                sum += 1 + n % 10;
        }

Note that, if there are only digits, twice the value can't
be larger than 18, so  if the doubled value is larger than
9 you just have to add 1 to the second digit of the resulting
number (i.e. to the modulo 10 of that number).

                        Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-08 04:13 -0700
  Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-08 04:19 -0700
    Re: A finalized program fir <profesor.fir@gmail.com> - 2015-09-08 04:30 -0700
    Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 14:45 +0100
      Re: A finalized program Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-08 08:29 -0700
      Re: A finalized program Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-08 08:52 -0700
        Re: A finalized program Martin Shobe <martin.shobe@yahoo.com> - 2015-09-08 13:01 -0500
        Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 21:00 +0100
          Re: A finalized program Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-08 13:33 -0700
            Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-09 00:28 +0100
  Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 14:26 +0100
    Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-08 06:42 -0700
      Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 20:50 +0100
        Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 01:47 -0700
  Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-08 13:41 +0000
    Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-10 01:31 -0700
      Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-10 10:39 +0000
        Re: A finalized program David Thompson <dave.thompson2@verizon.net> - 2015-09-20 18:35 -0400
    Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-10 01:33 -0700
    Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-10 10:16 -0700
      Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-10 20:08 +0200
        Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:05 -0700
          Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-11 10:17 +0100
            Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-11 10:19 +0100
            Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:28 -0700
            Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:29 -0700
        Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:19 -0700
          Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-11 10:23 +0100
            Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:33 -0700
              Re: A finalized program Keith Thompson <kst-u@mib.org> - 2015-09-11 14:11 -0700
                Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-11 21:39 +0000
          Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-11 12:49 +0200
      Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-10 23:31 +0100
        Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:15 -0700
      Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-10 22:37 +0000
        Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-10 23:47 +0100
          Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-11 10:18 +0000
        Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 01:07 -0700
        Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:14 -0700
          Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-11 10:10 +0000
            Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 04:19 -0700
  Re: A finalized program Barry Schwarz <schwarzb@dqel.com> - 2015-09-08 15:53 -0700
  Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 04:27 -0700
    Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-11 14:05 +0200
  Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 04:42 -0700
    Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-11 14:15 +0200
      Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 05:50 -0700
        Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-11 15:00 +0200
      Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 05:50 -0700
    Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-11 13:46 +0100
      Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-11 14:19 +0100

csiph-web