Tech2 Forums - Powered by vBulletin

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

Page 1 of 3 123 LastLast
Results 1 to 10 of 22
  1. #1
    Uber Newbie
    Join Date
    Mar 2010
    Location
    Faridabad,India
    Posts
    6
    Liked
    0 times
    Reputation points
    0

    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 ?

  2. #2
    Uber Newbie
    Join Date
    Aug 2008
    Location
    Hyderabad
    Posts
    0
    Liked
    0 times
    Reputation points
    0

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

  3. #3
    Tech2 Members
    Join Date
    Jun 2006
    Location
    Pune, Nagpur
    Posts
    411
    Liked
    1 times
    Reputation points
    0

    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:



  4. #4
    Uber Newbie
    Join Date
    Mar 2010
    Location
    Faridabad,India
    Posts
    6
    Liked
    0 times
    Reputation points
    0

    Re: C Program Help

    Thanks but i don't know what's getch().

  5. #5
    Tech2 Members
    Join Date
    Jun 2006
    Location
    Pune, Nagpur
    Posts
    411
    Liked
    1 times
    Reputation points
    0

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



  6. #6
    Uber Newbie
    Join Date
    Mar 2010
    Location
    Faridabad,India
    Posts
    6
    Liked
    0 times
    Reputation points
    0

    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:
    A=C;

    C=D;
    D=A;

  7. #7
    Tech2 Members
    Join Date
    Jun 2006
    Location
    Pune, Nagpur
    Posts
    411
    Liked
    1 times
    Reputation points
    0

    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?



  8. #8
    Uber Newbie
    Join Date
    Mar 2010
    Location
    Faridabad,India
    Posts
    6
    Liked
    0 times
    Reputation points
    0

    Re: C Program Help

    Thanks for the help !!! :P

  9. #9
    Uber Newbie
    Join Date
    Aug 2008
    Location
    Hyderabad
    Posts
    0
    Liked
    0 times
    Reputation points
    0

    Re: C Program Help

    Quote 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

  10. #10
    Tech2 Members
    Join Date
    Jun 2006
    Location
    Pune, Nagpur
    Posts
    411
    Liked
    1 times
    Reputation points
    0

    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



Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •