Question

Java: All Hail Modulus Agustus! The modulus operator is used all the time. Realize that if...

Java: All Hail Modulus Agustus! The modulus operator is used all the time. Realize that if you “mod” any number by a number “n”, you’ll get back a number between 0 and n-1. For example, “modding” any number by 20 will always give you a number between 0-19. Your job is to design implement (source code) a program to sum the total of all digits in an input integer number between 0 and 1000, inclusive. Notice that you need to extract individual digits from the input number using the remainder (modulus) and division mathematical operators. For example, if the input number is 123, the sum of its digits is 6. Document your code and properly label the input prompts and the outputs as shown below.

Sample run 1: Entered number: 123 Sum of digits: 6

Sample run 2: Entered number: 588 Sum of digits: 21

Sample run 3: Entered number: 100 Sum of digits: 1

Homework Answers

Answer #1


import java.util.Scanner;

public class SumDigit {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number: ");
int num = sc.nextInt();
int r = 0, sum = 0;
int dup = num;
while(dup>0){
r = dup %10; // IT GETS END DIGIT OF NUMBER FOR EX 123 IT RETURN 3
sum = sum + r;
dup /= 10; // IT RETURN NUMBER/10 FOR EX 123/10 = 12
}
System.out.println("Entered Number: "+num+" Sum of digits: "+sum);
}
  
}

/* OUTPUT */

Know the answer?
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
Similar Questions
Java: Distance calc. This question is fairly straightforward. Design implement (source code) a program to compute...
Java: Distance calc. This question is fairly straightforward. Design implement (source code) a program to compute the distance between 2 points. The program prompts the user to enter 2 points (X1, Y1) and (X2, Y2). The distance between 2 points formula is: Square_Root [(X2 – X1)^2 + (Y2 – Y1)^2] Document your code, properly label the input prompts, and organize the outputs as shown in the following sample runs. Note: for C++, #include and then call sqrt(). For Java, you’ll...
design a program that prompts the user to enter a positive integer number and reads the...
design a program that prompts the user to enter a positive integer number and reads the user’s input. If the user input is not a positive number, the program should prompt them repeatedly until they enter a valid input. Once a valid input is received ,the program uses a loop to calculate the sum of the digits of the user’s input and displays the sum. For example, if the user enters the number 94311, the program should print the sum...
Write a program that asks a user to enter his/her phone number. Then remove all the...
Write a program that asks a user to enter his/her phone number. Then remove all the non-digits from the phone number and then display the digits entered. Hint: you may need to use the syntax “.isdigit()” in your program Sample run Example 1 Please enter your phone number?123-456/7891 1234567891 Example 2 Please enter your phone number?123(456)-7891 1234567891 Example 3 Please enter your phone number?987@654!4321 9876544321
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for...
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for an integer between 0 and 9999, inclusive. The script should then calculate the digit in each of the 1000’s, 100’s, 10’s, and 1’s place of the number. Create a variable for each of the 4 extracted digits. For example, if your variables are named nThousands, nHundreds, nTens, and nOnes, then, in the case of 9471, they would be end up being set to 9,...
Problem: Our Armstrong number Please write code for C language So far we have worked on...
Problem: Our Armstrong number Please write code for C language So far we have worked on obtaining individual digits from 4 digits or 5 digit numbers. Then added them to find the sum of digits in various examples and assignments. However, the process of extracting individual digits is actually can be solved using a loop as you were doing a repetitive task by using mod operation and division operation. Now, we know how loops work and we can remove the...
Please code in Java and please implement constarints Digital Root and Iterations Given a non-negative integer,...
Please code in Java and please implement constarints Digital Root and Iterations Given a non-negative integer, print out its digital root and the number of iterations required to reach it. The digital root is the single digit number obtained by an iterative process of finding the sum of digits. In the next iteration, the sum of the digits in the previous iteration is computed, and the process repeated until a single digit value is obtained. Input Format The first line...
Goal: Write a simple number guessing game in java. The game picks a number between bounds...
Goal: Write a simple number guessing game in java. The game picks a number between bounds input by the user, then user has to guess the number. The game will tell you if your guess is too high or too low. When you guess it, the game will tell you how many guesses it took Run multiple games and print statistics (# games, # guesses, avg) when all done. Sample Run: G U E S S I N G G...
Problem: Certain mathematical problems involve manipulation of the digits within a whole number. One such problem...
Problem: Certain mathematical problems involve manipulation of the digits within a whole number. One such problem requires that the digits be re-arranged.In this project, we will reverse the order of the digits in a number. Assignment: Design, develop, and test an Object-Oriented C++program that reads a whole number from the user, removes the sign and all leading and trailing zeroes from that number, and then performs the following operations on that number: 1) reverses the digits, 2) sorts the digits...
Write a JAVA program that reads in a string from standard input and determines the following:...
Write a JAVA program that reads in a string from standard input and determines the following: - How many vowels are in the string (FOR THE PURPOSE OF THIS PROGRAM 'Y' is NOT considered a vowel)? - How many upper case characters are in the string? - How many digits are in the string? - How many white space characters are in the string? - Modify the program to indicate which vowel occurs the most. In the case of a...
This question is broken into 3 parts, each of which builds on the functions developed in...
This question is broken into 3 parts, each of which builds on the functions developed in the previous part. Note that you can (and should) still answer parts (b) and (c) even if you cannot answer the preceding parts - just assume that the functions work as they should, and continue. Please explain the code as well Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT