Monday, October 27, 2014

How to swap any two numbers using C and java



USING C

#include<stdio.h>
#include<conio.h>
int main()
{
   int x,y,z;
   x=5;
   y=6;
   z=x;
   x=y;
   y=z;
   printf(" the new first no is: %d ", x);
   printf(" the new second no is: %d ", y);
// %d is use of integer type variables//
   return 0;
}


USING JAVA


public static void main(String[] args)
{
int firstvalue = 5;
int secondvalue = 6;
int x = firstvalue;
  firstvalue = secondvalue;
  secondvalue = x;
System.out.println(" the new first no is: " + firstvalue);
  System.out.println(" the new second no is: " + secondvalue);
}

..........................................................................................................

in both  java and c the output will be

the new first no is: 6

the new second no is: 5

...........................................................................................................

IF WE WANT THE USER TO INPUT THE NUMBERS:

USING C

#include<stdio.h>
#include<conio.h>
int main()
{
   int x,y,z;
   printf("Enter the first number:");
   scanf("%d", &x);
   printf("Enter the second number:");
   scanf("%d", &y);
   z=x;
   x=y;
   y=z;
   printf(" the new first no is: %d ", x);
   printf(" the new second no is: %d ", y);
// %d is use of integer type variables//
   return 0;
}

USING JAVA

import java.util.Scanner;
{
static Scanner sc= new Scanner(System.in);
        public static void main(String[] args)
       {
int firstvalue;
int secondvalue;
System.out.print("Enter the first number:");
firstvalue= sc.nextInt();
System.out.print("Enter the Second number:");
secondvalue=sc.nextInt();
int z = firstvalue ;
  firstvalue = secondvalue;
  secondvalue = z;
System.out.println("the new first no is:" + firstvalue);
System.out.println("the new second no is:" + secondvalue);


}


}

0 comments:

Post a Comment