View Full Version : Solve the given C Puzzle
adi007
15-10-2007, 09:14 AM
Hi! I am Adithya U,17 year old Engineering Student(IT) from Hassan,Karnataka
http://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
casanova
15-10-2007, 05:10 PM
C Puzzle 1
here is a simple c program
#include<stdio.h>
void main()
{
char str[100];
printf("Enter any string\n");
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
aditya.shevade
15-10-2007, 05:18 PM
^^ Read man.... he says no use of anything other than scaf... why though?
eggman
15-10-2007, 05:36 PM
Well you can use Edit Set [...] instead of %s in the scanf function.....it'll solve the problem....
saurabh kakkar
15-10-2007, 05:55 PM
following Program will work :)
#include<stdio.h>
void main()
{
char str[100];
printf("Enter any string\n");
scanf("%[^\n]s",str); //<----- Imp step--------
printf("\n You entered string %s \n",str);
}
eggman
15-10-2007, 05:59 PM
^^ Read man.... he says no use of anything other than scaf... why though?
Thats the puzzle......
aditya.shevade
15-10-2007, 07:38 PM
^^
:-d
:-D
Sykora
15-10-2007, 07:56 PM
#include<stdio.h>
int main() {
char str[100], c;
int i = 0;
printf("Enter any string\n");
do {
scanf("%c", &c);
str[i++] = c;
} while (c != '\n' && i < 99);
str[i] = '\0'
printf("\n You entered string %s \n",str);
return 0;
}
EDIT : || changed to &&.
Yamaraj
15-10-2007, 08:48 PM
#include<stdio.h>
#define MAXLEN 100
int main() {
char str[MAXLEN], c;
int i = 0;
printf("Enter any string\n");
do {
scanf("%c", &c);
str[i++] = c;
} while (c != '\n' || i < (MAXLEN-1)); /* Use && */
str[i] = '\0';
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.
saurabh kakkar
15-10-2007, 09:50 PM
I have given the soultion already in post no 5 here it goes again
#include<stdio.h>
void main()
{
char str[100];
printf("Enter any string\n");
scanf("%[^\n]s",str); //<----- Imp step--------
printf("\n You entered string %s \n",str);
}
The_Devil_Himself
15-10-2007, 10:07 PM
well done sykora,saurabh,and yamaraj.You are the chamions.
adi007
16-10-2007, 11:21 AM
#include<stdio.h>
int main() {
char str[100], c;
int i = 0;
printf("Enter any string\n");
do {
scanf("%c", &c);
str[i++] = c;
} while (c != '\n' && i < 99);
str[i] = '\0'
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.
Projjwal
16-10-2007, 01:11 PM
i think saurabh kakkar is 100% correct.that's the right way to do it.
@Sykora is also correct but do while make this simple problem complex & complexity also higher than saurabh .
My vote goes to saurabh .
adi007
18-10-2007, 04:49 PM
Here is the 2nd C Puzzle
Here is a simple C program
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
int b,c;
clrscr();
printf("Enter a character and 2 integer values\n");
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
saurabh kakkar
18-10-2007, 07:06 PM
[QUOTE=adi007]Here is the 2nd C Puzzle
Here is a simple C program
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
int b,c;
clrscr();
printf("Enter a character and 2 integer values\n");
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 :)
adi007
19-10-2007, 08:52 AM
wrong mate.
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
adi007
22-10-2007, 10:27 AM
last day for the answers.Tommorow i will post solution.
fun2sh
22-10-2007, 10:35 AM
hey != is not a logical operator. its comparetion operator.
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'
adi007
22-10-2007, 11:18 AM
^^Sorry:confused: typing mistake it is '!' not '!=''.
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
fun2sh
22-10-2007, 12:21 PM
here is th solution thaen
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
float b,c;
clrscr();
printf("Enter a character and 2 integer values\n");
b=scanf("%c %f %f",&a,&b,&c);
if(b!=3)
printf("\n thats bad");
else
printf("\n thats good");
getch();
}
[xubz]
22-10-2007, 12:41 PM
Um. . 27 is a ASCII Code. 65 is for A, so if you do printf("%c", 65); it prints out 'A'.
Thats the reason why its taking 27 as a character.
http://www.devlist.com/Default.aspx
adi007
22-10-2007, 04:49 PM
no....,
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.
fun2sh
22-10-2007, 05:54 PM
hey! where r my points???
puzzleslover
23-10-2007, 03:41 AM
hi all,
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.
adi007
23-10-2007, 12:06 PM
Here is the solution for puzzle2
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
int b,c;
clrscr();
printf("Enter a character and 2 integer values\n");
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<stdio.h>
#include<conio.h>
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
piyush gupta
23-10-2007, 12:18 PM
Enumerations are defined much like structures. An example definition is as follows:
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 cents\n");
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...
adi007
24-10-2007, 12:07 PM
^^ i want only program.
Please specify your modified program along with the answer.
no responses???:(:(
saurabh kakkar
24-10-2007, 12:52 PM
EDIT: Problem solved
Solution for 3rd C Puzzle
#include<stdio.h>
#include<conio.h>
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("%d\n%d\n%d\n%d\n%d\n%d\n%d\n",a,b,c,d,e,f,g);
getch();
}
OR
#include<stdio.h>
#include<conio.h>
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("%d\n%d\n%d\n%d\n%d\n%d\n%d\n",a,b,c,d,e,f,g);
getch();
}
piyush gupta
24-10-2007, 04:30 PM
^^ i want only program.
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 :)
adi007
25-10-2007, 10:54 AM
I program suggested by saurabh will give the output like this
0
1
2
3
4
5
6
make some modifications..
saurabh kakkar
25-10-2007, 11:32 AM
^^ sorry dude I made mistake in hurry :( .here is the correct solution
#include<stdio.h>
#include<conio.h>
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("%d\n%d\n%d\n%d\n%d\n%d\n%d\n",a,b,c,d,e,f,g);
getch();
}
OR
#include<stdio.h>
#include<conio.h>
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("%d\n%d\n%d\n%d\n%d\n%d\n%d\n",a,b,c,d,e,f,g);
getch();
}
aditya.shevade
25-10-2007, 06:21 PM
^^ Correct.
you can use enum data type.
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<stdio.h>
#include<conio.h>
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
adi007
26-10-2007, 12:19 PM
Here is the solution for 3rd C Puzzle
#include<stdio.h>
#include<conio.h>
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("%d\n%d\n%d\n%d\n%d\n%d\n%d\n",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.
saurabh kakkar
27-10-2007, 09:32 AM
i just think that you have to specify 'enum days' instead of simply days.:confused:
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
adi007
29-10-2007, 01:08 PM
[B]
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<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long double pi=3.14159265,a,r;
int pre;
clrscr();
printf("Enter precision of pi between 0 and 8\n");
scanf("%d",&pre);
printf("Enter radius\n");
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!!
nightcrawler
03-11-2007, 09:52 PM
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!!
Since no one has answered the question I will give my try. I hope it is correct
the question is
The question
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long double pi=3.14159265,a,r;
int pre;
clrscr();
printf("Enter precision of pi between 0 and 8\n");
scanf("%d",&pre);
printf("Enter radius\n");
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<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long double pi=3.14159265,a,r;
int pre;
clrscr();
printf("Enter precision of pi between 0 and 8\n");
scanf("%d",&pre);
printf("Enter radius\n");
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 %Lf\n", 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.
adi007
05-11-2007, 10:33 AM
to nightcrawler :
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.
nightcrawler
05-11-2007, 11:34 AM
Hey I have commented it. So it is not being used if you look closely. Anyways I will remove that part all together.
adi007
05-11-2007, 12:57 PM
^^ please rewrite the program in other post along with the output's and please no comments
:!: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
nightcrawler
05-11-2007, 01:58 PM
Ok. I forgot that part as well. Anyways writing the code again wherin I don't round of the value of the number in floor call. Also made changes to printf statement. The program is running as expected on a Linux box minus getch, clrscr calls and conio.h header files and plus the int return format of main. The change to printf statement was to correct the absurd behaviour in linux box wherin the output was not being printed properly. I think this printf should also work in tc/devc++ or any other win32 C compiler
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long double pi=3.14159265,a,r;
int pre;
clrscr();
printf("Enter precision of pi between 0 and 8\n");
scanf("%d",&pre);
printf("Enter radius\n");
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 %Lf\n", 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 :)
adi007
06-11-2007, 03:07 PM
nightcrawler has solved the puzzle.This was a tough puzzle.I thought that i will get the point myself but it didn't happen.:(:(
Anyways,a round of applause to nightcrawler
Linux version of this program is
#include<stdio.h>
#include<math.h>
main()
{
long double pi=3.14159265,a,r;
int pre;
printf("Enter precision of pi between 0 and 8\n");
scanf("%d",&pre);
printf("Enter radius\n");
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 %Lf\n", 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<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long double pi=3.14159265,a,r;
int pre;
clrscr();
printf("Enter precision of pi between 0 and 8\n");
scanf("%d",&pre);
printf("Enter radius\n");
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 %Lf\n", pre, pi, a);
getch();
}
printf("The Value of pi taken is %.*Lf and the area of the circle is %Lf\n", 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
saurabh kakkar
06-11-2007, 10:54 PM
Puzzle 5 Solved
#include<stdio.h>
#include<conio.h>
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
adi007
07-11-2007, 12:02 PM
^^No saurab.Glance the rules carefully.
4.the symbol '&' shouldn't come in the program
where as you have used it twice
(fscanf(stdin, "%ld", &a));
(fscanf(stdin, "%ld", &b));
adi007
09-11-2007, 10:17 AM
No other try yet??:confused::confused:
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
nightcrawler
09-11-2007, 11:16 AM
I will give it a try if no one knows...but saurabh_kakkar has almost figured it out i think.
adi007
09-11-2007, 04:37 PM
^^will that means that you know the answer.If so,please do give the answer.
:!::!::!:
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
adi007
12-11-2007, 10:55 AM
Last day to solve the Puzzle.I will give answer tommorow.
Look's like i am going to get my first point:D:D:D:D:D:D:D
adi007
13-11-2007, 03:02 PM
Answer to puzzle5.It's really surprise that no one was able to solve this puzzle which was there for a week.
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<stdio.h>
#include<stdlib.h>
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!!
fun2sh
13-11-2007, 08:55 PM
thats real EASY
HERE ITS IS :mrgreen:
#include<conio.h>
#include<stdio.h>
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!!!!!!!!!!!!!
saurabh kakkar
14-11-2007, 04:17 PM
Puzzle 6 solved
#include<stdio.h>
#include<conio.h>
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!='\0';a++)
{
printf("%c%c",*a,10);
}
printf("%cEnd",10);
getch();
}
I have solved this puzzle only with single variable
adi007
15-11-2007, 11:44 AM
Ok ...
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.....
Pathik
15-11-2007, 03:04 PM
Great thread @ Adi
adi007
15-11-2007, 04:38 PM
^^thanks
fun2sh
15-11-2007, 04:54 PM
yeah ur puzzle was toooooo simple this time. a child play i say. but U R DOIN A GREAT JOB BUDDY.
saurabh kakkar
15-11-2007, 05:19 PM
...
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
adi007
16-11-2007, 12:40 PM
Dude this is not done U should award me also cos I did'nt knew at that time that puzzle 6 has been put
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
fun2sh
16-11-2007, 06:28 PM
is that really possible?
QwertyManiac
16-11-2007, 06:52 PM
Only 1 variable but constants are allowed as desired?
fun2sh
16-11-2007, 07:59 PM
wat do u mean by
No pointers,functions,structures,unions......
then how will one store a string (as arrays are also pointers)
n which functions r not allowed??????????
QwertyManiac
16-11-2007, 08:01 PM
He meant creating just strings using arrays and one's not allowed to create a sub-routine to process things.
fun2sh
16-11-2007, 08:04 PM
wat do u mean by
No pointers,functions,structures,unions......
then how will one store a string (as arrays are also pointers)
n which functions r not allowed??????????
adi007
17-11-2007, 04:36 PM
Only 1 variable but constants are allowed as desired?
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..
fun2sh
19-11-2007, 07:15 PM
yeah they r seperate group of data structures but they too are based on pointers, so tel me can we use them or not.??
adi007
22-11-2007, 11:19 AM
u can use arrays.....
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 <stdio.h>
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
fun2sh
22-11-2007, 04:47 PM
can we use this statement
char *string;
oh didnt read properly. cant use that statement :sad:
oh didnt read properly. cant use that statement :sad:
adi007
23-11-2007, 02:54 PM
answer will given on next tuesday
(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).....
fun2sh
23-11-2007, 03:17 PM
@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
@ontopic
can we use global variable and this operator ::
adi007
23-11-2007, 03:33 PM
Answer to puzzle 7
#include<stdio.h>
main()
{
char a[1000];
printf("Enter the string\n");
gets(a);
printf("Enter the character whose frequency is to be determined\n");
scanf("%c",&a[997]);
a[998]='0';a[999]='0';a[1000]='0';
for(;a[(a[998]-48)]!='\0';a[998]++)
{
if(a[997]==a[(a[998]-48)])
a[1000]++;
}
a[998]--;
for(;a[(a[999]-48)]!='\0';a[999]++)
{
if (a[(a[999]-48)]!=a[(a[998]-48)])
{
printf("\n%s is not a palindrome\n",a);
break;
}
a[998]--;
}
if(a[(a[999]-48)]=='\0')
printf("\n%s is a palindrome\n",a);
for(a[998]='0';a[(a[998]-48)];a[998]++);
printf("The string \"%s\" contains %d characters and '%c' occurs %d times\n",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....
fun2sh
23-11-2007, 03:41 PM
wel i was also doin this but usin a global variable, coz global variable is different from local 1. so i can use same name for both hence only one variable name is required.
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.
adi007
23-11-2007, 03:45 PM
I think 1000 is far more than sufficent for a string.....
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:
fun2sh
23-11-2007, 03:52 PM
that wat i was askin!!!
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
adi007
27-11-2007, 12:09 PM
good info fun2sh..Didn't knew this
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
fun2sh
27-11-2007, 09:41 PM
wat do mean by "until a smiley is encountered" ??
QwertyManiac
27-11-2007, 10:47 PM
Until :) is typed in.
its easy man, may be if i'll get time will post here
Common interview question.
Btw am not a C fanatic
adi007
28-11-2007, 11:19 AM
Until :) is typed in.
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
no...
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<stdio.h>
#include<stdlib.h>
int recReverse( char *tempStrPtr )
{
int finalInt;
if (tempStrPtr == NULL)
finalInt = 0;
else if (*tempStrPtr == '\0')
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 _
adi007
28-11-2007, 05:37 PM
1.This program cannot be solved in Linux.Because u will not get a smiley in linux.You will get the smiley in TC.
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
fun2sh
30-11-2007, 05:47 PM
hey can u give more examples of output involvin smiley. i m not gettin wat u want to do with smiley.
adi007
30-11-2007, 05:58 PM
smiley is just the way to indicate the end....
It's just like normal char...
smiley is just the way to indicate the end....
It's just like normal char...
how do u enter smiley after string in cmd prompt ?
Hitting CTRL+B gives ^B
adi007
30-11-2007, 06:18 PM
example program here
http://www.thinkdigit.com/forum/showpost.php?p=654017&postcount=21
fun2sh
30-11-2007, 06:25 PM
wat if the smiley appears in th mid like this
i luv digit forum :) but i hate spamer
do u expect output like
i vul tigid murof
example program here
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
adi007
30-11-2007, 06:39 PM
wat if the smiley appears in th mid like this
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...
There is no separate output and input.The program is dynamic
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
adi007
01-12-2007, 01:17 PM
i will post the gif file or link to runtime vedio on Tuseday(@offtopic :I am going to release my software presenmaker(beta) on that day.:D:D:D...)
Date for the puzzle 8 has been extended till next Thursday...
anuj919
01-12-2007, 01:47 PM
Hey I am also a student in 1st year in Gujarat & new in this puzzle
I have done puzzle 8
Here is the Solution...
#include<stdio.h>
#include<conio.h>
//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]!='\0';i++);
i--;
for(j=0;i>=0;i--)
{
rev[j++]=str[i];
}
rev[j]='\0';
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]='\0'; //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<i)
{
printf("%s ",str_rev(a[k]));
k++;
}
}
}
//After smily print string in new line
printf("\nHave a Nice Day");
getch();
}
:D:D:D
yeah thats correct :D It was puzzle no 7.
anuj919
02-12-2007, 11:48 AM
yeah thats correct :D It was puzzle no 7.
Sorry but it was puzzle no. 8:D
Sorry but it was puzzle no. 8:D
yeah my bad:D:D:D
adi007
03-12-2007, 10:41 AM
i think the answer given by anuj919 is wrong....
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......
i think the answer given by anuj919 is wrong....
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 ?
fun2sh
05-12-2007, 11:32 AM
yeah its dynamic..me to checked it n its reverses the strin as soon as u press a space bar
adi007
05-12-2007, 12:05 PM
program compiled and checked ..
it works..
1 point to anuj919
adi007
06-12-2007, 12:09 PM
Hi! I am Adithya U,17 year old Engineering Student(IT) from Hassan,Karnataka
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!!
fun2sh
06-12-2007, 01:32 PM
are bhai why u givin same program now!!!!
waise now one question can we use strtok() function n gotoxy() function???
adi007
06-12-2007, 04:36 PM
^^no
anuj919
09-12-2007, 10:14 PM
I think it is next to impossible to solve the puzzle 9 without using getch/getche (Which r in "conio.h ") because without it one can't read sentence 'word by word'
So please can we use getch/getche?
adi007
10-12-2007, 10:35 AM
I think it is next to impossible to solve the puzzle 9 without using getch/getche (Which r in "conio.h ") because without it one can't read sentence 'word by word'
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.....
QwertyManiac
10-12-2007, 11:05 AM
Under Turbo stuff, getch() comes in conio.h
adi007
10-12-2007, 04:02 PM
confirmed...
getche() and getch() comes under stdio.h(tested in TC++ 3.0)..
Yamaraj
10-12-2007, 04:24 PM
confirmed...
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.
fun2sh
10-12-2007, 04:25 PM
confirmed...
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!!
adi007
10-12-2007, 04:31 PM
hmmm..:confused:
i created the solution program by using conio.h for clrscr()
then i removed clrscr() and #include<conio.h>
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......
Yamaraj
10-12-2007, 05:05 PM
What is there to confirm? You should not have skipped studying the C Standard Library while you were busy mastering the syntax.
Non-standard and system/implementation dependent functions like getch(), getche() or clrscr() CANNOT reside in a standard header like stdio.h.
anuj919
10-12-2007, 07:56 PM
So what have come out all your discussion
Can we use getch/getche or not?
adi007
11-12-2007, 11:24 AM
I accept my fault....
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<stdio.h>
void main()
{
clrscr();
printf("Press any key\n");
getche();
getch();
}
and even this works fine
void main()
{
clrscr();
printf("Press any key\n");
getche();
getch();
}
I have made the vedio file and will upload and give the link within this week...
fun2sh
11-12-2007, 05:36 PM
^^^strange???
has some settings been change of ur TC++ compiler??
QwertyManiac
11-12-2007, 05:48 PM
Yeah how does stdio.h cover clrscr as your code example claims so? Use standard code mate, and allow a wider population to join.
anuj919
11-12-2007, 07:18 PM
I got the answer again:D :D :D :D
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<stdio.h>
#include<conio.h>
void main()
{
char b[999];
int i=0,j=0;
char c;
printf("Enter the String\n");
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();
}
adi007
12-12-2007, 10:06 AM
^^Very good :):)
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 now on...
Yamaraj
12-12-2007, 04:20 PM
[B]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
I wonder if it's of any use talking sense into people these days. Turbo C++ is NOT C or even C++. Either ask mods to change the title from "C puzzle" to "Turbo C++ puzzles", or spend some time reading the standard ANSI/ISO C and post accordingly.
People still using TC++ 3.0 should be hanged till death. Tell that to your instructor/professor.
Sykora
12-12-2007, 04:24 PM
^^^ Agreed.
adi007
14-12-2007, 10:20 AM
I wonder if it's of any use talking sense into people these days. Turbo C++ is NOT C or even C++. Either ask mods to change the title from "C puzzle" to "Turbo C++ puzzles", or spend some time reading the standard ANSI/ISO C and post accordingly.
People still using TC++ 3.0 should be hanged till death. Tell that to your instructor/professor.
sorry Yamaraj..
I will take care that this will not happen again...
Will follow strict ANSI/ISO C standards from now on...
By the way which c compiler u use..?
Sorry for not giving puzzle 10 yeterday.Will give puzzle 10 tommorow...
Yamaraj
14-12-2007, 10:40 AM
No need to be sorry. I only want you and others to get better at programming. I use many compilers actually - GCC (On Linux and Cygwin), MinGW (GCC for Win32), Visual C++ 2008 Express Edition, Sun C and C++ on Solaris etc.
If you want a decent ANSI/ISO conforming C compiler for Windows, try Pelles C - http://www.smorgasbordet.com/pellesc/ or MinGW.
Things like unbuffered I/O, non-standards functions like clrscr() and getch()/getche() are entirely system or implementation dependent, and likely to distract a potential learner from the path of becoming a real good programmer. In fact, it's the code readability, re-usability and portability that you should be more concerned about.
adi007
15-12-2007, 11:47 AM
Hi! I am Adithya U,17 year old Engineering Student(IT) from Hassan,Karnataka
http://farm3.static.flickr.com/2052/2086499468_1f153310e7_o.gif
Currently you are witnessing C puzzle 10
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 10th C puzzle
write a c program in which tom will chase jerry for 2 lines and then jerry bounce back and will chase tom to the begining..
confused..http://gigasmilies.googlepages.com/39.gifhttp://gigasmilies.googlepages.com/39.gif
here is the output that i expect..
http://farm3.static.flickr.com/2351/2085714733_b87446cf37_o.gif
Note:I captured the output using camtassia but it could not capture it correctly..Even then the real output will be similar to this..
Note :Specify your modified program along with the answer
Awards gallary:
Puzzles solved :9
me (If no one gets the solution then points will be for me) 2
saurabh kakkar 2
fun2sh 2
anuj919 2
eggman 1
nigthtcrawler 1
Leading:saurabh kakkar,fun2sh,anuj919 and myself
Puzzle 10 added thread updated...
If you want a decent ANSI/ISO conforming C compiler for Windows, try Pelles C - http://www.smorgasbordet.com/pellesc/ or MinGW.
^^Thanks Yamaraj for the link...have downloaded Pelles C.It's nice.http://gigasmilies.googlepages.com/4.gif
QwertyManiac
15-12-2007, 05:04 PM
Two lines as in two physical lines or prompt/terminal width dependent? Cause I see your 'Jerry' trickling down to the 2nd line character by character.
a_k_s_h_a_y
15-12-2007, 08:46 PM
Execute in Turbo C only
You must first save this file in TC directory ONLY save as .C file and then run it in TC
if you save as .CPP then it won't execute
so some graphics in C
/* Execute in Turbo C only
You must first save this file in TC directory as NAME.C only and then run it in TC
if you save as .CPP then it won't execute */
#include <graphics.h>
#include <dos.h>
#include <conio.h>
main()
{
int gd=DETECT,gm,button,x,y,x2,y2,i;
char a;
x=0;y=0;
initgraph(&gd,&gm,"");
setcolor(2);
setfillstyle(SOLID_FILL,BLACK);
outtextxy(50,400,"Keep holding Space For Tom to Chase, Any key to Exit ");
outtextxy(50,430,"hell with the rules this was made by me long back now just edited little ");
outtextxy(50,450,"i have to study for VTU bye and btw All the best Mr Very Rude Adi");
for(x=0;x<480;x++)
{
outtextxy(x,y,"Tom Jerry");
a=getch();
bar(x,y,x+79,y+10);
if(a!=' ') exit(0);
}
for(x=480;x>0;x--)
{
outtextxy(x,y+30,"Tom Jerry");
a=getch();
bar(x,y,x+79,y+50);
if(a!=' ') exit(0);
}
while(!kbhit());
}
Execute in Turbo C only
You must first save this file in TC directory ONLY save as .C file and then run it in TC
if you save as .CPP then it won't execute
so some graphics in C
/* Execute in Turbo C only
You must first save this file in TC directory as NAME.C only and then run it in TC
if you save as .CPP then it won't execute */
#include <graphics.h>
#include <dos.h>
#include <conio.h>
main()
{
int gd=DETECT,gm,button,x,y,x2,y2,i;
char a;
x=0;y=0;
initgraph(&gd,&gm,"");
setcolor(2);
setfillstyle(SOLID_FILL,BLACK);
outtextxy(50,400,"Keep holding Space For Tom to Chase, Any key to Exit ");
outtextxy(50,430,"hell with the rules this was made by me long back now just edited little ");
outtextxy(50,450,"i have to study for VTU bye and btw All the best Mr Very Rude Adi");
for(x=0;x<480;x++)
{
outtextxy(x,y,"Tom Jerry");
a=getch();
bar(x,y,x+79,y+10);
if(a!=' ') exit(0);
}
for(x=480;x>0;x--)
{
outtextxy(x,y+30,"Tom Jerry");
a=getch();
bar(x,y,x+79,y+50);
if(a!=' ') exit(0);
}
while(!kbhit());
}
lol..these quizzies are nuts:))http://farm3.static.flickr.com/2286/2107305542_22d75ef21a_o.png
The_Devil_Himself
15-12-2007, 11:46 PM
^^yea,these compiler specific codes are funny.
a_k_s_h_a_y
16-12-2007, 12:15 AM
lol..these quizzies are nuts:))http://farm3.static.flickr.com/2286/2107305542_22d75ef21a_o.png
yeah i did not give into it much ..
just wrote back the program with slight changes of the version that i already had
its basically Dos Program with DOS.H and i tried compiling it in borland latest but i get errors .. hell lots .. don't know why.
Graphics in C is outdated i think.. but that's what i used for My Text Editor and Graphics Editor Projects in 3rd SEM .. i mean i am using .. still in 3rd Sem lol
i really don't know why newly available compiler fails in this .. !! after all its just C.. about KBHIT function its ok i know its defined in DOS.h
and i could have used sleep() to get out put exactly as need but let it be space bar . i think its cool
yeah i did not give into it much ..
just wrote back the program with slight changes of the version that i already had
its basically Dos Program with DOS.H and i tried compiling it in borland latest but i get errors .. hell lots .. don't know why.
Graphics in C is outdated i think.. but that's what i used for My Text Editor and Graphics Editor Projects in 3rd SEM .. i mean i am using .. still in 3rd Sem lol
i really don't know why newly avaible compiler fails in this .. !! after all its just C.. about KBHIT function its ok i know its defined in DOS
and i could have used sleep() to get out put exactly as need but let it be space bar . i think its cool
turbo c graphics libraries are not ANSI C compliant.
I hated so much using it till 3rd year.
a_k_s_h_a_y
16-12-2007, 12:37 AM
^^ then how do i go about with graphics in C ?
my big text editor and graphics editor projects that i wrote in TC are all waste or what ??
^^ then how do i go about with graphics in C ?
my big text editor and graphics editor projects that i wrote in TC are all waste or what ??
nothing is waste, all serves for some purpose.
U will need it during ur engg (curse the old syllabus)
Just dont make it mainstream, there are other tools to create graphics.
a_k_s_h_a_y
16-12-2007, 12:51 AM
there are other tools to create graphics.
for example ??
no not directx and windows API .. i am trying to stay away from it for some time now
other then that maybe
for example ??
no not directx and windows API .. i am trying to stay away from it for some time now
other then that maybe
am not aware abt other tools in C, and its hihgly unlikely that u will be allowed to use anyhing other than gcc and turbo c in college. So better stick to old ways for the time being.
i hav learned making UI using JAVA and XUL.
Yamaraj
16-12-2007, 01:53 AM
C doesn't care about graphics, sound or input devices. You must use external libraries and/or system API for that kind of a program.
C doesn't care about graphics, sound or input devices. You must use external libraries and/or system API for that kind of a program.
yeah those 3rd party librarieshttp://farm3.static.flickr.com/2192/2107308402_d580fcfc62_o.png
anantkhaitan
16-12-2007, 06:26 PM
Gr8 thread...
My solution to Problem no. 10
#include<conio.h>
#include<stdio.h>
#include<dos.h>
void main()
{
int x=1,y=1,d=1;
char a[]=" Tom Jerry ";
while(!kbhit())
{
gotoxy(x,y);
printf(a);
delay(50);
x+=d;
if(x==81&&y==1&&d==1){x=1;y=2;}
if(x==0&&y==2&&d==-1){x=80;y=1;}
if(x==69&&y==2&&d==1){d=-1;a[4]=32;a[12]=2;}
if(x==1&&y==1&&d==-1){d=1;a[4]=2;a[12]=32;}
}
}
Note: String a[ ] contains that similey Alt+2 after Tom
And guys I don't have a TC++ compiler so this program was tested by dry run GCC doesn't support conio
Pathik
16-12-2007, 06:59 PM
am not aware abt other tools in C, and its hihgly unlikely that u will be allowed to use anyhing other than gcc and turbo c in college. So better stick to old ways for the time being.
i hav learned making UI using JAVA and XUL.
But isnt XUL used mainly only in firefox extensions??
btw r u also in engg??
But isnt XUL used mainly only in firefox extensions??
btw r u also in engg?? nope XUL is used in songbird player.(its platform independent)
And its all based on XML (the mega player of web based applications), XML can be used to make almost everything.
Everything is getting web based.
yeah am in engg.
adi007
17-12-2007, 08:34 AM
http://gigasmilies.googlepages.com/68.gifsorry,i just forgot to specify another imporatant rule
Rules
No user defined functions..
only stdio.h,conio.h and string.h are allowed
no gotoxy()
now try..http://gigasmilies.googlepages.com/yes.gif
and let me give u a hint that it has nothing to do with graphics...http://gigasmilies.googlepages.com/16.gif
anantkhaitan
17-12-2007, 10:31 AM
^^ are yaar.. I was using dos.h only for delay().. ok if you don't want it then..
#include<conio.h>
#include<stdio.h>
void main()
{
int x=1,y=1,d=1;
char a[]=" Tom Jerry ";
while(!kbhit())
{
gotoxy(x,y);
printf(a);
for(long i=0;i<99999;i++); // Time delay loop
x+=d;
if(x==81&&y==1&&d==1){x=1;y=2;}
if(x==0&&y==2&&d==-1){x=80;y=1;}
if(x==69&&y==2&&d==1){d=-1;a[4]=32;a[12]=2;}
if(x==1&&y==1&&d==-1){d=1;a[4]=2;a[12]=32;}
}
}
Not using graphics is not a hint but its obvious.. :p
adi007
17-12-2007, 12:57 PM
first thing there should be smiley..
second jerry should chase tom with a smiley..
third no gotoxy(sorry i didn't specified it in the beginning)...
i will add more rules..
wait till tommorow..
I think i made a mess of this puzzle...http://gigasmilies.googlepages.com/39.gif
anantkhaitan
17-12-2007, 02:07 PM
^^
Brother do you want to me to write the program exactly as you have written.. A program can be written in hundred of ways, and now you start adding rules..Its not that I cannot write the program without using gotoxy(), but again I will write a program and you will say: "You cannot use this or that"..First of all be clear with your problem. And as far as the competition/contest/(whateva it is) is concern I am seeing that I am the only participant..
first thing there should be smiley..
second jerry should chase tom with a smiley..
Have you compiled the program..?? and for the smiley check what I have written earlier #131 (http://www.thinkdigit.com/forum/showpost.php?p=692938&postcount=131): String a[ ] contains that similey (Alt+2) after Tom
Now you can take this much of pain atleast because i don't have Turbo C++ with me.. rather i don't have DOS with me..
adi007
17-12-2007, 02:15 PM
i will be clear with my rules tommorow and that will be final..http://gigasmilies.googlepages.com/yes.gif
sorry for the trouble..http://gigasmilies.googlepages.com/63.gif
i am accesing net from my college and i have no internet connection in my home..
so it is not possible to check ur program..
instead of smiley use some char such as '#'
anantkhaitan
17-12-2007, 02:44 PM
Ok brother bring your rules tomorrow.. here I bring my program (without gotoxy());
#include<conio.h>
#include<stdio.h>
void main()
{
int s=0,d=1,j;
char a[]="Tom# Jerry ";
while(!kbhit())
{
clrscr();
for(j=0;j<s;j++)printf(" ");
printf(a);
for(long i=0;i<99999;i++);
s+=d;
if(s==149&&d==1){d=-1;a[3]=32;a[11]=35;}
if(s==0&&d==-1){d=1;a[3]=35;a[11]=32;}
}
}
adi007
18-12-2007, 11:07 AM
final set of rules
allowed header files -->stdio.h,conio.h&string.h
no gotoxy(),clreol(),delline(),kbhit()
maximum of 4 variables
no user defined functions
the printf statements shouldn't be empty i.e printf(" ") not allowed
if ur compiler doesn't support smiley then some char such as '#'
restrictions and terms ruined my life http://farm3.static.flickr.com/2192/2106525721_b621e4efee_o.png
QwertyManiac
18-12-2007, 11:45 AM
Yeah, why is he asking people to make it exactly as his? Those set of rules are very very specific.
anantkhaitan
18-12-2007, 12:54 PM
Perhaps he is making the rules after seeing my code :p
@ adi
Listen brother i have just one thing to say "I GIVE UP". Do you know what is the use of kbhit ??? If we use something like while(!kbhit()) the loop keeps on iterating until you press a key i.e. ending up your program whenever you like, Now tell me does it anywhere hampers/interferes with the logic of the code. I have used 4 variables with a string.. I can write the same program using 2-3 variables without using any string but that will elongate the code...
Every programmer has a unique way of writing .. and what I submitted was my way of writing and you cannot it change by imposing those stupid rules (whateva I submitted U made a new rule against that).. And I am feeling proud that I am disqualified.. Waiting for your code..ATB
And yes forgot to mention about printf(" ") replace it by printf("%c",32) :p
a_k_s_h_a_y
18-12-2007, 01:28 PM
yeah that's what i meant
a program can be written in many ways !!
he wants us to think exactly as he does !
that can be done using basics of printf thing ... going back and coming forward .. but now who has time to solve it .. exams are on !!
yeah that's what i meant
a program can be written in many ways !!
he wants us to think exactly as he does !
that can be done using basics of printf thing ... going back and coming forward .. but now who has time to solve it .. exams are on !! better study for exams dude, ur placement depends on marks.
Alas finally am above all these plcaement tension:D
yay my 600th posthttp://farm3.static.flickr.com/2355/2107305358_394278500c_o.png
The_Devil_Himself
18-12-2007, 02:57 PM
who said placement depends upon marks?toppers sux and are good for nothing!The guy who got the highest placement in my university in 2005-2006 had 9 backs at the time of interviews!!
who said placement depends upon marks?toppers sux and are good for nothing!The guy who got the highest placement in my university in 2005-2006 had 9 backs at the time of interviews!!
exceptions do occur :D
Probably some other qualities helped him, but its a risky matter to depend on this if we can surely secure placement by getting good marks.
After all good marks are not just for placements but also ur parents will be happy that their efforts and resources are not getting wasted.
Btw who said toppers sux ?, i said get good marks just to be on safer side.
fun2sh
18-12-2007, 03:12 PM
who said placement depends upon marks?toppers sux and are good for nothing!The guy who got the highest placement in my university in 2005-2006 had 9 backs at the time of interviews!!
yeah! u r right! few girls who r topper in my class knows nothin abt programin!! jst mug up the things! but at least 1 need the grace marks for sittin in placement exams!!!
n guys plz dont talk offtopic here(sorry coz i too did just now)!!! its a very nice thread!
harryneopotter
18-12-2007, 03:19 PM
devil is right in a sense .......toppers sux ..but only who r crammers (unfortunately most toppers are crammers..) and regarding placement, if u fulfill the minimum criteria for a company ... thn it dsnt matter whether u have 71% marks or 91% marks ... it depends solely on ur talent .............
a_k_s_h_a_y
18-12-2007, 03:22 PM
guys guys enough .. adi will start to wonder from where did #include <placements.h> come in his programs ;)
ok guys u win, i had the same notion what you hav now but everything changes. I hav seen all this, this very year (people havin high marks were not even asked any questions at interviews but average students were passed thru trials).
Do you think mass recruiters look for talents, agar aisa hota toh india mein aaj hazaroo einstein hote.
its a fight club discussion so am backing off now.
Back to topic comrades :D:D:D
adi007
19-12-2007, 11:40 AM
yeah that's what i meant
a program can be written in many ways !!
he wants us to think exactly as he does !
no a_k_s_h_a_y my intension is not that..
i modified the rules because i later found that the puzzle would become too easy without them..
Perhaps he is making the rules after seeing my code :p
this is some what true :D:D
but let me say that my intension was not to make u program the way i programmed..
After seeing ur code i found out that the puzzle will be too easy to solve without some rules.So i had to modify the rules
@ adi
Listen brother i have just one thing to say "I GIVE UP".
don't give up anantkhaitan. the answer to this puzzle is way too easy..
easier than ur old program..
If we use something like while(!kbhit()) the loop keeps on iterating until you press a key i.e. ending up your program whenever you like,
i already told that tom should chase jerry for 2 lines...
so there is no necessity for kbhit()
who said placement depends upon marks?toppers sux and are good for nothing!The guy who got the highest placement in my university in 2005-2006 had 9 backs at the time of interviews!!
^^I agree with u ..
please start a new thread named 'does placement depends upon marks' in the fight club..
lets see there what others think...
Pathik
19-12-2007, 02:33 PM
who said placement depends upon marks?toppers sux and are good for nothing!The guy who got the highest placement in my university in 2005-2006 had 9 backs at the time of interviews!!
+100
anuj919
20-12-2007, 05:51 PM
Hi Adi :o :o
I think this answer fulfills all your conditions.:p
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j=1;
long k;
char a[]="Tom# Jerry ";
clrscr();
while(j<=2)
{
for(i=0;i<=149;i++)
{
printf("%*s",i+strlen(a),a);
for(k=0;k<9999999;k++);
clrscr();
}
a[3]=' ';a[10]='#';
for(i=149;i>=0;i--)
{
printf("%*s",i+strlen(a),a);
for(k=0;k<9999999;k++);
clrscr();
}
a[3]='#';a[10]=' ';
j++;
}
}
:D :D
adi007
22-12-2007, 04:17 PM
^^yup u are right..
one point for anuj919 and one for anantkhaitan and a_k_s_h_a_y...
Hi! I am Adithya U,17 year old Engineering Student(IT) from Hassan,Karnataka
http://farm3.static.flickr.com/2052/2086499468_1f153310e7_o.gif
Currently you are witnessing C puzzle 11
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 11th C puzzle
write a c program which gives the output as below..
Enter a number
52
The reverse of 52 is 25
52 is not a prime number
Sum of digits=7
Enter a number
17
The reverse of 17 is 71
17 is a prime number
Sum of digits=8
If i didn't specify the rules then anyone can solve this :D:D
Rules
maximum of 2 header files
maximum 2 variables(including global and local variables)
Both the variables must be of same data type
no structures,pointers,user defined functions..
Note :Specify your modified program along with the answer
Awards gallary:
Total puzzles solved :10
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
Leading:anuj919
11th puzzle added!!
Thread updated!!:D
adi007
24-12-2007, 12:33 PM
hmm...
no responses..:sad:
may be everyone are busy with there exams :D
adi007
01-01-2008, 12:14 PM
Puzzle date has been extended till 11th Jan(coz i think many of u have exams like me:()
anuj919
01-01-2008, 12:18 PM
We can use arrays,can't we........
anantkhaitan
01-01-2008, 06:53 PM
Check out :
#include<stdio.h>
int main()
{
int a,b;
printf("Enter a Number\n");
scanf("%d",&a);
b=10*(a%10)+(a/10);
printf("The reverse of %d is %d\n",a,b);
for(b=2;b<=a;b++)
if(a==b)
{printf("%d is a prime number\n",a);break;}
else if(a%b==0)
{printf("%d is not a prime number\n",a);break;}
printf("Sum of digits=%d\n",a/10+a%10);
return 0;
}
This time do not add rules after submitting
khattam_
03-01-2008, 03:25 AM
Here's the solution which works for all integers:#include<stdio.h>
#include<conio.h>
void main(void){
int n,i;
clrscr();
printf("Enter a number\n");
scanf("%d",&n);
printf("The reverse of %d is ",n);
i=n;
while(i>0){
printf("%d",i%10);
i/=10;
}
printf("\n%d is ",n);
for(i=2;i<n;i++)
if (n%2==0){
printf("not ");
break;
}
printf ("a prime number",n);
i=0;
while(n>0){
i+=n%10;
n/=10;
}
printf("\nSum of digits=%d",i);
getch();
}
maddy354
05-01-2008, 10:13 AM
#include<stdio.h>
main()
{
int i,a[10];
printf("\nenter a number\n");
scanf("%d",&a[0]);
a[1]=a[0];a[2]=0;
for(;a[1]>0;i++)
{a[2]=a[2]*10+a[1]%10;
a[1]/=10;
}
a[3]=0;
for(i=2;i<a[0]/2;i++)
{if(a[0]%i==0)
a[3]=1;
}
printf("\nreverse of %d is %d\n",a[0],a[2]);
if(a[3]==1)
printf("\n%d is not prime\n");
else
printf("\n%d is prime\n",a[0]);
return 0;
}
adi007
12-01-2008, 11:03 AM
I'm back:)
Check out :
#include<stdio.h>
int main()
{
int a,b;
printf("Enter a Number\n");
scanf("%d",&a);
b=10*(a%10)+(a/10);
printf("The reverse of %d is %d\n",a,b);
for(b=2;b<=a;b++)
if(a==b)
{printf("%d is a prime number\n",a);break;}
else if(a%b==0)
{printf("%d is not a prime number\n",a);break;}
printf("Sum of digits=%d\n",a/10+a%10);
return 0;
}
This time do not add rules after submitting
^^hmm..
Not have compiled ur program but i think it will work only for 2 digit integers..
If it is then ur program is wrong..
The output's that i have given are just examples..It doesn't mean that the program should work only for 2 digit integers..
It must work for all integers (Upto the integers supported by int datatype)..
Here's the solution which works for all integers:
#include<stdio.h>
#include<conio.h>
void main(void)
{
int n,i;
clrscr();
printf("Enter a number\n");
scanf("%d",&n);
printf("The reverse of %d is ",n);
i=n;
while(i>0)
{
printf("%d",i%10);
i/=10;
}
printf("\n%d is ",n);
for(i=2;i<n;i++)
if (n%2==0)
{
printf("not ");
break;
}
printf ("a prime number",n);
i=0;
while(n>0)
{
i+=n%10;
n/=10;
}
printf("\nSum of digits=%d",i);
getch();
}
^^Exellent logic..U got the right answer but i think their is one small mistake
for(i=2;i<n;i++)
if (n%2==0)
{
printf("not ");
break;
}
^^It's not n%2 it's n%i ..:)
#include<stdio.h>
main()
{
int i,a[10];
printf("\nenter a number\n");
scanf("%d",&a[0]);
a[1]=a[0];a[2]=0;
for(;a[1]>0;i++)
{a[2]=a[2]*10+a[1]%10;
a[1]/=10;
}
a[3]=0;
for(i=2;i<a[0]/2;i++)
{if(a[0]%i==0)
a[3]=1;
}
printf("\nreverse of %d is %d\n",a[0],a[2]);
if(a[3]==1)
printf("\n%d is not prime\n");
else
printf("\n%d is prime\n",a[0]);
return 0;
}
^^It's right..:)
Any more different methods to solve this puzzle ...
Puzzle 11 will be posted on monday 14th Jan.
adi007
14-01-2008, 11:47 AM
Hi! I am Adithya U,17 year old Engineering Student(IT) from Hassan,Karnataka
http://farm3.static.flickr.com/2052/2086499468_1f153310e7_o.gif
Currently you are witnessing C puzzle 12
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
puzzle 12 added thread updated
adi007
17-01-2008, 04:46 PM
hmmm..
not even one try..
looks like the puzzle is too difficult or i am not clear in puzzle ..:confused:
rachitpant
17-01-2008, 07:27 PM
Hi! I am Adithya U,17 year old Engineering Student(IT) from Hassan,Karnataka
http://farm3.static.flickr.com/2052/2086499468_1f153310e7_o.gif
i entered this thread thinking some1 must be challenging to write a GUI or console based solution for sodoku .
and see wht is see
Currently you are witnessing C puzzle 12
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
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
i dont see any thing puzzling here
some1 who has spend 3 days studying c++ will be able to answer this
fun2sh
18-01-2008, 09:29 AM
u forgot to mention the rules!:mrgreen:
adi007
18-01-2008, 03:14 PM
^^i had given the rules already it's in the first post of the thread..
I just quoted that post..but i don't know why the rules disappeared..
Any ways i have edited the post here is the rules
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
i dont see any thing puzzling here
some1 who has spend 3 days studying c++ will be able to answer this
take a look at the puzzle now...
http://www.thinkdigit.com/forum/showpost.php?p=719822&postcount=163
QwertyManiac
18-01-2008, 04:17 PM
I can't use conio.h on Linux since its a DOS header. Thus you may have to excuse 3 functions of NCURSES am gonna use to emulate the getche() with echo off as required for your output. [NCURSES has only getch() and certain key-break/check functions to use along with it.]
1. initscr() - Required to start a working window for manipulating I/O. Mandatory for ncurses programs, segfaults else.
2. noecho() - Turns off echo. As your program demands.
3. endwin() - To close the screen started. Complements the 1st allowance. You can treat it as one.
(One thing I don't understand is, getche is supposed to getch() and echo right? Why doesn't your output have any characters then? Anyway, I used noecho() to do so.)
Ps. You have exit() under stdio.h probably, while I need to use stdlib.h. I have no use of stdio.h in my program and hence I guess you can allow me this one more too.
Program:
#include<ncurses.h>
#include<stdlib.h>