Thursday, 14 December 2017

Swap Two Number

Write a program to swap two numbers


Program:



                 


CODE IN C LANGUAGE:
#include<stdio.h>
#include<conio.h>
void main()
{
            int m=4, n=15;
clrscr();
            n=m-n+(m=n);
            printf("m=%d, n=%d",m,n);
getch();
}
Download jdk before installing netbeans...
For netbeans: http://www.filehorse.com/download-netbeans/



CODE IN JAVA(in netbeans):

package swap;
import java.util.*;
public class Swap
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in); System.out.println("Enter value for a: ");
int a=in.nextInt();
System.out.println("Enter value for b: ");
int b=in.nextInt(); System.out.println("Values before swap- a="+a+", b="+b);
b=a+b-(a=b);
System.out.println("Values after swap- a="+a+", b="+b);
}
}


CODE IN JAVASCRIPT:

<html>
<body>
<script>
Var x=10;
Var y=5;
y=x-y+(x=y);
document.write(“value of x is “+x+” and value of y is “+y);
</script>
</body>
</html>


For downloading .NetFramework:
http://download.microsoft.com/download/B/A/4/BA4A7E71-2906-4B2D-A0E1-80CF16844F5F/dotNetFx45_Full_setup.exe
     
      For downloading Visual Studio 2012
      http://www.microsoft.com/en-in/download/details.aspx?id=30682



CODE IN C#:

  using System;
  using System.Collections.Generic;//Not Required Now
  using System.Linq;//Not Required Now
  using System.Text;//Not Required Now
  using System.Threading.Tasks;//Not Required Now
  namespace Swap
  {
  class Program
  {
  static void Main(string[] args)
  {
  back:
  Console.WriteLine("Enter value for variable a: ");
  int a;
            try
            {
              a = Convert.ToInt32(Console.ReadLine());
      }
            catch
            {
                     Console.WriteLine("Wrong Input");
                     goto back;
            }
            back1:
            Console.WriteLine("Enter value for variable b: ");
            int b;
            try
            {
                            b = Convert.ToInt32(Console.ReadLine());
            }
catch
            {
                            Console.WriteLine("Wrong Input");
                            goto back1;
            }
            Console.WriteLine("Values before swapping- a=" + a + ", b=" + b);
            b=a+b-(a=b);
            Console.WriteLine("Values after swapping- a=" + a + ", b=" + b);
            Console.ReadKey();
      }
    }
}

No comments:

Post a Comment