Solve the given C Puzzle

Status
Not open for further replies.

nightcrawler

Broken In
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

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)) / 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
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

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 :)
 
OP
A

adi007

Youngling
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
Code:
#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,
Code:
cc adi.c -lm
assuming that adi.c is the filename

find more about floor function at my Lesser Known Facts in C thread
*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

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=(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();
}



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


adi007 said:

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

saurabh kakkar

D i s t i n c t l y Ahead
Puzzle 5 Solved
Code:
#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
 
Last edited:
OP
A

adi007

Youngling
^^No saurab.Glance the rules carefully.
4.the symbol '&' shouldn't come in the program
where as you have used it twice
Code:
(fscanf(stdin, "%ld", [B]&[/B]a));
(fscanf(stdin, "%ld", [B]&[/B]b));
 
OP
A

adi007

Youngling
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.
*www.thinkdigit.com/forum/showthread.php?p=655539#post655539
 
OP
A

adi007

Youngling
^^will that means that you know the answer.If so,please do give the answer.


:!::!::!:
saurabh kakkar said:
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
*www.thinkdigit.com/forum/showthread.php?goto=lastpost&t=71047
 
Last edited:
OP
A

adi007

Youngling
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
 
OP
A

adi007

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

Code:
#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
*www.thinkdigit.com/forum/showthread.php?goto=lastpost&t=71047

adi007 said:
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
Code:
Enter the string
adithya
Start

a
d
i
t
h
y
a

End

Code:
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,
Code:
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!!
 
Last edited:

fun2sh

Pawned!... Beyond GODLIKE
thats real EASY

HERE ITS IS :mrgreen:

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

D i s t i n c t l y Ahead
Puzzle 6 solved
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char *a;
printf("enter a string %c",10); // [b]ascii value of '\n' is 10[/b]
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
 
Last edited:
OP
A

adi007

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

fun2sh

Pawned!... Beyond GODLIKE
yeah ur puzzle was toooooo simple this time. a child play i say. but U R DOIN A GREAT JOB BUDDY.
 

saurabh kakkar

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

adi007

Youngling
saurabh kakkar said:
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

saurabh kakkar said:
and from next time onwards plz mention the date nd Time at which u will post new puzzle :)
^^I will implement this
saurabh kakkar said:
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

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

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

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

fun2sh

Pawned!... Beyond GODLIKE
wat do u mean by
adi007 said:
No pointers,functions,structures,unions......
then how will one store a string (as arrays are also pointers)
n which functions r not allowed??????????
 
Status
Not open for further replies.
Top Bottom