Solve the given C Puzzle

Status
Not open for further replies.

adi007

Youngling
Hi! I am Adithya U,17 year old Engineering Student(IT) from Hassan,Karnataka

*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:facts that you never knew about C

2.C in linux/unix:Bored of using TC,switch to linux.All about running C programs in linux/unix

3.Presenmaker 1.0: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..

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

casanova

The Frozen Nova
C Puzzle 1
here is a simple c program
Code:
#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
Code:
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
Code:
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
 
Last edited:

eggman

I have Yolks not Brains!
Well you can use Edit Set [...] instead of %s in the scanf function.....it'll solve the problem....
 

saurabh kakkar

D i s t i n c t l y Ahead
following Program will work :)

Code:
#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);
}
 

Sykora

I see right through you.
Code:
#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 &&.
 
Last edited:

Yamaraj

The Lord of Death
Sykora said:
Code:
#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' [B][U]||[/U][/B] i < (MAXLEN-1)); /* Use && */
    str[i] = '\0'[B][U];[/U][/B]
    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

D i s t i n c t l y Ahead
I have given the soultion already in post no 5 here it goes again

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

adi007

Youngling
Sykora said:
Code:
#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)

eggman said:
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.
 
Last edited:

Projjwal

free world from money
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 .
 
OP
A

adi007

Youngling
adi007 said:
Here is the 2nd C Puzzle

Here is a simple C program
Code:
#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
Code:
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
Code:
Output:
Enter a character and 2 integer values
2 23 45.96
That's good
^^'2' is also a character

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

saurabh kakkar

D i s t i n c t l y Ahead
adi007 said:
Here is the 2nd C Puzzle

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

adi007

Youngling
wrong mate.

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

fun2sh

Pawned!... Beyond GODLIKE
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'
 
OP
A

adi007

Youngling
^^Sorry:confused: typing mistake it is '!' not '!=''.

fun2sh said:
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

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

fun2sh

Pawned!... Beyond GODLIKE
here is th solution thaen

Code:
#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();

}
 
Last edited:
Status
Not open for further replies.
Top Bottom