C Program Help
This is a topic on C Program Help within the Programmers Corner forums, part of the Technology category; Hi guys. I am learning C by YK. In the exercises i have a doubt. Well this is ch-1 only ...
-
Uber Newbie
C Program Help
Hi guys. I am learning C by YK. In the exercises i have a doubt. Well this is ch-1 only so it is a n00b question !!! I have to write a program.
Program Description:
Two numbers are input through the keyboard into two locations C and D.Write a program to interchange the contents of C and D.
Can anyone help me ?
-
Uber Newbie
Re: C Program Help
^Google for "Swap two numbers C Program"
My own code (might use older version of the language)
Code:
void main()
{
int a,b;
printf("Enter two numbers one by one");
scanf("%d,%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("The numbers are c= %d, d= %d",a,b);
getch();
}
-
Tech2 Members
Re: C Program Help
A typical misleading program:
Code:
void main()
{
int c,d;
printf("Enter two numbers one by one");
scanf("%d,%d",&c,&d);
printf("The numbers are c= %d, d= %d",d,c);
getch();
}
:mrgreen:
-
Uber Newbie
Re: C Program Help
Thanks but i don't know what's getch().
-
Tech2 Members
Re: C Program Help
Simple version:
Code:
void main()
{
int c,d,e;
printf("Enter two numbers one by one");
scanf("%d,%d",&c,&d);
e=c;
c=d;
d=e;
printf("The numbers are c= %d, d= %d",c,d);
getch();
}
-
Uber Newbie
Re: C Program Help
EDIT :
I found this
Code:
#include<stdio.h>
main ()
{
int A,C,D;
printf ("Enter the value of C and D: ");
scanf ("%d %d", &C, &D);
A=C;
C=D;
D=A;
printf ("\nThe exchanged values of C and D are: %d and %d ", C, D);
}
Can anyone explain this part:
-
Tech2 Members
Re: C Program Help
I used C language last in my second year i.e 6 years back, but iirc its used to display output after user presses Enter.
If you read my second program, I have done the same thing.
Its pretty obvious, first we are saving value of C in A, then saving value of D in C, now C has D's value. Then we are saving C's value in D from A. Its extremely simple, and I have no freaking idea what the poster above me posted as a code.
-- Sat Jul 17, 2010 3:23 pm --
@john, what is your code doing? By adding a and b and what not?
-
Uber Newbie
Re: C Program Help
Thanks for the help !!! :P
-
Uber Newbie
Re: C Program Help

Originally Posted by
ambar_hitman
extremely simple, and I have no freaking idea what the poster above me posted as a code.
-- Sat Jul 17, 2010 3:23 pm --
@john, what is your code doing? By adding a and b and what not?
Ever heard about "swapping two numbers without using a third variable" ?
It is essentially your second program without using the thrid variable . in your case e,
the confusion arised because i messed up the freaking program by trying to change a and b to c and d in the code but not chagning it completely. #FML
-
Tech2 Members
Re: C Program Help
^Oh yeah I remember that now, I also thought there was something missing or that was a part of some program. Now I remember the factorial program as well, in which it kept on getting simpler and simpler as new functions were introduced in C. Oh I miss those days, I always wanted to work on C/C++ or Java, but fate had something different planned for me. Oh well
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules