DIGIT-ARC read-only archive · snapshot: July 23, 2026

C/C++ simple challenge

51 posts · originally from https://geek.digit.in/community/threads/c-c-simple-challenge.2438/

shoegoe
shoegoe Original post #1
Oct 30, 2004 at 9:22 PM

we all know how to swap numbers in a program..

BUT we use a variable temp/like that to store the initial value..

can the same be done without using a TEMP variable?
C
cg84 #2
Oct 30, 2004 at 9:50 PM
very simple...
take 2 nos. x & y and do the following:
x=x+y
y=x-y
x=x-y
and ur nos r swapped... :)
sujithtom
Oct 30, 2004 at 10:13 PM
That was a cool idea. How about manipulating fuctions???
C
cg84 #4
Oct 30, 2004 at 11:27 PM
sujithtom said:
That was a cool idea. How about manipulating fuctions???

what??? didnt get ur question...
sms_solver
Oct 31, 2004 at 1:59 AM
cg84 from where did you find this solution?

take 2 nos. x & y and do the following:
x=x+y
y=x-y
x=x-y
alib_i
alib_i #6
Oct 31, 2004 at 3:22 AM
its very old question !!

there are many more answers possible
x= x*y
y= x/y
x= x/y
NikhilVerma
Oct 31, 2004 at 8:47 AM
Ok now here is another simple one....

Do the same swapping with strings... without using temp variable...
cooljeba
Oct 31, 2004 at 9:07 AM
cg84 said:
very simple...
take 2 nos. x & y and do the following:
x=x+y
y=x-y
x=x-y
and ur nos r swapped... :)
this is the best way to do it with out using a temp variable ;)


..:: peace ::..
jeba
alib_i
alib_i #10
Oct 31, 2004 at 9:48 PM
Write the smallest C program which prints itself.
swatkat
swatkat #11
Nov 1, 2004 at 1:51 PM
main()
{
getche();
}
How about this?
L
Nov 1, 2004 at 6:05 PM
That was a cool idea. How about manipulating fuctions???

you mean manipulating program execution flow?as in the main calls function A but you wanna call B, without B(); anywhere in the program?
alib_i
alib_i #13
Nov 1, 2004 at 9:53 PM
swatkat said:
main()
{
getche();
}
How about this?

dont know the answer ..
but i dont think that's correct
shoegoe
shoegoe #14
Nov 1, 2004 at 10:10 PM

O...
C
cnukutti #15
Nov 2, 2004 at 5:41 PM
What is the thing about manipulating functions??
and what do you mean by printing itself.
should it read the .c file and print the source code???
swatkat
swatkat #16
Nov 2, 2004 at 7:18 PM
"Write the smallest C program which prints itself."
What do you mean by this?
D
DKant #17
Nov 2, 2004 at 11:43 PM
NikhilVerma said:
Ok now here is another simple one....

Do the same swapping with strings... without using temp variable...

Simple? Well I dunno. Sumthin' with pointers? But I don't see how u can do any manips on pointers. :?

Apart from that, swapping without temps isn't fool-proof. For example, if ur dealing with 2 integers of the order of 32767/65535 (basically sumwhere close to the max.) , then u cld run into problems.
alib_i
alib_i #18
Nov 3, 2004 at 7:25 PM
@swatcat ...
frankly speaking .. even i dont know ... this exactly was a problem statement in one programming contest long time back ..
i still dont know wht it means and how to solve it
vysakh
vysakh #19
Nov 3, 2004 at 8:05 PM
i give up
whats the answer???
shoegoe
shoegoe #20
Nov 3, 2004 at 8:55 PM
cg84 said:
very simple...
take 2 nos. x & y and do the following:
x=x+y
y=x-y
x=x-y
and ur nos r swapped... :)

Ths...
BUT

