Solve the given C Puzzle

Status
Not open for further replies.

maddy354

Right off the assembly line
#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;
}
 
OP
A

adi007

Youngling
I'm back:)
Check out :
Code:
#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:
Code:
#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
Code:
for(i=2;i<n;i++)
if (n%2==0)
{ 
printf("not ");
break;
}
^^It's not n%2 it's n%i ..:)

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

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






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

puzzle 12 added thread updated
 
Last edited:

rachitpant

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

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

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


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

adi007

Youngling
^^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...
*www.thinkdigit.com/forum/showpost.php?p=719822&postcount=163
 
Last edited:

QwertyManiac

Commander in Chief
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:
Code:
#include<ncurses.h>
#include<stdlib.h>

int main()
{    
    
    [B]initscr();
    noecho();[/B]
    printw("Hello!\nContinue(y/n)\n");
    switch (getch())
    {
        case 121:
            main();
            break;
        case 89:
            main();
            break;
    }
    printw("Bye,press any key to exit");
    getch();
    [B]endwin();[/B]
    exit(0);
}

I don't expect this to pass or something, just wished to show that jumping over from TurboCrap stuff to standard code (And even Linux, for that matter) is easier than what most think.

Compile as:
Code:
gcc name.c -o outname [B]-lncurses[/B]

Outputs as:
Code:
Hello!
Continue(y/n)
Hello!
Continue(y/n)
Bye,press any key to exit
 

baccilus

Cyborg Agent
Find the fault:

Code:
#include <stdio.h>
main ()
{
    int a, b, sum;
    printf ("Enter a number ");
    scanf("%d",a);
    printf("\nEnter another number ");
    scanf("%d",b);
    sum=a+b;
    printf("\nSum is %d",sum);    
    return 0;
}

It is supposed to add two numbers by the way.
 

QwertyManiac

Commander in Chief
Your scanf arguments must be addresses and not values. Add an ampersand to each of the variables you use in a scanf statement. :)

Code:
#include <stdio.h>
main ()
{
    int a, b, sum;
    printf ("Enter a number ");
    scanf("%d",[B]&a[/B]);
    printf("\nEnter another number ");
    scanf("%d",[B]&b[/B]);
    sum=a+b;
    printf("\nSum is %d",sum);    
    return 0;
}
 
OP
A

adi007

Youngling
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:
Code:
#include<ncurses.h>
#include<stdlib.h>

int main()
{    
    
    [B]initscr();
    noecho();[/B]
    printw("Hello!\nContinue(y/n)\n");
    switch (getch())
    {
        case 121:
            main();
            break;
        case 89:
            main();
            break;
    }
    printw("Bye,press any key to exit");
    getch();
    [B]endwin();[/B]
    exit(0);
}

I don't expect this to pass or something, just wished to show that jumping over from TurboCrap stuff to standard code (And even Linux, for that matter) is easier than what most think.

Compile as:
Code:
gcc name.c -o outname [B]-lncurses[/B]

Outputs as:
Code:
Hello!
Continue(y/n)
Hello!
Continue(y/n)
Bye,press any key to exit
^^u solved the puzzle but let me say that u have used several functions but since u are in linux i have excused many of them..Try solving it in windows using conio.h.....
BTW is there any windows emulator c version in linux ..:confused:
There are many more ways to solve it..
The puzzle is open till tommorow..
(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.)
That's the intresting part of the puzzle..
make getche() work as getch()....:D
 

QwertyManiac

Commander in Chief
^^ I am just curious whether there is any such stuff or not...
I tried using wine but in vain...
Do you understand what a compiler is supposed to do? And why there are different compilers out there? :)

Btw: You can run it under dosbox, since its a DOS compiler set.
 
K

khattam_

Guest
Did this under dosbox.
Did not find any function to exit except exit() from stdlib.h. However, other rules have been followed. Keen to see anyone do this by follwoing the rules completely. No loops, no functions and no getch()?? this one is difficult, at least for me...

Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void main (void)
{
printf("\nHello!\nContinue(y/n)");
if(getche()=='y') main();
printf("\nBye, Press any key to exit.");
getche();
exit(0);
}

Maybe we cud clear the thing and reprint the thing. But no looping.??
 
OP
A

adi007

Youngling
Did this under dosbox.
Did not find any function to exit except exit() from stdlib.h. However, other rules have been followed. Keen to see anyone do this by follwoing the rules completely. No loops, no functions and no getch()?? this one is difficult, at least for me...

Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

void main (void)
{
printf("\nHello!\nContinue(y/n)");
if(getche()=='y') main();
printf("\nBye, Press any key to exit.");
getche();
exit(0);
}

Maybe we cud clear the thing and reprint the thing. But no looping.??

no if
.. and i think ur output will not match with mine coz u see even if press 'y' it will not get displayed on the screen..overall make getche() work like getch()...

The next puzzle will be asked soon..currently busy with my new software..sorry for the delay..till then this puzzle is open for everyone...
 
K

khattam_

Guest
ok... i remembered my 1st sem classes and did it. the echoing thing.. Thanks for the hint adi007
\b does the backspace and a space is printed over y.....
Code:
#include <stdio.h>
#include <conio.h>

void main (void){
printf("\nHello!\nContinue(y/n)");
switch (getche()){
case 'y':
printf("\b ");
main();
break;
default:
printf("\b \nBye, Press any key to exit.");
getche();
}}


but still for the exit(), I needed stdlib.h..
which i fixed by placing the default: thing..... but I was not sure how this thing worked....

But figured it out later.....:D
 
Last edited by a moderator:
Status
Not open for further replies.
Top Bottom