Velocity Reviews > plz send logic to write given program.

# plz send logic to write given program.

Johannes Bauer
Guest
Posts: n/a

 03-02-2008
Eric Sosman schrieb:

> Here you go. Invoke it exactly as described in the
> "Usage" message, or it may misbehave.

Humm, I did. It reports, after printing argv[Â¹]:

XIV, MMCVII

That's not quite the solution - am I misunderstanding something here?

Regards,
Johannes

--
"PS: Ein Realname wÃ¤re nett. Ich selbst nutze nur keinen, weil mich die
meisten hier bereits mit Namen kennen." -- Markus Gronotte aka Makus /
Kosst Amojan / maqqusz / Mr. G / Ferdinand Simpson / Quartillia
Rosenberg in dse <45608268\$0\$5719\$(E-Mail Removed)-online.net>

Richard Heathfield
Guest
Posts: n/a

 03-02-2008
pitamber kumar said:

> Write a program to find the number of and sum of all intergers greater
> than 100 & less than 200 that are divisible by 7.

We can simplify the program by making some elementary observations up
front.

The number of integers in the range 101 to 199 that are divisible by 7 is
the same as the number in the range 1 to 99, although possibly there is an
extra integer. As it happens, 98 is evenly divisible by 7, giving a
quotient of 14, so we deduce that there are fourteen integers in the range
1-99. The first integer in the range 101 to 199 that is divisible by 7 is
98+7=105, and the last is 196, so we haven't managed to squeeze another
one in there. (That doesn't happen till 301-399, in fact.)

Now we know there are fourteen terms in this arithmetic series. Clearly
they can be divided into seven pairs, each totalling 301, and there are
seven such pairs, giving 2107.

So our final C program is:

#include <stdio.h>
int main(void)
{
puts("(a) 14 (b) 2107");
return 0;
}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999

Eric Sosman
Guest
Posts: n/a

 03-02-2008
Johannes Bauer wrote:
> Eric Sosman schrieb:
>
>> Here you go. Invoke it exactly as described in the
>> "Usage" message, or it may misbehave.

>
> Humm, I did. It reports, after printing argv[Â¹]:
>
> XIV, MMCVII
>
> That's not quite the solution - am I misunderstanding something here?

Well, I may have made a misteak -- but I don't think
I did. The output looks correct to me, if we assume the
O.P.'s "intergers" are integers. If they're not, then I'm
completely lost; I don't know how to deal with intergers.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid

Martin Ambuhl
Guest
Posts: n/a

 03-02-2008
pitamber kumar wrote:
> Write a program to find the number of and sum of all intergers greater
> than 100 & less than 200 that are divisible by 7.

This is not a question about C. It doesn't even have anything to do
with programming. That makes if off-topic. However, I will provide you
with a complete answer.
<ot>
The first integer greater than 100 divisible by 7 is 105.
Every one of the integers 105+7k < 200 is divisible by 7.
for 105+7k < 200 to be true, 7k < 95, and k <= 13.
So there are 14 such numbers for k in {0, 1, ... 13).
This should not surprise you, since (200-100) = 100 = 14*7+2.
The sum of these numbers is
sum(105+7k) for k = 0, ..., 13
which is 14*105 + 7*sum(k) for k = 0, ..., 13
or 1470 + 7*13*14/2 = 2107

So here is the program

#include <stdio.h>
int main(void)
{
printf("There are 14 integers greater than 100 and less than 200\n"
"which are divisible by 7. Their sum is 2107.\n");
return 0;
}

That which is easy to do analytically rarely calls for a programming
solution.

</ot>

Johannes Bauer
Guest
Posts: n/a

 03-02-2008
Johannes Bauer schrieb:

> XIV, MMCVII
>
> That's not quite the solution - am I misunderstanding something here?

Yes I am! My program is broken. I didn't read the instructions throughly
enough. Oh well. :-/

Here it goes again:

q=0; z=0; i=101; while [ \$i -lt 200 ]; do if [ \$((\$i%7)) -eq 0 ]; then
z=\$((\$z+\$i)); q=\$((\$q+1)); fi; i=\$((\$i+1)); done; echo \$q \$z

Regards,
Johannes

--
"PS: Ein Realname wÃ¤re nett. Ich selbst nutze nur keinen, weil mich die
meisten hier bereits mit Namen kennen." -- Markus Gronotte aka Makus /
Kosst Amojan / maqqusz / Mr. G / Ferdinand Simpson / Quartillia
Rosenberg in dse <45608268\$0\$5719\$(E-Mail Removed)-online.net>

Randy Howard
Guest
Posts: n/a

 03-02-2008
On Sun, 2 Mar 2008 14:25:34 -0600, pitamber kumar wrote
(in article
<(E-Mail Removed)>):

> Write a program to find the number of and sum of all intergers greater
> than 100 & less than 200 that are divisible by 7.

I got an error when I tried to send it to you.

It said:

Error: Recipient incompatible with logic. Send aborted.

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Bartc
Guest
Posts: n/a

 03-02-2008

"Johannes Bauer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Johannes Bauer schrieb:
>
>> XIV, MMCVII
>>
>> That's not quite the solution - am I misunderstanding something here?

>
> Yes I am! My program is broken. I didn't read the instructions throughly
> enough. Oh well. :-/
>
> Here it goes again:
>
> q=0; z=0; i=101; while [ \$i -lt 200 ]; do if [ \$((\$i%7)) -eq 0 ]; then
> z=\$((\$z+\$i)); q=\$((\$q+1)); fi; i=\$((\$i+1)); done; echo \$q \$z

What language is this in? It's obviously not C, but apparently not Perl
either.

I had to convert to the following (with the help of a 10-min crash course in
Perl) to get it to work:

\$q=0; \$z=0; \$i=101; while ( \$i < 200 ) { if ( (\$i%7) == 0 )
{\$z=(\$z+\$i); \$q=(\$q+1)}; \$i=(\$i+1); } print \$q," ",\$z

--
Bart

Ian Collins
Guest
Posts: n/a

 03-03-2008
Bartc wrote:
> "Johannes Bauer" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Johannes Bauer schrieb:
>>
>>> XIV, MMCVII
>>>
>>> That's not quite the solution - am I misunderstanding something here?

>> Yes I am! My program is broken. I didn't read the instructions throughly
>> enough. Oh well. :-/
>>
>> Here it goes again:
>>
>> q=0; z=0; i=101; while [ \$i -lt 200 ]; do if [ \$((\$i%7)) -eq 0 ]; then
>> z=\$((\$z+\$i)); q=\$((\$q+1)); fi; i=\$((\$i+1)); done; echo \$q \$z

>
> What language is this in? It's obviously not C, but apparently not Perl
> either.
>

bash/ksh shell.

>
> --

Your sig is missing the trailing space after the "--"

--
Ian Collins.

Kenny McCormack
Guest
Posts: n/a

 03-03-2008
In article <j3Hyj.16868\$(E-Mail Removed)> ,
Bartc <(E-Mail Removed)> wrote:
>
>"Johannes Bauer" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>> Johannes Bauer schrieb:
>>
>>> XIV, MMCVII
>>>
>>> That's not quite the solution - am I misunderstanding something here?

>>
>> Yes I am! My program is broken. I didn't read the instructions throughly
>> enough. Oh well. :-/
>>
>> Here it goes again:
>>
>> q=0; z=0; i=101; while [ \$i -lt 200 ]; do if [ \$((\$i%7)) -eq 0 ]; then
>> z=\$((\$z+\$i)); q=\$((\$q+1)); fi; i=\$((\$i+1)); done; echo \$q \$z

>
>What language is this in? It's obviously not C, but apparently not Perl
>either.

It is equally clearly not Fortran.

Can we draw up an exhaustive list of languages that it isn't?

MisterE
Guest
Posts: n/a

 03-03-2008

"pitamber kumar" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Write a program to find the number of and sum of all intergers greater
> than 100 & less than 200 that are divisible by 7.

int main(int argc, char **argv)
{
int i,j=0;
for (i=101;i<200;i++)
{
printf("Number %d\n",i);
if (i%7==0) j+=i;
}
printf("Sum %d\n",j);
}