InterviewSolution
| 1. |
Write a java program to swap two numbers with output using third variable by using comment lines plz don't post anything if u don't know... if you post it ur answer will be reported.. |
|
Answer» ogram using Third Variable:ackage com.company; import java.util.SCANNER; public class main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter 1st number: "); INT N = sc.nextInt(); System.out.println("Enter 2nd number: "); int n2 = sc.nextInt(); int n3; n3 = n; n = n2; n2 = n3; System.out.println("After swapping 1st number is: "+n); System.out.println("After swapping 2nd number is: "+n2); } }Your program without using Third Variable:package com.company; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner (System.in); System.out.println("Enter Number 1:"); int a = sc.nextInt(); System.out.println("Enter Number 2:"); int b = sc.nextInt(); if (a!=b){ a = a+b; b = a-b; a = a-b; } System.out.println("Numbers after swapping"); System.out.println("1st Number = "+a+" 2nd Number = "+b); } } I was not able to understand what you means by using COMMENT lines so I gave two programs. |
|