HackerRank Solution: Python Arithmetic Operators


Hacker Rank Python

Author: Bashir Alam
Reviewer: Deepak Prasad

Question: Python Arithmetic Operators [Introduction]

Task

The provided code stub reads two integers from STDIN,a and b. Add code to print three lines where:

  • The first line contains the sum of the two numbers.
  • The second line contains the difference of the two numbers (first - second).
  • The third line contains the product of the two numbers.

Example:

a = 3

b = 5

print the following:

8
-2
15

Input format:

The first line contains the first integer, a.
The second line contains the second integer, b.

Constraint:

1<=a <=10^10

1<=b<=10^10

Output format:

Print the three lines as explained above

Sample Input:

3
2

Explanation:

3+2 --> 5

3-2 --> 1

3*2 --> 6

 

Possible solutions

Now we will go through the following solutions: The following code is given in the hacker rank:

if __name__ == '__main__':
    a = int(input())
    b = int(input())

We will use two different possible methods to solve the problem.

  1. Using single print method
  2. Using multiple print methods

Now we will go through each of the methods.

 

Solution-1: Using singple print method

Now we will solve the problem and then will explain the code:

if __name__ == '__main__':
    a = int(input())
    b = int(input())
    
    print(str(a+b)+"\n"+str(a-b)+"\n"+str(a*b))

This code defines a simple program that prompts the user to enter two integers, a and b, and then prints their sum, difference, and product. The program begins by reading in the two integers using the input() function, which reads user input from the command line as a string. The program then converts these strings to integers using the int() function and stores them in the variables a and b. The program then calculates the sum, difference, and product of a and b and prints the results using the print() function. The str() function is used to convert the results to strings so that they can be printed. The \n characters in the print() statement are used to insert newline characters, which cause the output to be printed on separate lines.

 

Solution-2: Using multiple print methods

Now we will use multiple print methods to print the value:

if __name__ == '__main__':
    a = int(input())
    b = int(input())
    
    print(a+b)
    print(a-b)
    print(a*b)

This code defines a simple program that prompts the user to enter two integers, a and b, and then prints their sum, difference, and product. The program begins by reading in the two integers using the input() function, which reads user input from the command line as a string. The program then converts these strings to integers using the int() function and stores them in the variables a and b. The program then calculates the sum, difference, and product of a and b and prints the results using the print() function. The print() function prints each result on a separate line because each call to print() is on a separate line in the code.

 

Summary

In this short article, we discussed how to solve Arithemetic operator problem on Hacker rank. We solved the error using two methods and explained both methods.

 

Further reading

Question to Hacker Rank: Python Arithmetic Operators [Introduction]

 

Views: 457
Bashir Alam

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in Python, Java, Machine Learning, OCR, text extraction, data preprocessing, and predictive models. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment