[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

[comp.lang.c.moderated]Re: Replace multiples of 3 and 5 with T and F

ram

4/9/2011 2:35:00 PM

srikanth <srikanth007m@gmail.com> writes:
>I am new to c programing. Recently one of my friend attended a
>interview and the interviewer asked a question to write a program
>which prints 100 numbers and from those 100 numbers replace multiples
>of 3 and 5 with T and F.

#include <stdio.h>
int main( void ){ for( int i = 0; i < 100; ++i )puts( "1" ); }

5 Answers

Malcolm McLean

4/11/2011 12:37:00 PM

0

On Apr 9, 5:34 pm, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
> srikanth <srikanth0...@gmail.com> writes:
> >I am new to c programing. Recently one of my friend attended a
> >interview and the interviewer asked a question to write a program
> >which prints 100 numbers and from those 100 numbers replace multiples
> >of 3 and 5 with T and F.
>
> #include <stdio.h>
> int main( void ){ for( int i = 0; i < 100; ++i )puts( "1" ); }
>

It's a bit trickier than it looks because a number can be a multiple
of both 3 and 5, and it's not specified what the program should do.
However most probably the user wants a "TF".
#include <stdio.h>

int main(void)
{
int i;

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

return 0;
}

Mark Bluemel

4/11/2011 1:00:00 PM

0

On 04/11/2011 01:37 PM, Malcolm McLean wrote:
> On Apr 9, 5:34 pm, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
>> srikanth<srikanth0...@gmail.com> writes:
>>> I am new to c programing. Recently one of my friend attended a
>>> interview and the interviewer asked a question to write a program
>>> which prints 100 numbers and from those 100 numbers replace multiples
>>> of 3 and 5 with T and F.
>>
>> #include<stdio.h>
>> int main( void ){ for( int i = 0; i< 100; ++i )puts( "1" ); }
>>
>
> It's a bit trickier than it looks because a number can be a multiple
> of both 3 and 5, and it's not specified what the program should do.
> However most probably the user wants a "TF".
> #include<stdio.h>
>
> int main(void)
> {
> int i;
>
> for(i=0;i<100;i++)
> {
> if( (i % 3)&& (i % 5))
> printf("%d ", i);
> else
> {
> if(i % 3)
> printf("T");
> if(i % 5)
> printf("F");
> printf(" ");
> }
> }
> printf("\n");
>
> return 0;
> }

I'm trying to work out whether you've been subtle here or not...

ram

4/11/2011 1:33:00 PM

0

Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
>However most probably the user

the interviewer

> wants a "TF".

>#include <stdio.h>

>int main(void)
>{
> int i;

> for(i=0;i<100;i++)
> {
> if( (i % 3) && (i % 5))
> printf("%d ", i);
> else
> {
> if(i % 3)

;else

> printf("T");
> if(i % 5)

;else

> printf("F");
> printf(" ");
> }
> }
> printf("\n");

> return 0;
>}

Or,

#include <stdio.h>

int main( void )
{ for( int i = 0; i < 100; ++i )
{ int const r3 = i % 3;
int const r5 = i % 5;
int const p = r3 && r5;
int const s = r3 || r5;
if( p )printf( "%d", i ); else printf( s ? r3 ? "F" : "T" : "TF" );
printf( " \n" ); }}

ram

4/14/2011 11:53:00 AM

0

ram@zedat.fu-berlin.de (Stefan Ram) writes:
>int const s = r3 || r5;
>if( p )printf( "%d", i ); else printf( s ? r3 ? "F" : "T" : "TF" );

This unnecessarily computes s even if its value is not used.
It also calls ·printf· multiple times. A more efficient version:

#include <stdio.h>

int main( void )
{ puts
( "TF 1 2 T 4 F T 7 8 T F 11 T 13 14 TF 16 17 T 19 F T"
" 22 23 T F 26 T 28 29 TF 31 32 T 34 F T 37 38 T F 4"
"1 T 43 44 TF 46 47 T 49 F T 52 53 T F 56 T 58 59 TF"
" 61 62 T 64 F T 67 68 T F 71 T 73 74 TF 76 77 T 79 "
"F T 82 83 T F 86 T 88 89 TF 91 92 T 94 F T 97 98 T" ); }


Lew Pitcher

4/21/2011 5:12:00 PM

0

On Apr 11, 8:37 am, Malcolm McLean <malcolm.mcle...@btinternet.com>
wrote:
> On Apr 9, 5:34 pm, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
>
> > srikanth <srikanth0...@gmail.com> writes:
> > >I am new to c programing. Recently one of my friend attended a
> > >interview and the interviewer asked a question to write a program
> > >which prints 100 numbers and from those 100 numbers replace multiples
> > >of 3 and 5 with T and F.
>
> > #include <stdio.h>
> > int main( void ){ for( int i = 0; i < 100; ++i )puts( "1" ); }
>
> It's a bit trickier than it looks because a number can be a multiple
> of both 3 and 5, and it's not specified what the program should do.
> However most probably the user wants a "TF".

Pardon my late posting, but I just ran across something (elsethread)
that relates: this "challenge" looks a lot like the "FizzBuzz" test
(http://c2.com/cgi/wiki?Fi...).

If it is, then one solution might be:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
unsigned int count;

for (count = 1; count <= 100; ++count)
{
char number[4];
sprintf(number,"%u",count);
printf("%s%s\n",(count%3&&count%5)?number:(count%3?"":"T"),count
%5?"":"F");
}
return EXIT_SUCCESS;
}


Or, less obfuscated:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
unsigned int count;

for (count = 1; count <= 100; ++count)
if (count%3 && count%5)
printf("%u\n",count);
else
printf("%s%s\n",count%3?"":"T",count%5?"":"F");
return EXIT_SUCCESS;
}