Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

1 Visitor Messages

  1. Hi, i am new to java and need help with the below program.


    Write a program that reads integers, finds the smallest of them and counts its occurrences. Assume that the input ends with number 0. Suppose that you entered 6 2 5 2 2 3 0; the program finds that the smallest is 2 and the occurrence count for 2 is 3.
    Hint: maintain two variables min and count. Min stores the current min number and count stores its occurrences. Initially assign the first number to min and 1 to count. Compare each subsequent number with min. If the number is less than min, assign it to min and reset count to 1. If the number is equal to min increment count to 1.

    So far:

    import java.util.Scanner;

    public class MinOccur {

    /** Main method */
    public static void main(String[] args) {

    // Create a Scanner
    Scanner input = new Scanner(System.in);

    int times = 1;

    int count = 1;

    int min; // The smallest of the integers entered


    int minNumber = Integer.MAX_VALUE; //Setting up the Max Value as the Minimum Number (so that anything smaller is automatically placed as minNumber)


    // Read an initial data
    System.out.print(
    "Enter an int value (the program exits if the input is 0): ");
    int data = input.nextInt();
    int minimum = data;

    // Keep reading data until the input is 0
    int sum = 0;
    while (data != 0) {
    sum += data;

    // Read the next data
    System.out.print(
    "Enter an int value (the program exits if the input is 0): ");
    data = input.nextInt();
    }


    if (data < minNumber) //checking number against MAX_VALUE, this causes the number imputted to automatically become the new minNumber
    {
    minNumber = data;
    }


    System.out.printf( "Smallest Number is %d\n", minNumber );

    for (int i = 0; i < 100; i++)
    count++;
    count++;
    if (minNumber > 1){times++;}

    System.out.println("Number " + minNumber + " occurs " + times + " Times ");


    }
    }
Showing Visitor Messages 1 to 1 of 1
About oriordc2

Basic Information

About oriordc2
Java Skill Level:
Beginner

Statistics


Total Posts
Total Posts
2
Posts Per Day
0.00
Visitor Messages
Total Messages
1
Most Recent Message
February 26th, 2012 09:15 AM
Total Thanks
Total Thanks
0
  • Thanked 0 Times in 0 Posts
General Information
Last Activity
February 27th, 2012 02:30 PM
Join Date
February 26th, 2012