How to Determine the Highest and Lowest Value Using Do While Loops

while loop for largest and smallest values Error

removing the else will solve the problem:

int main()
{
int num, minimum=10000000 , max=0;
int choice;
printf(" To Exit Enter (-1)\n");

while(num!=-1)
{

printf(" enter a number: ");
scanf("%d",&num);

if(num>max)
{
max=num;
}

if(num<minimum && num!=-1)
{
minimum=num;
}
}


printf("\n Largest Number: %d \n Smallest number: %d",max,minimum);
}

the problem was that the if condition was always true because you were entering ascending numbers so the else condition was never executed

Beginner - Do While loop and Smallest/Largest entry

Since you are incrementing the count only if the condition is satisfied,your count value wont be right if you dont get an entry smaller than the existing smallest or larger than the existing largest.

you have to change as folllows:

 else 
{

if (entry < smaller && entry!=-99 )
{
smaller = entry;

}
else if (entry > larger && entry !=-99)
{
larger = entry;

}
count+=1;

}

if (entry == -99) {
again = false;
count --;
}

Since you are checking the -99 condition only after the count has been incremented, if the user enters -99 , you have to decrease the counter by 1

Java Using do-while loop

Some homework improvements to meet the requirements:

import java.util.*;

public class Main {
public static void main(String[] args) {
String[] nums = new String[] {"1st", "2nd", "3rd", "4th", "5th"};

try(Scanner inp = new Scanner(System.in)) {

int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;

int i = 0;
do{

System.out.print("Enter " + nums[i] + " number: ");
int num = inp.nextInt();

if(num < min) min = num;
if(num > max) max = num;

} while(++i < nums.length);

System.out.println("\nThe highest value is: " + max);
System.out.println("The lowest value is: " + min);

} catch(InputMismatchException e) {
e.printStackTrace();
}
}
}

Find highest,lowest and average in a loop

Your highest and lowest variables both start at 0. So if your numbers are all negative, you'll get a highest of 0, incorrectly. If your numbers are all positive, you'll get a lowest of 0, incorrectly.

Just start off with highest = Integer.MIN_VALUE and lowest = Integer.MAX_VALUE and then the first iteration will end up with the correct highest and lowest value.

largest and smallest number using input while loop python

Instead, why don't you a list to store the values, then you can use the min and max methods:

nums = []

while True:
num = input("Enter a number: ")
if num == "done" : break
try:
fnum = float(num)

except:
print("Invalid input")
continue

nums.append(fnum)

largest = max(nums)
smallest = min(nums)

print("Maximum is", largest)
print("Minimum is", smallest)

How to find max&min in while loop - c

Printing an int using a conversion specifier for double is not a good idea, but invokes the infamous Undefined Behaviour.

The compiler might have noticed you about this. If it didn't, then increase its warning level. For GCC use -Wall -Wextra -pedantic.

So to fix this either make min and max be double

  int a = 0, b = 0;
double min = 1000., max = 0.; /* Such initialisations limit your input. */

or leave them be int (decreasing accuracy), and print them as what there are, namely int

  printf("Minimum: %d\nMaximum: %d\n", min, max);

The while and do-while loops The smallest value

You have to use long for the fact variable. When you use int the possible values are between -2,147,483,648 and 2,147,483,647.

However, you will never exceed the input number 6,188,989,133, so your while(fact <= number) loop will never exits. Every integer value possible will be smaller than 6,188,989,133. This explains why your code output is "empty", as it doesn't reach that System.out.println(count); line at all.

Keep in mind that the long type has such a limit as well, values can only be between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. When the input number is that big, you will not find the correct fact value which will be greater than the input because you are limited by the long type value range. As an example, for the input number 9,000,000,000,000,000,000 (which is inside the long type value range) the next factorial number is 51,090,942,171,709,440,000 (the value of 21!), which is outside the long type value range.

You can use the java.math.BigInteger class which has an "unlimited" range of possible values and do your math operations on BigInteger objects.



Related Topics



Leave a reply



Submit