Specify a class demo to initialize two characters ch1 and ch2 with the value G and E Interchange the value of ch1 and ch 2 print the value of ch 1 and ch 2 before and after interchange
please solve its urgent in written please
Answers
Answer:
Sample Solution:
Java Code:
public class Exercise15 {
public static void main(String[] args) {
int a, b, temp;
a = 15;
b = 27;
System.out.println("Before swapping : a, b = "+a+", "+ + b);
temp = a;
a = b;
b = temp;
System.out.println("After swapping : a, b = "+a+", "+ + b);
}
}
Copy
Without using third variable.
Sample Solution:-
Java Code:
public class Exercise15 {
public static void main(String[] args) {
// int, double, float
int a, b;
a = 15;
b = 27;
System.out.println("Before swapping : a, b = "+a+", "+ + b);
a = a + b;
b = a - b;
a = a - b;
System.out.println("After swapping : a, b = "+a+", "+ + b);
}
}
Copy
Sample Output:
Before swapping : a, b = 15, 27
After swapping : a, b = 27, 15
Flowchart:
Flowchart: Java exercises: Swap two variables
Sample Solution (Input from the user):
Java Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int x, y, z;
Scanner in = new Scanner(System.in);
System.out.println("Input the first number: ");
x = in.nextInt();
System.out.println("Input the second number: ");
y = in.nextInt();
z = x;
x = y;
y = z;
System.out.println(" Swapped values are3:" + x + " and " + y);
}
}
Copy
Sample Output:
Input the first number:
36
Input the second number:
44
Swapped values are:44 and 36
Flowchart:
Flowchart: Java exercises: Swap two variables
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Java program to print an American flag on the screen.
Next: Write a Java program to print a face.
What is the difficulty level of this exercise?
EASY MEDIUM HARD
Based on 424 votes, average difficulty level of this exercise is Easy .
New Content published on w3resource :
Python Numpy exercises
Python GeoPy Package exercises
Python Pandas exercises
Python nltk exercises
Python BeautifulSoup exercises
Form Template
Composer - PHP Package Manager
PHPUnit - PHP Testing
Laravel - PHP Framework
Angular - JavaScript Framework
React - JavaScript Library
Vue - JavaScript Framework
Jest - JavaScript Testing Framework
by TaboolaSponsored LinksYou May Like
Incredible Rs.2499 Smartwatch is Taking India By Storm
BlazeWatch
The cost of hearing aids in Hyderabad might surprise you!
hear.com
Your Child Can Be The Next Sundar Pichai
CampK12
Term plan that returns your premium at the end of policy*^
Max Life Insurance Quotes
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
©w3resource.com 2011-2020
PrivacyAboutContactFeedbackAdvertise