Question

Examine the C++, Python, and Java codes that do swap two values, which one you think...

Examine the C++, Python, and Java codes that do swap two values, which one you think is the best, which one you think is the worse – Note: identify the language evaluation criteria you used, but no need to justify.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ PROGRAM

//include required library file
#include <iostream>

//use nmaespace
using namespace std;

//main function defintion
int main()
{
//declare and initialize the required variables
int a = 10, b=20, temp;
  
//print the values before swapping
std::cout << "a = " <<a <<", b = "<<b << std::endl;
  
//code to swap
temp = a;
a=b;
b=temp;
  
//print the values after swapping
std::cout << "a = " <<a <<", b = "<<b << std::endl;

return 0;
}

JAVA PROGRAM

//Main class definition
public class Main
{
  
////main function defintion
   public static void main(String[] args) {
   //declare and initialize the required variables
   int a = 10, b=20, temp;
  
   //print the values before swapping
       System.out.println("a = " + a + ", b = " + b);
      
       //code to swap
       temp = a;
a=b;
b=temp;
      
       //print the values after swapping
       System.out.println("a = " + a + ", b = " + b);
   }
}

PYTHON PROGRAM

#declare and initialize two variables with values
a = 10
b = 20

#print the values
print('a = ',a , ", b = ",b)
# create a temporary variable and use it to swap the values
temp = a
a = b
b = temp

#print the values after swapping
print('a = ',a, ", b = ",b)

EXPLANATION

-> Python is the best

-> C++ is the worst

(Simplicity is the criteria used for comparison)

Add a comment
Know the answer?
Add Answer to:
Examine the C++, Python, and Java codes that do swap two values, which one you think...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT