Solve the given C Puzzle

Status
Not open for further replies.

[xubz]

"The Cake is a Lie!!"
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.

*www.devlist.com/Default.aspx
 
OP
A

adi007

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

puzzleslover

Right off the assembly line
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

i know that you are very skilled puzzles solvers, plz help.
 
OP
A

adi007

Youngling
Here is the solution for puzzle2

Code:
#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 *www.thinkdigit.com/forum/showthread.php?p=640073#post640073

the answer given by fun2sh is similar to it.So the point goes to fun2sh.

adi007 said:
Here is the 3rd C Puzzle

Here is a simple C program
Code:
#include<s[B][/B]tdio.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.

Code:
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
Code:
int sunday=1;
or 
#define sunday 1
are not allowed

2.a,b,c,d,e,f,g are not of int datatype
Code:
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
 
Last edited:

piyush gupta

Cyborg Agent
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...
 
OP
A

adi007

Youngling
^^ i want only program.
Please specify your modified program along with the answer.

no responses???:(:(
 
Last edited:

saurabh kakkar

D i s t i n c t l y Ahead
EDIT: Problem solved

Solution for 3rd C Puzzle
Code:
#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
Code:
#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();
}
 
Last edited:

piyush gupta

Cyborg Agent
adi007 said:
^^ 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 :)
 
OP
A

adi007

Youngling
I program suggested by saurabh will give the output like this
Code:
0
1
2
3
4
5
6
make some modifications..
 
Last edited:

saurabh kakkar

D i s t i n c t l y Ahead
^^ sorry dude I made mistake in hurry :( .here is the correct solution
Code:
#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
Code:
#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();
}
 

ALIM

Right off the assembly line
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.

adi007 said:
Here is the 3rd C Puzzle

Here is a simple C program
Code:
#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.

Code:
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
Code:
[B]int sunday=1;[/B]
[B]or [/B]
[B]#define sunday 1[/B]
are not allowed

2.a,b,c,d,e,f,g are not of int datatype
Code:
[B]int a;[/B]
^^ 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
 
OP
A

adi007

Youngling
Here is the solution for 3rd C Puzzle
Code:
#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.
 
Last edited:

saurabh kakkar

D i s t i n c t l y Ahead
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
 
OP
A

adi007

Youngling
adi007 said:

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
Code:
#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.

Code:
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
Code:
The value of pi taken=3.141 and the area of the circle is 2010.522690
but not this
Code:
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!!
 
Last edited:

nightcrawler

Broken In
adi007 said:
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

Code:
 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();
}
Code:
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
Code:
#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.
 
Last edited:
OP
A

adi007

Youngling
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
Code:
/**precision > 8[B]?[/B] *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.
 
Last edited:
OP
A

adi007

Youngling
^^ 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:

Code:
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

Code:
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
 
Last edited:
Status
Not open for further replies.
Top Bottom