Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Replace multiples numbers with char

10 views
Skip to first unread message

srikanth

unread,
Apr 1, 2011, 7:49:57 AM4/1/11
to
Hi All,
I am from non technical background and also i am new to c programming.
I got one question from one of my friend. print 100 numbers and
replace multiples of 3 and 5 with T and F. I am able to print 100
numbers but i can't find multiples from 100 numbers,
#include <stdio.h>
int main(){
int i;
for(i=1; i<=100; i++)
{
printf("%i\n",i);
}
return(0);
}

can any one tell me how to go to next step.
Thanks in advance.

Geoff

unread,
Apr 1, 2011, 4:46:31 PM4/1/11
to

Sounds like a homework problem. What are you supposed to print when
the number is a multiple of both three and five.

Clue: modulo operator.

srikanth

unread,
Apr 4, 2011, 11:45:31 PM4/4/11
to
Hello Geoff,
Thanks for your reply.
It should print T and F for multiples of 3 and 5.

Geoff

unread,
Apr 5, 2011, 7:43:38 AM4/5/11
to

I thought so.

#include <stdio.h>

int main (int argc, const char * argv[])
{
int i;

for (i = 1; i <= 100; i++)
{
if (i % 3 == 0 && i % 5 == 0)
printf("TF\n");
else if (i % 3 == 0)
printf("T\n");
else if (i % 5 == 0)
printf("F\n");
else
printf("%i\n", i);
}
return 0;
}

Now, what did you learn from this exercise?

What does % do?
What does && mean?
What does == mean?
Why do we write "if (i % 3 == 0)" and not "if (!i % 3)"?
Can this function be simplfied?

Keith Thompson

unread,
Apr 5, 2011, 4:17:17 PM4/5/11
to
Geoff <ge...@invalid.invalid> writes:
> On Mon, 4 Apr 2011 20:45:31 -0700 (PDT), srikanth
> <srikan...@gmail.com> wrote:
[...]

>>Hello Geoff,
>>Thanks for your reply.
>>It should print T and F for multiples of 3 and 5.
>
> I thought so.
>
[code snipped]

>
> Now, what did you learn from this exercise?
[...]

He's learned that he can get someone else to do his homework for him
just by asking.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Geoff

unread,
Apr 6, 2011, 12:56:05 AM4/6/11
to
On Tue, 05 Apr 2011 13:17:17 -0700, Keith Thompson <ks...@mib.org>
wrote:

>Geoff <ge...@invalid.invalid> writes:
>> On Mon, 4 Apr 2011 20:45:31 -0700 (PDT), srikanth
>> <srikan...@gmail.com> wrote:
>[...]
>>>Hello Geoff,
>>>Thanks for your reply.
>>>It should print T and F for multiples of 3 and 5.
>>
>> I thought so.
>>
>[code snipped]
>>
>> Now, what did you learn from this exercise?
>[...]
>
>He's learned that he can get someone else to do his homework for him
>just by asking.

It's all part of my diabolical plan to create an army of
cargo-cultists and copy-pasters who can't create an original program
or write good code. They will get good grades in school by stealing
other people's work, get well-paying jobs in foreign locations and
write crap applications, spoiling their products and diminishing
profits, thereby making domestic programmers look better, saving jobs
for America.

"Designed by a few guys at Apple, manufactured for pennies by
thousands in China. Bringing vast profits to a company that hasn't
paid a shareholder dividend since 1995."

0 new messages