Write a Python program to read three numbers and if any two variables are equal print that number

Try the new Google Books

Check out the new look and enjoy easier access to your favorite features

Hello people, here we discuss a simple python program which finds the biggest and smallest number out of given three numbers. Here we use concept of functions in this program.

Biggest and Smallest number — programminginpython.com

We use two functions biggest() and smallest() to find the biggest number and smallest number respectively and finally return the result.

Task :

To find smallest and biggest number out of given 3 numbers.

Approach :

  • Read 3 input numbers using input() or raw_input().
  • Use two functions largest() and smallest() with 3 parameters as 3 numbers
  • largest(num1, num2, num3)
  • check if num1 is larger than num1 and num2, if true num1 is largest, else
  • check if num2 is larger than num1 and num3, if true num2 is largest,
  • if both the above fails, num3 is largest
  • Print the largest number
  • smallest(num1, num2, num3)
  • check if num1 is smaller than num1 and num2, if true num1 is smallest, else
  • check if num2 is smaller than num1 and num3, if true num2 is smallest,
  • if both the above fails, num3 is smaller
  • Print the smallest number

Program :

Output :

Biggest and Smallest number — programminginpython.com
Biggest and Smallest number — programminginpython.com

Computer Science

a = int(input("Enter first number: ")) b = int(input("Enter second number: ")) c = int(input("Enter third number: ")) print("The three number are", a, b, c) a, b = a + b, b + c print("Numbers after swapping are", a, b, c)

Enter first number: 10 Enter second number: 15 Enter third number: 20 The three number are 10 15 20 Numbers after swapping are 25 35 20

In the program below, the three numbers are stored in num1, num2 and num3 respectively. We've used the if...elif...else ladder to find the largest among the three and display it.

Source Code

# Python program to find the largest number among the three input numbers # change the values of num1, num2 and num3 # for a different result num1 = 10 num2 = 14 num3 = 12 # uncomment following lines to take three numbers from user #num1 = float(input("Enter first number: ")) #num2 = float(input("Enter second number: ")) #num3 = float(input("Enter third number: ")) if (num1 >= num2) and (num1 >= num3): largest = num1 elif (num2 >= num1) and (num2 >= num3): largest = num2 else: largest = num3 print("The largest number is", largest)

Output

The largest number is 14.0

Note: To test the program, change the values of num1, num2 and num3.

Last update on May 28 2022 13:14:03 (UTC/GMT +8 hours)

Write a Python program to sum of three given integers. However, if two values are equal sum will be zero.

Pictorial Presentation:


Sample Solution:-

Python Code:

def sum_three(x, y, z): if x == y or y == z or x==z: sum = 0 else: sum = x + y + z return sum print(sum_three(2, 1, 2)) print(sum_three(3, 2, 2)) print(sum_three(2, 2, 2)) print(sum_three(1, 2, 3))

Sample Output:

0 0 0 6

Flowchart:


Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to get the least common multiple (LCM) of two positive integers.
Next: Write a Python program to sum of two given integers. However, if the sum is between 15 to 20 it will return 20.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Counts the occurrences of a value in a list:

Example:

def tips_count_occurrences(lst, val): return len([x for x in lst if x == val and type(x) == type(val)]) print(tips_count_occurrences([2, 2, 5, 2, 2, 5, 7], 2))

Output:

4

I'm trying to do the following:

Write a program that reads three numbers and prints “all the same” if they are all the same, “all different” if they are all different, and “neither” otherwise.

Your program should request 3 integers via 3 input statements. Use a combination of if, elif, and else to implement the algorithm that is needed for this problem.

However, whenever I input all the same integers I get both 'all the same' and 'neither'. How do I make it so that my "neither" section is correct?

x=input('enter an integer:') y=input('enter an integer:') z=input('enter an integer:') if x==y and y==z: print('all the same') if not x==y and not y==z: print('all different') if x==y or y==z or z==x: print('neither')

7

Postingan terbaru

LIHAT SEMUA