How to swap more than 2 variables .....
(DONT TELL ME TO USE THE SAME CODE FOR EACH 2 SWAPS

ANSWERS??
G
game_dev #21
Nov 3, 2004 at 9:47 PM
The smallest code that prints itself : (In the question, the format of the output was not specified. So, the program prints itself in binary (not 0s & 1s) but ASCII codes of the binary). The code is C++ code as it uses iostream; but can be converted to C easily. But this is just a guess & not a definite solution.

#include<iostream.h>
void main()
{
void (*f_ptr)()=main;
char *ptr=(char *)f_ptr;
cout << ptr;
}

Any other answers/corrections in above code ? Please post.
[/code]
R
rahuld #22
Nov 4, 2004 at 3:51 PM
ok try this

main()
{
printf("think digit");
main();
}
alib_i
alib_i #23
Nov 4, 2004 at 4:58 PM
i think u got it right game_dev
will have to check it !!!
L
Nov 4, 2004 at 8:31 PM
main()
{
printf("think digit");
main();
}

pal ....cant use recursion in main!

anyways guys find out the swaping string thingy ! it sounds amazing. my comp teacher who keeps screaming that he knows C for 10 yrs , not that im insulting him. he knows a lot and keeps giving us such programs but when i asked him this he couldnt do it.
I HAVE GOTTA KNOW THE SOLUTION TO THIS MAN! i wont move without it!


this exactly was a problem statement in one programming contest long time back ..
do you have any more of such stuff dude?
and anyone knows dennis ritchie's email add? :wink:
G
game_dev #25
Nov 4, 2004 at 10:51 PM
Swapping of strings without temp variables is quite simple. It uses the same principle of swapping 2 integers. The program :

Code:
#include <iostream.h>
void main()
{
	char a[50],b[50];
	int j=0;
	for(int i=0;i<50;i++)
	a[i]=b[i]=0;
	cout << "Enter string 1 : ";
	cin >> a;
	cout << "Enter string 2 : ";
	cin >> b;
	for(i=0;;i++)
	{
		if(a[i]=='\0') break;
		if(b[i]=='\0') {j=1;
			break;}
		a[i]=a[i]+b[i];
		b[i]=a[i]-b[i];
		a[i]=a[i]-b[i];
	}
	if(j==0)
	{
		j=i;
		do
		{
			a[i]=b[i];
			i++;
		}while(b[i]!='\0');
		b[j]='\0';
		a[i]='\0';
	}
	else
	{
		j=i;
		do
		{
			b[i]=a[i];
			i++;
		}while(a[i]!='\0');
		a[j]='\0';
		a[i]='\0';
	}
	cout << "String 1 : "<<a<<endl;
	cout << "String 2 : "<<b<<endl;
}
No third char variable is used. No user enterable character has an ASCII value more than 127. So this code will work for any user entered string.
Hope this helps. Bye !
L
Nov 4, 2004 at 11:07 PM
dude .........you re the man! :mrgreen:
G
game_dev #27
Nov 4, 2004 at 11:31 PM
Thanks for the compliments. As for Dennis Ritchie's e-mail address, goto : *www.cs.bell-labs.com/who/dmr/

Bye !
C
cg84 #28
Nov 5, 2004 at 11:12 AM
DKant said:
Apart from that, swapping without temps isn't fool-proof. For example, if ur dealing with 2 integers of the order of 32767/65535 (basically sumwhere close to the max.) , then u cld run into problems.

No dkant, using add/subtract to swap without temps is pretty good. even if ur values are close to the limits(or even on the limits), the swap will take place without any problems. the reason for that is binary arithmetic. just add ur nos in binary, reject the carry out, and then subtract(using 2's complement) rejecting the borrow out. u will get the same nos u started with, only their places would be swapped. go ahead, try it, it works!!!
shoegoe
shoegoe #29
Nov 5, 2004 at 5:23 PM
alib_i said:
Write the smallest C program which prints itself.

ok....But i dont know wheather it is correct answer to ur question..BUT

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

void main()
{
	FILE *fd;
	char c;
	clrscr();
	fd = fopen("printme.c", "r"); /* Enter the address of the file */
	while (!feof(fd))
	{
		fscanf(fd, "%c", &c);
		putchar(c);
	}
	fclose(fd);
	getch();

}

OK...its in "C" AS U TOLD~

WELL>>THIS PROGRAM STILL PRINTS ITSELF .....

Tell me weather its correct or ><~

:)
D
DKant #30
Nov 6, 2004 at 6:31 PM
@cg84 thx 4 that m8...I was beginning 2 lose my fundae. But actually C doesn't reject the carry out - that happens only when u use unsigned int. So the way it ultimately works is sumwhat skrud up..but it works! :p
alib_i
alib_i #31
Nov 7, 2004 at 12:36 AM
yaar .. shoegoe ..
frankly speaking i dont know the answer ..
but when i tried running the code on my TC .. the program went haywire

i wud ask other C studs here to verify the answer
shoegoe
shoegoe #32
Nov 7, 2004 at 10:58 AM
shoegoe said:
alib_i said:
Write the smallest C program which prints itself.

ok....But i dont know wheather it is correct answer to ur question..BUT

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

void main()
{
	FILE *fd;
	char c;
	clrscr();
	fd = fopen("c:\printme.c", "r"); /* Enter the address of the file */
	while (!feof(fd))
	{
		fscanf(fd, "%c", &c);
		putchar(c);
	}
	fclose(fd);
	getch();

}

OK...its in "C" AS U TOLD~

WELL>>THIS PROGRAM STILL PRINTS ITSELF .....

Tell me weather its correct or ><~

:)


