Question

In: Computer Science

Program Name: Divisible. Write a program that calculates the number of integers in a range from...

Program Name: Divisible. Write a program that calculates the number of integers in a range from 1 to some user-specified upper bound that are divisible by 3, the sum of those integers, and the number and sum of those integers not divisible by 3. Your program must display meaningful output which includes: the selected upper bound number of integers divisible by 3 sum of integers divisible by 3 number of integers not divisible by 3 sum of integers not divisible by 3

IN JAVA USING A WHILE LOOP

Solutions

Expert Solution

JAVA CODE :

import java.util.*;

public class Main
{
   public static void main(String[] args) {
       Scanner s = new Scanner(System.in);
       System.out.print("Enter the upper bound : ");
       int n = s.nextInt(); // read the upperbound
       int i = 1;
       int n1 = 0, n2 = 0, sum1 = 0, sum2 = 0;
       while(i <= n)
       {
       if(i % 3 == 0) // if divisible by 3
       {
       sum1 = sum1 + i; // sum
       n1++; // count
       }
       else{ // if not
       sum2 = sum2 + i; // sum
       n2++; // count
       }
       i++;
       }
       System.out.println("Count of no's divisible by 3 : " + n1);
       System.out.println("Sum of no's divisible by 3 : " + sum1);
       System.out.println("Count of no's not divisible by 3 : " + n2);
       System.out.println("Sum of no's not divisible by 3 : " + sum2);
      
   }
}

OUTPUT :


Related Solutions

Program Name: Divisible. Write a program that calculates the number of integers in a range from...
Program Name: Divisible. Write a program that calculates the number of integers in a range from 1 to some user-specified upper bound that are divisible by 3, the sum of those integers, and the number and sum of those integers not divisible by 3. Your program must display meaningful output which includes: the selected upper bound number of integers divisible by 3 sum of integers divisible by 3 number of integers not divisible by 3 sum of integers not divisible...
Write a program that will sum the integers between a given range (limit your range from...
Write a program that will sum the integers between a given range (limit your range from 0 to 50). For example, if the user want to add the integers between (and including) 1 and 10, then the program should add: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 Algorithm: Program title/description Ask the user to input a start value in the range 0 to 50. Remember to create the...
Write a program that prompts user to enter integers one at a time and then calculates...
Write a program that prompts user to enter integers one at a time and then calculates and displays the average of numbers entered. Use a while loop and tell user that they can enter a non-zero number to continue or zero to terminate the loop. (Switch statement) Write a program that prompts user to enter two numbers x and y, and then prompts a short menu with following 4 arithmetic operations: Chose 1 for addition Chose 2 for subtraction Chose...
write a mips program to find the number of elements of the array that are divisible...
write a mips program to find the number of elements of the array that are divisible by 3.
Write a C program that calculates the average grade for a specified number of students from...
Write a C program that calculates the average grade for a specified number of students from each student's test1 and test2 grades. The program must first ask the user how many students there are. Then, for each student, the program will ask the user for the test1 grade (grade #1) and test2 grade (grade #2). The program should be able to handle up to 100 students, each with 2 grades (test1 and test2). Use a two-dimensional float array to store...
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers...
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers between a given range (limit your range from 0 to 50). For example, if the user want to add the integers between (and including) 1 and 10, then the program should add: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 Algorithm: Program title/description Ask the user to input a start value in the...
Write a Java program that calculates a random number 1 through 100. The program then asks...
Write a Java program that calculates a random number 1 through 100. The program then asks the user to guess the number.If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.The program must let the user continue to guess until the user correctly guesses the number. ★Modify the program to output how many guesses it took the user to correctly guess the right number
Write a program that calculates the floor of a decimal number as defined here. Basically the...
Write a program that calculates the floor of a decimal number as defined here. Basically the Floor of any decimal number x, is the nearest whole number less than or equal to x. ( Code Should Be In C++) Requirements 1) You must implement a function to calculate the floor (you cannot use C++'s floor function). Name it floorAndError). It will have a return value and a single parameter 2) The program will ask the user to input a number...
Write a program named MakeChange that calculates and displays the conversion of an entered number of...
Write a program named MakeChange that calculates and displays the conversion of an entered number of dollars into currency denominations—twenties, tens, fives, and ones. For example, if 113 dollars is entered, the output would be twenties: 5 tens: 1 fives: 0 ones: 3. Answer in C# (P.S i'm posting the incomplete code that i have so far below.) using System; using static System.Console; class MakeChange { static void Main() { int twenties, tens, fives, ones; WriteLine("Enter the number of dollars:");...
Consider all positive integers less than 100. Find the number of integers divisible by 3 or...
Consider all positive integers less than 100. Find the number of integers divisible by 3 or 5? Consider strings formed from the 26 English letters. How many strings are there of length 5? How many ways are there to arrange the letters `a',`b', `c', `d', and `e' such that `a' is not immediately followed by`e' (no repeats since it is an arrangement)?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT