Solve the given C Puzzle
March 15th, 2010http://farm3.static.flickr.com/2052/2086499468_1f153310e7_o.gif
Currently you are witnessing C puzzle 12
The answer to this puzzle will be given soon till i frame another puzzle..
Don't forget to visit these other threads started by me
1.Lesser known facts in C (http://www.thinkdigit.com/forum/showthread.php?t=71047):facts that you never knew about C
2.C in linux/unix (http://www.thinkdigit.com/forum/showthread.php?t=71889):Bored of using TC,switch to linux.All about running C programs in linux/unix
3.Presenmaker 1.0 (http://www.thinkdigit.com/forum/showthread.php?t=74832):Presenmaker stands for presentation maker. It's a free software developed by me which can be used to create interactive agent animations in seconds. By using Presenmaker you can convert a lengthy text file into agent animation. It is very useful to create interactive presentations, tutorials, to read lengthy lessons etc
Index
1.Puzzle 1[#2]
2.Puzzle 2[#14]
3.Puzzle 3[#25]
4.Puzzle 4[#36]
5.Puzzle 5[#42]
6.Puzzle 6[#49]
7.Puzzle 7[#57]
8.Puzzle 8[#73]
9.Puzzle 9[#97]
10.Puzzle 10[#118]
11.Puzzle 11[#155]
11.Puzzle 12[#162]
First,let me state the rules in this thread:
1.Please do not give suggestions or hints.Specify the modified program only.
2.Before posting the program make sure it's working in the way i want.
Here is the 12th C puzzle
write a c program which gives the output as below..
Hello!
Continue(y/n)
Hello!
Continue(y/n)
Bye,press any key to exit
^^When i press 'y' the program prints Hello! once more and if i press any other key the program ends
If i didn't specify the rules then anyone can solve this :D:D
Rules
maximum of 2 header files -stdio.h and conio.h
no variables,constants
no structures,pointers,user defined functions..
no looping structure(for ,while,dowhile etc..)
no goto statement
the words if and else shouldn't appear in the whole program
conio.h should be included only for getche() i.e, getch() etc.. are not allowed
Note :Specify your modified program along with the answer
Awards gallary:
Total puzzles solved :11
anuj919 3
me (If no one gets the solution then points will be for me) 2
saurabh kakkar 2
fun2sh 2
eggman 1
nigthtcrawler 1
a_k_s_h_a_y 1
anantkhaitan 1
khattam 1
maddy354 1
Leading:anuj919
#include
main()
{
char a[1000];
printf("Enter the stringn");
gets(a);
printf("Enter the character whose frequency is to be determinedn");
scanf("%c",&a[997]);
a[998]='0';a[999]='0';a[1000]='0';
for(;a[(a[998]-48)]!=' ';a[998]++)
{
if(a[997]==a[(a[998]-48)])
a[1000]++;
}
a[998]--;
for(;a[(a[999]-48)]!=' ';a[999]++)
{
if (a[(a[999]-48)]!=a[(a[998]-48)])
{
printf("n%s is not a palindromen",a);
break;
}
a[998]--;
}
if(a[(a[999]-48)]==' ')
printf("n%s is a palindromen",a);
for(a[998]='0';a[(a[998]-48)];a[998]++);
printf("The string "%s" contains %d characters and '%c' occurs %d timesn",a,a[998]-48,a[997],(a[1000]-48));
}
confused ??:confused::confused::confused::confused::
Don't worry.All explanation will be given on next tuesday....
@offtopic
HEY ADI also MINE 3rd INTERNAL R GOIN ON!!!
but me is givin only unix exam coz i got avg of (24-25)/25 in rest subject. i didnt study for unix in 2nd internal coz of illness
@offtopic
We have only 2 internals and the marks are added without reduction.Internals is for 20 marks.So 20+20+5(assignments)+5 (teachers wish)=50.The final exam will be condected for 100 marks and will be reduced to 50.
Sick.isn't it :sad::sad::sad:
@ontopic
can we use global variable and this operator ::
Sorry:( i didn't read this post .I just typed the program and tested and posted it without refreshing the thread....
So please can we use getch/getche?
puzzle 9 solved
But yes Adithya is true:idea: , the program works fine without including "conio.h"
& can anyone tell me that how this happens in Turbo C++ 3.0 :confused: :confused:
^^I had told this already but nobody believed me :sad::sad:
Thanks for supporting me...:D:D:D
New puzzle tommorow at 12:15 PM
has some settings been change of ur TC++ compiler??
^^nope..
Yeah how does stdio.h cover clrscr as your code example claims so? Use standard code mate, and allow a wider population to join.
^^ i will be careful from
It's just like normal char...
how do u enter smiley after string in cmd prompt ?
Hitting CTRL+B gives ^B
getche() and getch() comes under stdio.h(tested in TC++ 3.0)..
If you cannot tell ANSI/ISO standard C from a system-dependent implementation (i.e., Turbo C++), you ought to invest more time in learning the language rather than posting non-standard, useless, and tricky puzzles here.
here is a simple c program
#include
void main()
{
char str[100];
printf("Enter any stringn");
scanf("%s",str);
printf("n You entered string %s n",str);
}
The output of the above program is
Enter any string
This is a test
You entered string This
As you can see the scanf will stop accepting the string as soon as it encounters white space or special chars.
Now modify the above programs such that the output will be
Enter any string
This is a test
You entered string This is a test
the rules are
1.No new variables
2.You have to use only scanf to accept input
3.No new header files
Edited:
To show the first puzzle as requested by Adi
1.The keyword if or operator '?' should come only once in a program
2.No logical operator's are allowed (that means && ! should not be used)
3.No new variabels must be used
4.No header file other than the existing one should be used
Rules Updated
char *string;
oh didnt read properly. cant use that statement :sad:
oh didnt read properly. cant use that statement :sad:
i created the solution program by using conio.h for clrscr()
then i removed clrscr() and #include
then i made some modifications to the solution program and compiled it...
It worked fine..
I tested the program by using both getch() and getche() without using conio.h and it worked....:confused::confused:
wait ..
I will confirm it later by creating the program from the scratch......
Solution for 3rd C Puzzle
#include
#include
void main()
{
enum days{
sunday=1,
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
};
//THIS IS IMP STEP
days a,b,c,d,e,f,g;
a=sunday,
b=monday,
c=tuesday,
d=wednesday,
e=thursday,
f=friday,
g=saturday;
clrscr();
printf("%dn%dn%dn%dn%dn%dn%dn",a,b,c,d,e,f,g);
getch();
}
OR
#include
#include
void main()
{
enum days{
sunday=1,
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
};
//THIS IS IMP STEP
days a=sunday,b=monday,c=tuesday,d=wednesday,e=thursday ,f=friday,
g=saturday;
clrscr();
printf("%dn%dn%dn%dn%dn%dn%dn",a,b,c,d,e,f,g);
getch();
}
I am planning to upload the runtime vedio of the program on monday or tuesday...
that means that the input and output will be on same line
It would be nice if u can provide screenshot
#include
#include
void main()
{
enum days{
sunday=1,
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
};
enum days a,b,c,d,e,f,g;
a=sunday,
b=monday,
c=tuesday,
d=wednesday,
e=thursday,
f=friday,
g=saturday;
clrscr();
printf("%dn%dn%dn%dn%dn%dn%dn",a,b,c,d,e,f,g);
getch();
}
puzzle solved by saurabh kakkar.But i just think that you have to specify 'enum days' instead of simply days.:confused:
Did you executed the program saurabh...
Also a round of applause to piyush gupta who first suggested the use of enum.I just hope that from next puzzle onwards he will suggest the program also.
Please specify your modified program along with the answer.
no responses???:(:(
i thinks sourabh solved the puzzle...
hope u will giv eme some credit at least for the idea
whats next buddy :)
make sure there is no * in the whole program....
n which functions r not allowed??????????
user defined functions are not allowed....
u must make sure that there is only one header file
Hurry......
time is running...
Answer will be given on NOV 23
Last day to answer the puzzle...
Will give answer tommorow....
Looks like i will get the point :D:D
#include
#include
#include
void main()
{
long double pi=3.14159265,a,r;
int pre;
clrscr();
printf("Enter precision of pi between 0 and 8n");
scanf("%d",&pre);
printf("Enter radiusn");
scanf("%Lf",&r);
pi = floor(pi *pow(10, pre)) / pow(10, pre); // this will set the precision according to input value
a = pi * r * r;
printf("The Value of pi taken is %1.*3$Lf and the area of the circle is %Lfn", pre, pi, a);
getch();
}
The Output
Enter precision of pi between 0 and 8
6
Enter radius
25.3
The Value of pi taken is 3.141592 and the area of the circle is 2010.901623
Enter precision of pi between 0 and 8
4
Enter radius
25.3
The Value of pi taken is 3.1415 and the area of the circle is 2010.842735
Enter precision of pi between 0 and 8
7
Enter radius
25.3
The Value of pi taken is 3.1415926 and the area of the circle is 2010.902007
I think you will find this correct :)
it is not taking 27 as a character. it is just taking 2 as char and assigning it to 'a'.Remaining 7 to variable 'b''.The other value to 'c'.The last decimal value is discarded.
http://farm3.static.flickr.com/2052/2086499468_1f153310e7_o.gif
Currently you are witnessing C puzzle 9
First,let me state the rules in this thread:
1.Please do not give suggestions or hints.Specify the modified program only.
2.Before posting the program make sure it's working in the way i want.
Here is the 9th C puzzle
this is the same 8th puzzle but there are somes rules..
Write a c program which gives accepts words and at the end of a word,it will replace the word by it's reverse.This is done until a smiley is encountered.At the end it will print "Have a nice day".That is if i entered i luv c and i think it's rocks :) will give the output as..
to be more explainable
Enter the string
i luv_
as soon i press space,it will become
Enter the string
i vul _
Rules
1.No user defined functions..
2.No usage of clrscr()
3.No header file other than stdio.h should be used.
4.No * symbol in the whole program
Note :Specify your modified program along with the answer
Awards gallary:
Puzzles solved :8
me (If no one gets the solution then points will be for me) 2
saurabh kakkar 2
fun2sh 2
eggman 1
nigthtcrawler 1
anuj919 1
Leading:saurabh kakkar,fun2sh and myself
puzzle 9 added thread updated!!
Here is a simple C program
#include
#include
void main()
{
char a;
int b,c;
clrscr();
printf("Enter a character and 2 integer valuesn");
scanf("%c%b%c",&a,&b,&c);
getch();
}
This is an program which accepts a character followed by 2 integer values.
NOW ALL I WANT TO DO IN THIS PROGRAM IS TO CHECK WHETHER THE USER HAS ENTERED CORRECT INPUT(FIRST CHARACTER AND FOLLOWED BY THE 2 INTEGER VALUES.
It should display "That's Good" if the user has inputted in correct sequence else it should display "That's Bad".
Here are some sample ouput's
Output:
Enter a character and 2 integer values
a 23 45.96
That's good
^^ decimal values are rounded off.Hence it is valid input
Output:
Enter a character and 2 integer values
2 23 45.96
That's good
^^'2' is also a character
Output:
Enter a character and 2 integer values
2 ad 45.96
That's bad
^^ ad is not a decimal value.
The following are the Rules:
1.The keyword if or operator '?' should come only once in a program
2.No logical operator's are allowed (that means && ! should not be used)
3.No new variabels must be used
4.No header file other than the existing one should be used
Note :Specify your modified program along with the answer
Awards gallary:
Puzzles solved :1
me (If no one gets the solution then points will be for me) 0
saurabh kakkar 1
eggman 1
Leading:saurabh kakkar and eggman
New puzzle added.Thread updated.
No responses :( I think either the puzzle is too difficult or i have not made some points clear
:confused:
Not even one try:)
That's bad
Look's like i am going to get my first point:D:D:D:D:D:D:D
and i hav made this program. will post it as soon i get my laptop. Abhi me admitted in hospital since 17th.
i an just facin 1 problem in my program.for '27' OR any 2digit no, for first variable its tellin 'GOOD'
By the way,how C compiler decides Garbage value in case of dataoverflow
Find this out in my lesser known facts in c thread.
http://www.thinkdigit.com/forum/showthread.php?p=655539#post655539
#include
#include
void main()
{
long signed int a,b;
clrscr();
puts("enter the value for a");
(fscanf(stdin, "%ld", &a));
puts("enter the value for b");
(fscanf(stdin, "%ld", &b));
printf("%ld" "+" "%ld" "=" "%ld"" and ""%ld" "-" "%ld" "=" "%ld",a,b,a+b,a,b,a-b);
getch();
}
I can not provide the output as I m using Turbo C++ compiler
http://www.thinkdigit.com/forum/showpost.php?p=654017&postcount=21
yeah seen that.
During runtime also you have to press CTRL B to enter smiley
CTRL B gives ^B not a smiley in dos prompt
#include
void main()
{
char str[100];
printf("Enter any stringn");
scanf("%[^n]s",str); //<----- Imp step--------
printf("n You entered string %s n",str);
}
yeah my bad:D:D:D
No pointers,functions,structures,unions......
then how will one store a string (as arrays are also pointers)
n which functions r not allowed??????????
Anyways,a round of applause to nightcrawler
Linux version of this program is
#include
#include
main()
{
long double pi=3.14159265,a,r;
int pre;
printf("Enter precision of pi between 0 and 8n");
scanf("%d",&pre);
printf("Enter radiusn");
scanf("%Lf",&r);
pi = floor(pi *pow(10, pre)) / pow(10, pre);
a = pi * r * r;
printf("The Value of pi taken is %.*Lf and the area of the circle is %Lfn", pre, pi, a);
}
Note:
The compilation of this program will sometimes lead to an error
Then you have to compile this program by suffixing -lm at end of cc command
i.e,
cc adi.c -lm
assuming that adi.c is the filename
find more about floor function at my Lesser Known Facts in C thread
http://www.thinkdigit.com/forum/showpost.php?p=652953#post652953
:!:But there is another method to solve this puzzle without using floor function.So if there is someone other than nightcrawler who know this, then post it here within tommorow.1 point will be given to that person also.
The other answer will be given tommorow.
Another method to solve the puzzle without using floor function
#include
#include
#include
void main()
{
long double pi=3.14159265,a,r;
int pre;
clrscr();
printf("Enter precision of pi between 0 and 8n");
scanf("%d",&pre);
printf("Enter radiusn");
scanf("%Lf",&r);
pi=(long)(pi*pow(10, pre));
pi=pi/pow(10, pre);
a = pi * r * r;
printf("The Value of pi taken is %.*Lf and the area of the circle is %Lfn", pre, pi, a);
getch();
}
printf("The Value of pi taken is %.*Lf and the area of the circle is %Lfn", pre, pi, a);
^^
More info about this statement at my Lesser known facts in C thread
http://www.thinkdigit.com/forum/showthread.php?p=652971#post652971
First,let me state the rules in this thread:
1.Please do not give suggestions or hints.Specify the modified program only.
2.Before posting the program make sure it's working in the way i wan.
Here is the 5th C puzzle
Write a c program which gives the output as
Enter the value of a
4561256
Enter the value of b
6565665
4561256+6565665= 11126921 and 4561256-6565665= -2004409
If i don't specify the rules than anyone can solve this.:D:D:D
The rules are
1.Only 2 variables are allowed
2.scanf is not allowed
3.only one printf is allowed
4.the symbol '&' shouldn't come in the program
5.No escape sequence is allowed(n,t, .....)
Note :Specify your modified program along with the answer
Awards gallary:
Puzzles solved :4
me (If no one gets the solution then points will be for me) 0
saurabh kakkar 2
eggman 1
fun2sh 1
nigthtcrawler 1
Leading:saurabh kakkar
Puzzle 5 added! Thread updated
:!:nightcrawl attention please:
I have compiled your c program and the following is the output of your c program:
Enter precision of pi between 0 and 8
6
Enter radius
25.3
The Value of pi taken is 3.141593 and the area of the circle is 2010.902263
But i have stated that
pre - radius - pi value taken - Area
0 - 25.3 - 3 - 1920.270000
1 - 25.3 - 3.1 - 1984.279000
2 - 25.3 - 3.14 - 2009.882600
3 - 25.3 - 3.141 - 2010.522690
4 - 25.3 - 3.1415 - 2010.842735
5 - 25.3 - 3.14159 - 2010.900343
6 - 25.3 - 3.141592 - 2010.901623
7 - 25.3 - 3.1415926 - 2010.902007
8 - 25.3 - 3.14159265 - 2010.902039
so your output should be like this
Enter precision of pi between 0 and 8
6
Enter radius
25.3
The Value of pi taken is 3.141592 and the area of the circle is 2010.901623
means no round off should takes place
Your c program is making roundoff for 4,6,7 values of precesion
waise now one question can we use strtok() function n gotoxy() function???
u can use a global variable usin :: operator
forexample 'a' is a global variable n local to
so
'a' will mean local
while
::a wil mean global
getche() and getch() comes under stdio.h(tested in TC++ 3.0)..
HEY ADI also MINE 3rd INTERNAL R GOIN ON!!!
but me is givin only unix exam coz i got avg of (24-25)/25 in rest subject. i didnt study for unix in 2nd internal coz of illness
@ontopic
can we use global variable and this operator ::
it works..
1 point to anuj919
The program hint is given by the rule that no & must be used.That means we have to use string.And use a function to convert the value stored by the string into int or long type....
Here is the program.......
#include
#include
void main()
{
char a[10],b[10];
puts("Enter the value of a");
gets(a);
puts("Enter the value of b");
gets(b);
printf("%s+%s=%ld and %s-%s=%ld",a,b,(atol(a)+atol(b)),a,b,(atol(a)-atol(b)));
getch();
}
I have opened my account :D:D:D:D:D:D
what is atol()???:confused::confused:
find this out at my lesser known facts in C thread
http://www.thinkdigit.com/forum/showthread.php?goto=lastpost&t=71047
Hi! I am Adithya U,17 year old Engineering Student(IT) from Hassan,Karnataka
Currently you are witnessing C puzzle 6
First,let me state the rules in this thread:
1.Please do not give suggestions or hints.Specify the modified program only.
2.Before posting the program make sure it's working in the way i want.
Here is the 6th C puzzle
Write a c program which gives the output as
Enter the string
adithya
Start
a
d
i
t
h
y
a
End
Enter the string
adi 007
Start
a
d
i
0
0
7
End
If i don't specify the rules than anyone can solve this.:D:D:D
The rules are
1.Only 2 variables are allowed
2.No header file other than stdio.h is allowed.
3.gets should be used for input.
4.Only printf should be used for output(That means puts,putch,... not allowed)
5.No escape sequences or backslash characters are allowed(n,t, .....)
6.Only one for loop is allowed.
while,do while, if and it's variants,?.. <---not allowed
7.The printf statement should not be blank
i.e,
printf(" ");
^^not allowed
Note :Specify your modified program along with the answer
Awards gallary:
Puzzles solved :5
me (If no one gets the solution then points will be for me) 1
saurabh kakkar 2
eggman 1
fun2sh 1
nigthtcrawler 1
Leading:saurabh kakkar
Puzzle 6 added!!Thread updated!!
smiley means smiley[:)] not this -->:)
read this http://www.thinkdigit.com/forum/showpost.php?p=654017&postcount=21
Believe me it's not so easy...:D:D:D
I hav done revesing the string.
Clarify some points:
Will there be upper bound to the length of the string ? or link list will be used ?
And that smiley thing will be ASCII code or a real jpeg file ?
I really dont like C :D:D:D, here is the bad code, someone can refine it further (I hav forgotten a lots of things in C):
#include
#include
int recReverse( char *tempStrPtr )
{
int finalInt;
if (tempStrPtr == NULL)
finalInt = 0;
else if (*tempStrPtr == ' ')
finalInt = 0;
else if (*tempStrPtr == ' ')
finalInt = 0;
else
finalInt = recReverse(tempStrPtr+1)+1;
if(finalInt)
putchar(*tempStrPtr);
return finalInt;
}
int main ( )
{
char initStrPtr[20],*finalStrPtr;
int size=0;
gets(initStrPtr);
finalStrPtr=initStrPtr;
int incr=0;
while(finalStrPtr && *finalStrPtr)
{
incr = recReverse(finalStrPtr);
finalStrPtr += incr;
if(*finalStrPtr==' ')
{
putchar(' ');
finalStrPtr++;
}
}
return 0;
}
The Output, with stack overflow:
jit@jit-desktop:~$ ./a.out
i love c and i think it rocks
*** stack smashing detected ***: ./a.out terminated
i evol c dna i kniht ti skcorAborted (core dumped)
The Bad:
1) This thing will overflow like hell
2) gets() is a dangerous escape
3) No smiley return
to be more explainable
Code:
Enter the string
i luv_
as soon i press space,it will become // you hav already pressed spacwe before, how will the program know that this space is the end
Code:
Enter the string
i vul _
#include
void main()
{
char str[100];
printf("Enter any stringn");
scanf("%[^n]s",str); //<----- Imp step--------
printf("n You entered string %s n",str);
}
#include
#include
void main()
{
clrscr();
char *a;
printf("enter a string %c",10); // ascii value of 'n' is 10
gets(a);
printf("%cStart%c",10,10);
for(;*a!=' ';a++)
{
printf("%c%c",*a,10);
}
printf("%cEnd",10);
getch();
}
I have solved this puzzle only with single variable
i luv digit forum :) but i hate spamer
do u expect output like
i vul tigid murof
please post it in the lesser known facts in c thread ....
Hi! I am Adithya U,17 year old Engineering Student(IT) from Hassan,Karnataka
Currently you are witnessing C puzzle 8
First,let me state the rules in this thread:
1.Please do not give suggestions or hints.Specify the modified program only.
2.Before posting the program make sure it's working in the way i want.
Here is the 8th C puzzle
Write a c program which gives accepts words and at the end of a word,it will replace the word by it's reverse.This is done until a smiley is encountered.At the end it will print "Have a nice day".That is if i entered i luv c and i think it's rocks :) will give the output as..
Enter the string
i vul c dna i kniht s'ti skcor :)
Have a nice day
to be more explainable
Enter the string
i luv_
as soon i press space,it will become
Enter the string
i vul _
There are no rules for this puzzle
Note :Specify your modified program along with the answer
Awards gallary:
Puzzles solved :7
me (If no one gets the solution then points will be for me) 2
saurabh kakkar 2
fun2sh 2
eggman 1
nigthtcrawler 1
Leading:saurabh kakkar,fun2sh and myself
puzzle 8 added!Thread updated
So please can we use getch/getche?
what....:shock::shock::shock:
getch() and getche() comes under stdio.h :confused::confused:
wait let me confirm it.....
It's just like normal char...
i an just facin 1 problem in my program.for '27' OR any 2digit no, for first variable its tellin 'GOOD'
That's Ok.Because it depends upon the computer and the compiler.In my home i use linux and i have extracted the output from it.After trying this in TurboC++,i faced the same problem as you.Actually what's happening is it taking only a single character from 27 and assigning it to char a.The remaining '7' is assigned to int b and the next decimal number is assigned to int c.And the last number is discarded.
So,I have removed this output
Output:
Enter a character and 2 integer values
27 23 45.96
That's bad
^^27 is not a character
No need to satisfy this condition:!::!::!:
I hope now everything is clear :confused:
Sorry:(:(I think i made a mess of this puzzle
Unfortunately, i couldn't give point to him since he answered lately.
Dude this is not done U should award me also cos I did'nt knew at that time that puzzle 6 has been put
and from next time onwards plz mention the date nd Time at which u will post new puzzle :)
by the way I m very busy with my studies so May be i will not be able to participate :(
regards
saurabh kakkar
no constants...
wat do u mean by
then how will one store a string (as arrays are also pointers)
n which functions r not allowed??????????
arrays are not pointers..
There are the separate group of structured datatype..
int main() {
char str[100], c;
int i = 0;
printf("Enter any stringn");
do {
scanf("%c", &c);
str[i++] = c;
} while (c != 'n' && i < 99);
str[i] = ' '
printf("n You entered string %s n",str);
return 0;
}
EDIT : changed to &&.
Not yet compiled and checked ...
But i think what the program is doing is accept the string and then clear the screen and then print the reverse of the string.This is wrong because I said the program is dynamic.....
Anyways ,I have prepared the animated gif run time output,will post it tommorow......
no its reversing the word as soon as u press space, its dynamic.
I dunno wat else can be dynamic other than that ?
getche() and getch() comes under stdio.h(tested in TC++ 3.0)..
no adi. it cant be!:shocked: i checked it again now n its comes in conio.h only!!n i hav the same tc as urs. even in the help file of tc its mentioned conio.h!!
:!::!::!:
I can not provide the output as I m using Turbo C++ compiler
How to save the output to a text file in TC++,find this out in my lesser known facts in c thread
http://www.thinkdigit.com/forum/showthread.php?goto=lastpost&t=71047
Here is a simple C program
#include
#include
void main()
{
char a;
int b,c;
clrscr();
printf("Enter a character and 2 integer valuesn");
scanf("%c%b%c",&a,&b,&c);
getch();
}
This is an program which accepts a character followed by 2 integer values.
NOW ALL I WANT TO DO IN THIS PROGRAM IS TO CHECK WHETHER THE USER HAS ENTERED CORRECT INPUT(FIRST CHARACTER AND FOLLOWED BY THE 2 INTEGER VALUES.
It should display "That's Good" if the user has inputted in correct sequence else it should display "That's Bad".
Here are some sample ouput's
sorry for late responce m very busy :(
I can solve this Program using ctype.h header file if thats the way to solve thats fine :) if not plz post the answer urself :)
no...
smiley means smiley[:)] not this -->:)
read this http://www.thinkdigit.com/forum/showpost.php?p=654017&postcount=21
its easy man, may be if i'll get time will post here
Common interview question.
Btw am not a C fanatic
Believe me it's not so easy...:D:D:D
Sorry but it was puzzle no. 8:D
#include
#include
void main()
{
char a;
float b,c;
clrscr();
printf("Enter a character and 2 integer valuesn");
b=scanf("%c %f %f",&a,&b,&c);
if(b!=3)
printf("n thats bad");
else
printf("n thats good");
getch();
}
Thats the puzzle......
do u expect output like
noooo....
As soon as the smiley encounters the program should end by displaying
Have a nice day
and the main thing is
There is no separate output and input.The program is dynamic
yeah seen that.
During runtime also you have to press CTRL B to enter smiley
CTRL B gives ^B not a smiley in dos prompt
I am planning to upload the runtime vedio of the program on monday or tuesday...
Can we use getch/getche or not?
HERE ITS IS :mrgreen:
#include
#include
void main()
{clrscr();
char *s;
int i;
printf("%c enter the string :",10);
gets(s);
printf("Start %c%c",10,10);
for(i=0;*(s+i)!=NULL;i++)
{printf("%c%c",*(s+i),10);
}
printf("%cEnd",10);
getch();
}
HAHAHAHAHAHA!!!!!!!!!!!!!
The answer to this puzzle will be given on Nov 5 if no one solves the puzzle
First,let me state the rules in this thread:
1.Please do not give suggestions or hints.Specify the modified program only.
2.Before posting the program make sure it's working in the way i wan.
Here is the 4th C Puzzle[A bit complicated one]
Here is a C program to find the area of the circle
#include
#include
#include
void main()
{
long double pi=3.14159265,a,r;
int pre;
clrscr();
printf("Enter precision of pi between 0 and 8n");
scanf("%d",&pre);
printf("Enter radiusn");
scanf("%Lf",&r);
/*Complete the program*/
getch();
}
NOW ALL I WANT YOU TO DO IN THIS PROGRAM IS TO COMPLETE PROGRAM SO THAT IT MACTHES THE OUTPUT.
Enter precision of pi between 0 and 8
3
Enter radius
25.3
The value of pi taken=3.141 and the area of the circle is 2010.522690
I hope you have understood the purpose of the program i.e,calculate the area of the circle by taking value of pi upto certain decimal point
:!:
There is another important point that i wished to specify i.e,
The output should be like this
The value of pi taken=3.141 and the area of the circle is 2010.522690
but not this
The value of pi taken=3.141000 and the area of the circle is 2010.522690
Similarly,for various value of precision the ouput should be
pre - radius - pi value taken - Area
0 - 25.3 - 3 - 1920.270000
1 - 25.3 - 3.1 - 1984.279000
2 - 25.3 - 3.14 - 2009.882600
3 - 25.3 - 3.141 - 2010.522690
4 - 25.3 - 3.1415 - 2010.842735
5 - 25.3 - 3.14159 - 2010.900343
6 - 25.3 - 3.141592 - 2010.901623
7 - 25.3 - 3.1415926 - 2010.902007
8 - 25.3 - 3.14159265 - 2010.902039
The following are the Rules:
1.If and it's variants,switch,?,for,while,do..while,goto -->not allowed
2.No new variables
3.No new header file other than math.h,conio.h,stdio.h
Note :Specify your modified program along with the answer
Awards gallary:
Puzzles solved :3
me (If no one gets the solution then points will be for me) 0
saurabh kakkar 2
eggman 1
fun2sh 1
Leading:saurabh kakkar
Puzzle 4 added.Thread Updated!!.
I had to write this twice because after writing once i clicked save and it asked my password and username,I specifed it but it just got discarded:(:(
Please rate this thread!!
I have done puzzle 8
Here is the Solution...
#include
#include
//Function to return the reverse the string given as argument
char *str_rev(char str[100])
{
char rev[100];
int i,j;
for(i=0;str[i]!=' ';i++);
i--;
for(j=0;i>=0;i--)
{
rev[j++]=str[i];
}
rev[j]=' ';
return rev;
}
void main()
{
char a[100][100],c; // a is two dimentional array to store diffrent strings
// c is temp variable
int i=0,j=0,k=0; // i is for i th string entered
// j is for j th letter of i th string
clrscr();
printf("Enter String n");
while((c=getche())!=':)') //:)=Ctrl+B
{
if(c!=' ')
{
a[i][j++]=c; //If space is not entered put char at the end of i th word (i.e,a[i])
}
else
{
a[i][j]=' '; //If space is entered put null character at the end of i th word
i++;j=0;k=0; // increment no of words & make j=0 for next use
//Now first clear the screen & start printing all the strings reversed
clrscr();
printf("Enter String n");
while(k {
printf("%s ",str_rev(a[k]));
k++;
}
}
}
//After smily print string in new line
printf("nHave a Nice Day");
getch();
}
:D:D:D
Thats the reason why its taking 27 as a character.
http://www.devlist.com/Default.aspx
No buddy there is no need to spectify 'enum days' :)
Did you executed the program saurabh...
Yes, before posting any c++ program I make sure to compile the program but since I use TurboC++ compiler so i can not paste the output :D
sorry:(:(:(:(
But i am helpless
and from next time onwards plz mention the date nd Time at which u will post new puzzle :)
^^I will implement this
by the way I m very busy with my studies so May be i will not be able to participate :(
That really sad:sad::sad:
puzzle 7 Schedule
Date:Nov16
Time:12:15 PM
Venue:this same thread:D:D:D
Hi! I am Adithya U,17 year old Engineering Student(IT) from Hassan,Karnataka
Currently you are witnessing C puzzle 7
First,let me state the rules in this thread:
1.Please do not give suggestions or hints.Specify the modified program only.
2.Before posting the program make sure it's working in the way i want.
Here is the 7th C puzzle
Write a c program which gives the output as
Enter the string
adithya
Enter the character whose frequency is to be determined
a
adithya is not a palindrome
The string "adithya" contains 7 characters and 'a' occurs 2 times
Enter the string
madam
Enter the character whose frequency is to be determined
e
madam is a palindrome
The string "madam" contains 5 characters and 'e' occurs 0 times
Enter the string
malayalam malayalam
Enter the character whose frequency is to be determined
m
malayalam malayalam is a palindrome
The string "malayalam malayalam" contains 19 characters and 'm' occurs 4 times
If i don't specify the rules than anyone can solve this.:D:D:D
The rules are
1.Only 1 variable is allowed
2.No header file other than stdio.h is allowed.
3.Only printf should be used for output(That means puts,putch,... not allowed)
4.No pointers,functions,structures,unions......
5.getche(),getch(),getchar()... not allowed
Note :Specify your modified program along with the answer
Awards gallary:
Puzzles solved :6
me (If no one gets the solution then points will be for me) 1
saurabh kakkar 2
fun2sh 2
eggman 1
nigthtcrawler 1
Leading:saurabh kakkar and fun2sh
puzzle 7 added!!.Thread updated
http://www.thinkdigit.com/forum/showpost.php?p=654017&postcount=21
@Sykora is also correct but do while make this simple problem complex & complexity also higher than saurabh .
My vote goes to saurabh .
int main() {
char str[100], c;
int i = 0;
printf("Enter any stringn");
do {
scanf("%c", &c);
str[i++] = c;
} while (c != 'n' && i < 99);
str[i] = ' '
printf("n You entered string %s n",str);
return 0;
}
^^ You have used new variables called 'c' 'i'. I told to modify the existing program.
So the winner is saurabh kakkar and eggman(Because he suggested the use of Edit set)
Well you can use Edit Set [...] instead of %s in the scanf function.....it'll solve the problem....
From next puzzle ,specify modified program also.
but wat if i give a string greater than 1000 chars???
se usin global variable this wont be a problem as we can use locale to store the string while global to do the things u hav done.
0
1
2
3
4
5
6
make some modifications..
Common interview question.
Btw am not a C fanatic
Not yet compiled and checked ...
But i think what the program is doing is accept the string and then clear the screen and then print the reverse of the string.This is wrong because I said the program is dynamic.....
Anyways ,I have prepared the animated gif run time output,will post it tommorow......
4.the symbol '&' shouldn't come in the program
where as you have used it twice
(fscanf(stdin, "%ld", &a));
(fscanf(stdin, "%ld", &b));
Non-standard and system/implementation dependent functions like getch(), getche() or clrscr() CANNOT reside in a standard header like stdio.h.
I just said to complete the statements after.That means you are not allowed to edit or add any statements before.
And secondly,you have used '?' operator which i said you shouldn't
/**precision > 8? *precision=8:(*precision < 0? *precision=0:;);*/ /* Commented because of if not allowed rule function call useless*/
1.If and it's variants,switch,?,for,while,do..while,goto -->not allowed
If anyone wants to try, try today itself because I will give the answer tommorow.
Please specify your modified program along with the answer.
no responses???:(:(
#define MAXLEN 100
int main() {
char str[MAXLEN], c;
int i = 0;
printf("Enter any stringn");
do {
scanf("%c", &c);
str[i++] = c;
} while (c != 'n' i < (MAXLEN-1)); /* Use && */
str[i] = ' ';
printf("n You entered string %s n",str);
return 0;
}
There is a little logic error here. You should have used AND in stead of OR. And, use of magic numbers should be discouraged.
If u declare a global and local variable with the same name then preference will be given to local variable.....
SO HOW CAN U USE BOTH OF THEM IF THEY ARE OF SAME NAME???:confused:
has some settings been change of ur TC++ compiler??
i found website that gives money for solving puzzles:cool: ;) , please help me in solving them :confused: :confused: :confused: . the website is www.jadook.com (http://www.jadook.com)
i know that you are very skilled puzzles solvers, plz help.
enum coin { ten_cent, quart_doll, half_doll, dollar };
An example declaration is:
enum coin money;
Given this definition and declaration, the following statements are valid:
money = quart_doll;
if(money == quart_doll)
printf("is 25 centsn");
In an enumeration, each symbol stands for an integer value. For example, using the above definition and declaration:
printf("%d %d", ten_cent, dollar);
displays 0 3 on the screen.
i thinks using them your puzzle can be solved easily...
i last worked on C in 2001 its long time and I cant write exact code here now...
#include
#include
void main()
{
enum days{
sunday=1,
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
};
//THIS IS IMP STEP
days a,b,c,d,e,f,g;
a=sunday,
b=monday,
c=tuesday,
d=wednesday,
e=thursday,
f=friday,
g=saturday;
clrscr();
printf("%dn%dn%dn%dn%dn%dn%dn",a,b,c,d,e,f,g);
getch();
}
OR
#include
#include
void main()
{
enum days{
sunday=1,
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
};
//THIS IS IMP STEP
days a=sunday,b=monday,c=tuesday,d=wednesday,e=thursday ,f=friday,
g=saturday;
clrscr();
printf("%dn%dn%dn%dn%dn%dn%dn",a,b,c,d,e,f,g);
getch();
}
I am extermely sorry for the mistake...
I referred the help documentation and some books and found that getch() ,getche() comes under conio.h...
So rules are modified
One can use getch(),getche() but must not use clrscr()
here are the modified rules
Rules
1.No user defined functions..
2.No usage of clrscr()
3.No header file other than stdio.h and conio.h should be used.
4.No * symbol in the whole program
Now i let me explain why i commited such a big mistake..
I am using TC++ 3.0 for the past 3 years and have never used conio.h for getch() and getche().:shock:So i thought these 2 comes under stdio.h
The following program works fine in my TC
#include
void main()
{
clrscr();
printf("Press any keyn");
getche();
getch();
}
and even this works fine
void main()
{
clrscr();
printf("Press any keyn");
getche();
getch();
}
I have made the vedio file and will upload and give the link within this week...
:-d
:-D
I am very very sorry that i asked such a simple puzzle :sad::sad: .fun2sh solved it on that day itself.The main problem while giving puzzles is u will not know whether the puzzle u asked is easy or not :confused::confused:. Whether the fact u know are known by others or not:confused::confused:.Normally ,i will ask my freinds the puzzle and if there are unable to answer then only i post the puzzle.
fun2sh has solved the puzzle.One point for him.
saurabh kakkar has given other possibility that could solve the puzzle by using only one variable.A big applause to him.Unfortunately, i couldn't give point to him since he answered lately.
New puzzle tommorow.I just hope that the puzzle is of high standard.....
2.There is no seperate input and output.The program is dynamic.
to be more explainable
Code:
Enter the string
i luv_
as soon i press space,it will become // you hav already pressed spacwe before, how will the program know that this space is the end
Code:
Enter the string
i vul _
^^underscore represents the cursor position
I hav done revesing the string.
Clarify some points:
And that smiley thing will be ASCII code or a real jpeg file ?
use CTRL B in TC editor and u will see yourself....
Will there be upper bound to the length of the string ?
^^yes
I had to write this twice because after writing once i clicked save and it asked my password and username,I specifed it but it just got discarded:(:(
Please rate this thread!!
Since no one has answered the question I will give my try. I hope it is correct
the question is
The question
#include
#include
#include
void main()
{
long double pi=3.14159265,a,r;
int pre;
clrscr();
printf("Enter precision of pi between 0 and 8n");
scanf("%d",&pre);
printf("Enter radiusn");
scanf("%Lf",&r);
/*Complete the program*/
getch();
}
Output
Enter precision of pi between 0 and 8
3
Enter radius
25.3
The value of pi taken=3.141 and the area of the circle is 2010.522690
The Possible Answer
#include
#include
#include
void main()
{
long double pi=3.14159265,a,r;
int pre;
clrscr();
printf("Enter precision of pi between 0 and 8n");
scanf("%d",&pre);
printf("Enter radiusn");
scanf("%Lf",&r);
pi = floor(pi *pow(10, pre) + 0.5) / pow(10, pre); // this will set the precision according to input value
a = pi * r * r;
printf("The Value of pi taken is %.*1$Lf and the area of the circle is %Lfn", pre, pi, a); /* .*1$ will take the precision value after the decimal from the first argument of the printf which should be an int which is pre */
getch();
}
EDIT: Made changes to the code. Complies with the requirement now I think.
#include
#include
void main()
{
char a;
int b,c;
clrscr();
printf("Enter a character and 2 integer valuesn");
if (scanf("%c %d %d",&a,&b,&c)==3)
printf("n thats good");
else
printf("n thats bad");
getch();
}
Find more info about this at my lesser known C facts thread http://www.thinkdigit.com/forum/showthread.php?p=640073#post640073
the answer given by fun2sh is similar to it.So the point goes to fun2sh.
Here is the 3rd C Puzzle
Here is a simple C program
#include
#include
void main()
{
/*Complete the declartions*/
clrscr();
a=sunday;
b=monday;
c=tuesday;
d=wednesday;
e=thursday;
f=friday;
g=saturday;
printf("%d/n%d/n%d/n%d/n%d/n%d/n%d/n",a,b,c,d,e,f,g);
getch();
}
NOW ALL I WANT YOU TO DO IN THIS PROGRAM IS TO COMPLETE DECLARATIONS SO THAT IT MACTHES THE OUTPUT.
Output:
1
2
3
4
5
6
7
The following are the Rules:
1.sunday,monday,tuesday,wednesday,thursday,friday, saturday etc are not varibles nor constants.Means
int sunday=1;
or
#define sunday 1
are not allowed
2.a,b,c,d,e,f,g are not of int datatype
int a;
^^ not allowed
Note :Specify your modified program along with the answer
Awards gallary:
Puzzles solved :2
me (If no one gets the solution then points will be for me) 0
saurabh kakkar 1
eggman 1
fun2sh 1
Leading:saurabh kakkar ,eggman,fun2sh
Puzzle 3 added.Thread updated
But yes Adithya is true:idea: , the program works fine without including "conio.h"
& can anyone tell me that how this happens in Turbo C++ 3.0 :confused: :confused:
Here is the answer:
#include
#include
void main()
{
char b[999];
int i=0,j=0;
char c;
printf("Enter the Stringn");
while((c=getche())!='')
{
if(c==' ')
{
for(j=0;j<=i;j++)
printf("b");
for(j=i-1;j>=0;j--)
printf("%c",b[j]);
printf(" ");
i=0;
}
else
b[i++]=c;
}
printf("nHave a Nice Day!");
getch();
}
(i have internals for the next 3 days :(:( and the answer program needs a lot of explanation).So,Three days extra time
EDIT:Answer in #69.Explanation and logic on next tuesday(Nov27).....
enum
{
sunday
monday
tuesday;
wednesday;
thursday;
friday;
saturday;
}weekdays
then you can decalre:
a,b,c,d,e,f,g of type weekdays
this will resolve the problem.
Here is the 3rd C Puzzle
Here is a simple C program
#include
#include
void main()
{
/*Complete the declartions*/
clrscr();
a=sunday;
b=monday;
c=tuesday;
d=wednesday;
e=thursday;
f=friday;
g=saturday;
printf("%d/n%d/n%d/n%d/n%d/n%d/n%d/n",a,b,c,d,e,f,g);
getch();
}
NOW ALL I WANT YOU TO DO IN THIS PROGRAM IS TO COMPLETE DECLARATIONS SO THAT IT MACTHES THE OUTPUT.
Output:
1
2
3
4
5
6
7
The following are the Rules:
1.sunday,monday,tuesday,wednesday,thursday,friday, saturday etc are not varibles nor constants.Means
int sunday=1;
or
#define sunday 1
are not allowed
2.a,b,c,d,e,f,g are not of int datatype
int a;
^^ not allowed
Note :Specify your modified program along with the answer
Awards gallary:
Puzzles solved :2
me (If no one gets the solution then points will be for me) 0
saurabh kakkar 1
eggman 1
fun2sh 1
Leading:saurabh kakkar ,eggman,fun2sh
No pointers,functions,structures,unions......
then how will one store a string (as arrays are also pointers)
n which functions r not allowed??????????
Date for the puzzle 8 has been extended till next Thursday...
#If you have any other info about this subject , Please add it free.# |