amm......
you should save the above code in a file named printme.c( in this case c:\printme.c)

then compile and run as an ordinary program...

hope it works~!


shoegoe
shoegoe #33
Nov 7, 2004 at 11:00 AM

anyone tried it?
:?: :?: :?: :?:
alib_i
alib_i #34
Nov 7, 2004 at 11:24 PM
yes man .. it worked ...
u did it completely right ...
i had accidently stored it as printme.cpp ..
that's y it was giving error ..
its working prefect now
shoegoe
shoegoe #35
Nov 8, 2004 at 8:40 PM
alib_i said:
yes man .. it worked ...
u did it completely right ...
i had accidently stored it as printme.cpp ..
that's y it was giving error ..
its working prefect now

U asked a program in C and not cpp~!

Good to help.. :D

any more challenges?:?: :?:
alib_i
alib_i #36
Nov 8, 2004 at 8:51 PM
yeah .. my mistake ..
u did it right
C
cg84 #37
Nov 9, 2004 at 10:01 AM
ok. i've another idea...
lets use shoegoe's printme.c program, but just make a couple of changes to it. firstly, rename the main() function to something else, like print(). and don't save this file as printme.c, give it another name like printit.c.
now, make a file called printme.c(in the same dir as printme.c) and write this code in it:

#include "printit.c"
void main()
{
print();
}

now, now, i know that it really isnt the answerr. its just using a loophole in the Q. technically, printme.c is a complete C program in itself, isnt it? so maybe, this is ur answer... :(
firewall
firewall #38
Nov 9, 2004 at 12:20 PM
smallest c program to print itself :::

go to *forums.devshed.com/archive/t-159303

:)
NikhilVerma
Nov 9, 2004 at 9:00 PM
dunno if it's right.....
P
prankzter #40
Nov 11, 2004 at 8:01 PM
MAN...this guy is piling up his 11th class homework on us!!!

u R in cbse board...right??
alib_i
alib_i #41
Nov 12, 2004 at 3:13 PM
f00k u prankzter ..
its not any 11th class homework

this question was asked in an all-india c/c++ programming contest 3yrs back....
the only problem is that i dont remember the question properly.
and dont know the right answer too.

and to tell u ..
im not in 11th ..
im in 4th yr of my college ..
see how angry i am
<----------------------
:eek: :eek:
D
DKant #42
Nov 13, 2004 at 4:10 AM
Anybody done the magic square thingy? (It was something like sum of all rows/cols/diags of an nxn matrix is the same...I don't remember exactly. U have to build that matrix of course)

I haven't thought a lot about it..How do we implement it? Iterative methods? Remember this was just one question in a 3 hour/so C++ challenge.
NikhilVerma
Nov 13, 2004 at 10:21 AM
Actually tere is a prescribed method for making that square...

One of my freinds made that program ... Maybe I could get it from him... or make my own...

BTW check these out

*math.nmsu.edu/breakingaway/Lessons/magicsquare1/magicsquare.html
*www.math.hmc.edu/funfacts/ffiles/10001.4-8.shtml
vysakh
vysakh #44
Nov 13, 2004 at 10:28 AM
try this
//program to print the numbers in a triangle
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int ar[10][10],n,i,j;
cout<<"Enter number of rows:";
cin>>n;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
ar[j]=0;
cout<<"The pascal triangle for "<<n<<" rows is :\n";
for(i=0;i<n;i++)
{
cout<<"\n";
ar[0]=1;
for(j=0;j<=i;j++)
{
cout<<ar[j]<<"\t";
ar[i+1][j+1]=ar[j]+ar[j+1];
}
}
getch();
}
J
jcmac #45
Nov 13, 2004 at 12:47 PM
#include<iostream.h>
void main()
{
int a,b,c;
cout<<"enter 2 numbers";
cin>>a,b;
c=a+b;
cout<<"first number :"<<c-a;
cout<<"second number :"<<c-b;
}
J
jcmac #46
Nov 13, 2004 at 12:49 PM
#include<iostream.h>
void main()
{
int a,b,c;
cout<<"enter 2 numbers";
cin>>a,b;
c=a+b;
cout<<"first number :"<<c-a;
cout<<"second number :"<<c-b;
}
vysakh
vysakh #47
Nov 13, 2004 at 1:02 PM
you cant cin like that
it_waaznt_me
Nov 13, 2004 at 3:48 PM
And temp variable is not to be used ..
V
Nov 14, 2004 at 12:24 AM
x=x+y
y=x-y
x=x-y
NikhilVerma
Nov 14, 2004 at 9:48 AM
Tat problem has already been solved
Z
Jun 5, 2009 at 6:51 PM
One line code for swapping two variables, a and b, without using third variable

a^=b^=a^=b

the above line of code will swap values of a and b