Results 1 to 6 of 6

Thread: C sharp program for swapping number

  1. #1
    Join Date
    Dec 2009
    Posts
    32

    C sharp program for swapping number

    Hello Friends,

    I am studying the C sharp language. I have to write the C sharp code to swap the given two numbers. I am getting lots of error while running my program code.
    Do you have C sharp programming code for swapping number? If yes then please let know that program code.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: C sharp program for swapping number

    Hi,

    Please review the following C sharp code. Use this in your C sharp code, When you will run this program you will get swapping of two given number.

    public static void Swap(int l, int m)

    {
    int tmp
    tmp= l;

    l= m;

    l = tmp;
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: C sharp program for swapping number

    Hi friend,

    I hope you have aware about the other basic programming languages such as C, C++, and Java. If you program for swapping two number in these languages then I think it will be more easy to code swapping program in C sharp . You just need to make the minor changes in the programming syntax. There will no change in the programming logic of this program. Try this and let me know the output.

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: C sharp program for swapping number

    C sharp program to swap two numbers:
    class CsharpSwapping
    {
    public static void DemoSwap(int* a, int* b)

    {
    int tp = *a;

    *a = *b;

    *b = tp;
    }

    static void Main(string ags[])

    {

    int i = 25, j = 35; // Values for swapping.

    DemoSwap(&a, &b);

    Console.WriteLine("Number value after swapping: a = {0}, b = {1}", a, b);

    }
    }

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    Re: C sharp program for swapping number

    Hello,

    Please try something below, I hope this code will definitely give you some ides to code the C sharp program to swap numbers:
    int o,p;
    printf("Enter numbers ");

    scanf("%d%d",&o,&p);

    o=o+p;

    p=o-p;

    o=o-p;
    printf("o is now%d",o);

    printf("o is now %d",p);

  6. #6
    Join Date
    May 2008
    Posts
    2,389

    Re: C sharp program for swapping number

    num1= num2;
    num2=num3;
    num3=num1;
    Use above logic to print the swapping of two given numbers. Here the "num2" and "num3" are the two number which you want to swap. I have used the temporary variable "num1" to store the values of these numbers. Firstly I have assigned the "num2" value to the "num1" and finally assigned the "num1" value to the "num3".

Similar Threads

  1. Shell program to reverse a number
    By Bottlenecked in forum Software Development
    Replies: 5
    Last Post: 12-01-2010, 02:20 PM
  2. C sharp program to print random numbers
    By Prashobh Mallu in forum Software Development
    Replies: 5
    Last Post: 12-01-2010, 12:07 PM
  3. Shell program for the prime number
    By Rup_me in forum Software Development
    Replies: 5
    Last Post: 14-12-2009, 10:24 AM
  4. What is the Program to find-out factorial of given number?
    By Sheenas in forum Software Development
    Replies: 5
    Last Post: 27-11-2009, 01:42 PM
  5. Program to calulate the square of number
    By windows_user in forum Software Development
    Replies: 3
    Last Post: 10-12-2008, 04:41 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,110,917.28421 seconds with 16 queries