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

S

Stefan Ram

srikanth said:
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" ); }
 
M

Malcolm McLean

#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;
}
 
M

Mark Bluemel

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

Stefan Ram

Malcolm McLean said:
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" ); }}
 
S

Stefan Ram

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

Lew Pitcher

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?FizzBuzzTest).

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

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top