plz send logic to write given program.

P

pitamber kumar

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

Sjouke Burry

pitamber 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 are not supposed to do your homework.
And dont think you will ever learn anything by cheating.
 
J

Johannes Bauer

pitamber 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.

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

You're welcome.

Kind regards,
Johannes
 
A

Amandil

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

Ok, I wrote it. Now you write one yourself, post it, and I'll see how
they compare.

In general, do not post homework questions. If you want to hide the
fact that it's a homework problem, show us what you have done, and
we'll see that you're serious. We might even help you out.

Hint: Use a loop to run through the numbers from 100 to 200. Check
each number if it's divisible by 7: If it is, add it to a running
total.

-- Marty
 
K

Keith Thompson

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.

Done.

Now it's your turn.

(No, we will not do your homework for you.)
 
O

osmium

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.

Look at the modulo operator. Perversely, this is represented by '%'.
 
S

santosh

pitamber 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.

This is trivially easy. Show us your effort and you'll likely get good
help. But we don't do homework on demand.
 
E

Eric Sosman

pitamber 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.

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

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int l,char**s){static const short o[]={23,4,5,-2,-5,-11,14,
9,-5,1,-6,};int n,m;if(l!=2){fprintf(stderr,"Usage:\n\t%s \"Do your"
" own homework next time, vile cheater!\"\n",*s);return EXIT_FAILURE;
}for(m=n=puts(*++s)<0;m<(int)sizeof o;m+=sizeof(short)){n+=o[m/sizeof
(short)];putchar(toupper((unsigned char)(n[*s])));}puts("");return 0;}
 
J

Johannes Bauer

Eric said:
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
 
R

Richard Heathfield

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;
}
 
E

Eric Sosman

Johannes said:
Eric said:
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.
 
M

Martin Ambuhl

pitamber 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.


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>
 
J

Johannes Bauer

Johannes said:
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
 
R

Randy Howard

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.
 
B

Bartc

Johannes Bauer said:
Johannes said:
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
 
I

Ian Collins

Bartc said:
Johannes Bauer said:
Johannes said:
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 "--"
 
K

Kenny McCormack

Johannes Bauer said:
Johannes said:
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?
 
M

MisterE

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.

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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top