Find largest number from user input
You need to use try
and except
here.
while(True):
try:
value = float(raw_input(">")) # Get the input from user
if value > largest_so_far: # Type cast to integer
largest_so_far = value
except ValueError as e: # Handle ValueError
print largest_so_far
break # Break the infinite loop
Find the largest number input by the user and identify how many times the largest number was inputted
Some issues:
guess p/s is printf/scanf ?
for loop condition, "numberOfIntegers == listOfIntegers" Infact this
should be i < numberOfIntegers.Even above is corrected program will not work . For e.g take the input
1,3 ,5, 7, your answer will compute the correct max, but number of instances of max will be incorrect. (you need to reset occurence, when new max is found)
if (listOfIntegers > largest)
{
largest = listOfIntegers; occurence=1 ;
}
else if (listOfIntegers == largest)
{
occurrence++;
}
finding highest number in Javascript from user input
you are making things complex try this code .
Assignment 1: <input type="text" name="num1"><br>
Assignment 2: <input type="text" name="num2"><br>
Assignment 2: <input type="text" name="num3"><br>
<button onclick="high()">Submit</button>
<p id="result"></p>
<script>
function high(){
var arr = document.querySelectorAll("input");
var emptyarray = [];
arr.forEach((elem) => {
emptyarray.push(elem.value);
})
var max = Math.max.apply(null, emptyarray);
document.getElementById("result").innerHTML=max;
}
</script>
you will get the heighest number result , you can restrict the input field to just enter the number so that no one can add string instead of number and your code work fine .
How to find the greatest number among the numbers given input?
With the program below, you can get as many numbers as you want from the user and find the largest of them.
#include <iostream>
int main()
{
int size=0, largestValue=0, value=0;
std::cout << "Enter total numbers you want to add :" << "\n";
std::cin >> size;
for (int i{ 0 }; i < size; ++i)
{
std::cout << "Enter value to add : ";
std::cin >> value;
if (i == 0 || value > largestValue)
{
largestValue = value;
}
}
std::cout << "Largest value = " << largestValue << "\n";
return 0;
}
I'm trying to print the largest number from the inputs that the user gives, but it's printing the wrong number
So, first things first,
- the use of
max
can be avoided, as it is a reserved keyword in python
And coming to your fix, you are comparing it with the value only once in the loop, and you are returning the number, the indentation is the key here. You will have to wait for the loop to complete its job then return the value.
- There are many inbuilt methods to do the job, Here is your implementation (a bit modified)
session_live = True
numbers = []
a = 0
def largest_num(arr, n):
#Create a variable to hold the max number
max_number = arr[0]
#Using for loop for 1st time to check for largest number
for i in range(1, n):
if arr[i] > max_number:
max_number = arr[i]
# --- The indentation matters
#Returning max's value using return
return max_number
while session_live:
print("Tell us a number")
num = int(input())
numbers.insert(a, num)
a += 1
print("Continue? (Y/N)")
confirm = input()
if confirm == "Y":
pass
elif confirm == "N":
session_live = False
#Now I'm running the function
arr = numbers
n = len(arr)
ans = largest_num(arr, n)
print("Largest number is", ans)
else:
print(":/")
session_live = False
Finding the largest number and smallest number given the user input (METHODS)
You just need to add a couple of if
statements into for
loop of main
method to find out min and max values, e.g.:
for (int i = 0; i < noofgrades; i++) {
double Grade = 0;
// monthly interest rate//
double mograde = readPositiveDouble(input, "Please enter grade " + (i + 1) + ": ", "Errorrrrrrrr");
if (i == 0) {
lowestGrade = mograde;
highestGrade = mograde;
} else {
if (mograde < lowestGrade) {
lowestGrade = mograde;
}
if (mograde > highestGrade) {
highestGrade = mograde;
}
}
sumOfGrade += mograde;
}
How to print largest number from User Input
This method is quick and clean, basically read in values the number of times specified, and each time the number is greater than the current maximum, replace max with the value read.
int main()
{
int num_entries;
float num;
float max = 0;
cin >> num_entries;
while (num_entries-- > 0){
cin >> num;
if (num > max) {
max = num;
}
}
}
Related Topics
Python Executable Not Finding Libpython Shared Library
Plot Two Histograms on Single Chart With Matplotlib
Python: How to Calculate the Sum of Numbers from a File
Python-3: Why This Following Code Returns None in Print Statement
Replace a Word in a String by Indexing Without "String Replace Function" -Python
Number of Common Letters in Two Strings
Reading an Excel Named Range into a Pandas Dataframe
Change CSV Name to CSV Date Time Python
Tensorflow: Convert Tensor to Numpy Array Without .Eval() or Sess.Run()
Python - How to Extract Elements from an Array Based on an Array of Indices
Python: Element Is Not Attached to the Page Document
Reading Contents of a Gzip File from a Aws S3 in Python
Python3 Tkinter Set Image Size
Pip3: Command Not Found But Python3-Pip Is Already Installed
How to Disable Pylint Unused Import Error Messages in VS Code
Drop Rows Containing Empty Cells from a Pandas Dataframe
How to Assign Class Instance to a Variable and Use That in Other Class