Question

Write a single program in java using only do/while loops for counters(do not use array pls)...

Write a single program in java using only do/while loops for counters(do not use array pls) for the majority of the program and that asks a user to enter a integer and also asks if you have any more input (yes or no) after each input if yes cycle again, if not the program must do all the following 4 things at the end of the program once the user is finished inputting all inputs...

a.The smallest and largest of the inputs.

b.The number of even and odd inputs.

c.Cumulative totals. For example, if the input is 1 7 2 9, the program should print 1 8 10 19.

d.All adjacent duplicates. For example, if the input is 1 3 3 4 5 5 6 6 6 2, the program should print 3 5 6.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
class A{
   public static void main(String[] args) {
       int min=0,flag=0, max=0, count_even=0, count_odd=0, prev=0,n,cum=0,dup=0;
       String c="", d="";
       Scanner sc = new Scanner(System.in);
       do{
           System.out.println("Enter a number");
           n = sc.nextInt();
           cum+=n;
           c+=cum+" ";
           if(max<n)
               max=n;
           if(flag==0){
               min=n;
               flag++;
               dup=n;
           }
           else{
               if(min>n)
                   min=n;
               if(dup==n)
                   d+=dup+" ";
               dup=n;
           }
           if(n%2==0)
               count_even++;
           else
               count_odd++;
           System.out.println("Do you want to continue 1/0(yes/no)");
           n = sc.nextInt();
       }while(n!=0);
       System.out.println("Min = "+min);
       System.out.println("Max = "+max);
       System.out.println("No. of odd = "+count_odd);
       System.out.println("No. of even = "+count_even);
       System.out.println("Cumulative = "+c);
       String s[] = d.split(" ");
       LinkedHashSet<String> lhSetColors =
new LinkedHashSet<String>(Arrays.asList(s));
String[] newArray = lhSetColors.toArray(new String[ lhSetColors.size() ]);
       System.out.print("Duplicate = ");
       for(int i=0;i<newArray.length;i++)
           System.out.print(newArray[i]+" ");
   }
}

Add a comment
Know the answer?
Add Answer to:
Write a single program in java using only do/while loops for counters(do not use array pls)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • # Write PYTHON programs that read a sequence of integer inputs and print # a.  The...

    # Write PYTHON programs that read a sequence of integer inputs and print # a.  The smallest and largest of the inputs. # b.  The number of even and odd inputs. # c.  Cumulative totals. For example, if the input is 1 7 2 9, the program should print # 1 8 10 19. # d.  All adjacent duplicates. For example, if the input is 1 3 3 4 5 5 6 6 6 2, the # program should print...

  • For Java Write a program that reads a sequence of integer inputs all on one line...

    For Java Write a program that reads a sequence of integer inputs all on one line and prints: 1.)The smallest and largest of the inputs 2.)The number of odd and even inputs 3.)Cumulative totals. If the input is 1 7 2 9, the program prints 1 8 10 19. 4.)All adjacent duplicates. If the input is 1 3 3 4 5 5 6 6 6 2, the output is 3 5 6.

  • Write a program with loops that compute a.The sum of all even numbers between 2 and...

    Write a program with loops that compute a.The sum of all even numbers between 2 and 100 (inclusive). b.The sum of all squares between 1 and 100 (inclusive). c.All powers of 2 from 20 up to 220. d.The sum of all odd numbers between a and b (inclusive), where a and b are inputs. Note*: For part d, enter 3 and 21 as input Output should be: a. 2550 b. 338350 c. 1.0 2.0 4.0 8.0 ... 1048576.05 d. 120

  • In Python 3 - Write a program that reads a sequence of integer inputs from the...

    In Python 3 - Write a program that reads a sequence of integer inputs from the user. When the user is finished entering the integers, they will enter a 'q'. There is no need to check if the entry is a valid integer. All integers in the test data (input) will be between -255 and 255. The program should then print: The smallest and largest of the inputs. The number of even and odd inputs (0 should be considered even)...

  • WHILE LOOPS (13 points) Write a MATLAB program to find the smallest integer N such that:...

    WHILE LOOPS (13 points) Write a MATLAB program to find the smallest integer N such that: i> limit For example, if the limit is 5, the smallest N would be 3 since 1 + 2 + 3 = 6, and 6 > 5. If the limit is 15, the smallest N is 6 because 1+ 2+ 3+ 4+ 5+ 6 = 21. Note that N is not 5 in this case because 1+ 2+ 3+ 4+ 5 = 15, and...

  • Please do this in C++ using only for loops, while loops, and do while loops. No...

    Please do this in C++ using only for loops, while loops, and do while loops. No arrays. Use for loop, while loop, and do while loops to generate a table of decimal numbers, as well as the binary, octal, and hexadecimal equivalents of the decimal numbers in the range 1-256 Note: See Appendix for number systems The program should print the results to the Console without using any string formats Design Details: The Main program should prompt the user for...

  • Use Java program Material Covered : Loops & Methods Question 1: Write a program to calculate...

    Use Java program Material Covered : Loops & Methods Question 1: Write a program to calculate rectangle area. Some requirements: 1. User Scanner to collect user input for length & width 2. The formula is area = length * width 3. You must implement methods getLength, getWidth, getArea and displayData ▪ getLength – This method should ask the user to enter the rectangle’s length and then return that value as a double ▪ getWidth – This method should ask the...

  • C++ Write a program that asks for a number and then prints as many lines as...

    C++ Write a program that asks for a number and then prints as many lines as the user inputs. Each line contains as many pairs of characters ("*#") as the number of this line. It should look like the right half of a pyramid. Your version of the program must print the same result as the expected output. To to this lab, you must use two do-while loops. Two exceptions: When the user inputs a number less than 1, then...

  • Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a...

    Write a Java program that generates an array of Fibonacci numbers. Specifications: The program -Fills a one-dimensional array with the first 30 Fibonacci numbers using a calculation to generate the numbers. Note: The first two Fibonacci numbers 0 and 1 should be generated explicitly as in -long[] series = new long[limit]; //create first 2 series elements series[0] = 0; series[1] = 1; -But, it is not permissible to fill the array explicitly with the Fibonacci series’ after the first two...

  • C++ 4. Write a program that inputs 5 scores using a do while loop and asks...

    C++ 4. Write a program that inputs 5 scores using a do while loop and asks a user whether to continue or not ("yes" or "no il pressed you continues if pressed no' terutiinales) then calculates the average. 5. Make a program calculates sales for its division and quarterly earnings and show the total sum. Use 5 by 2 array matrix 6. This is an extra credil questioni Produce a program that displays by decreasing or decremeniting by 4 starting...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT