Question

b. Compute the weighted sum of digits of a number read from the keyboard in the...

b. Compute the weighted sum of digits of a number read from the keyboard in the following sense: Let us calculate sum of digits multiplying each digit by its position (counting from the left, starting from 1). For example, given the value 1971 we calculate such weighted sum of digits (let us call it "wsd") as: wsd(1971) = 1 * 1 + 9 * 2 + 7 * 3 + 1 * 4 = 41

I need in Java program

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

Dear Student,

below i written the Java code as per the requirement.Please note that the given example output is incorrect. The wsd would be 44.

Please note the below program has been tested on ubuntu 16.04 system and compiled using javac compiler. This code will also work on code blocks.

-----------------------------------------------------------------------------------------------------------------------------------

Program:

------------------------------------------------------------------------------------------------------------------------------------

import java.util.Scanner;

//public class

public class Weighted_Sum

{
//this is the main class

public static void main(String args[])

{

//variables

int count = 0, num;

Scanner in = new Scanner(System.in);

System.out.println("Enter a num: ");

num = in.nextInt();

//create an array to store the digits

int [] digit = new int[100];

//while loop to find the digits and store into an array

while(num>0)

{

digit[count] = num%10;

num = num/10;

count++;

}

System.out.println(count);

int mul = 1, wsd = 0;

for(int i = count-1; i>=0; i--)

{

//calculate weighted sum

wsd = wsd + digit[i] * mul;

mul++;

}

//display wsd

System.out.println("Weigted sum is = "+wsd);

}

}


==================================================================

Sample Output:

====================================================================

KIndly Check and Verify Thanks..!!!

Add a comment
Know the answer?
Add Answer to:
b. Compute the weighted sum of digits of a number read from the keyboard in the...
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
  • Program : Write a complete C++ program that Input: Read a positive integer from the keyboard...

    Program : Write a complete C++ program that Input: Read a positive integer from the keyboard (user) with proper prompt text and save it to a variable. The integer have up to five digits. Processing: From the right most digit, extract every digit from the input integer using modular operator %then save the digit to a separate variable. Remove the right most digit from the integer using integer division operator /. Repeat above two steps till all digits have been...

  • Introduction: Ten digit ISBN numbers are created so that the first nine digits are information digits...

    Introduction: Ten digit ISBN numbers are created so that the first nine digits are information digits and the last digit is a check digit. T his last number helps people notice and correct mistakes that might be made in recording the information digits. The same is true for thirteen digit ISBN numbers. Here is a ten digit ISBN number: 0-13-149498-8. The digit 0 indicates the book is written for English-speaking people. The number 13 and the number 149498 identify the...

  • Using c++ A ten diglt ISBN number uses a checksum as its last diglt to verlfy...

    Using c++ A ten diglt ISBN number uses a checksum as its last diglt to verlfy the first nine digits are valid. Before 2007, all ISBN numbers were composed like this, such as: e-20-5e8005-7 or 1-234-56789-X The first nine digits are assigned by a book's publisher and the last digit is calculated by "weighted sum (described below). The X stands for the checksum value of 10, in order to represent ten as a single digit. You must write a program...

  • I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the...

    I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the program should read your credit card (for example: 5278576018410787) and verify whether it is valid or not. In addition, the program must display the type (i.e. name of the company that produced the card). Each credit card must start with a specific digit (starting digit: 1st digit from left to ight), which also is used to detemine the card type according Table 1. Table...

  • To use the digits function, enter 1 To use the average function, enter 2 To use the perfect sum f...

    use matlab To use the digits function, enter 1 To use the average function, enter 2 To use the perfect sum function, enter3 To exit the program, enter 4 Please select a number = 6 Please re-select again: 2 please enter the first number 3 please enter the second number: 6 please enter the third number: 3 The average equals to: 4 Write a function, called digits function that is able to calculate the number of digits and the summation...

  • 1) Create a main() method with a switch statement that calls either a sum() OR factorial()...

    1) Create a main() method with a switch statement that calls either a sum() OR factorial() method, depending on what selection the user of the program makes - ask the user to enter a selection (with System.out.println()), create a Scanner then read the user's input with a call to Scanner next(), then call the appropriate method in a switch (the String that was read from the call to Scanner.next() should be what you use as your switch condition). 2) Create...

  • Using Racket Create a Racket procedure compute_pos that reads numbers from the keyboard until 0 is...

    Using Racket Create a Racket procedure compute_pos that reads numbers from the keyboard until 0 is read, count how many positive numbers are read, and also compute the sum of only positive numbers, and display the count and the sum of positive numbers in different lines. Note that 0 is not included in the computations. For example, if user enters: 3 6 -4 12 15 -9 -24 4 -3 -5 1 0 then your procedure should print positive count: 6...

  • Write a java recursive method digitMatch that takes two nonnegative integers as parameters and that returns...

    Write a java recursive method digitMatch that takes two nonnegative integers as parameters and that returns the number of digits that match between them. Two digits match if they are equal and have the same relative position starting from the end of the number (i.e., starting with the ones digit). In other words, the method should compare the last digits of each number, the second-to-last digits of each number, the third-to-last digits of each number, and so forth, counting how...

  • Write an application (SpaceDigits) that: (in java) a. inputs one number consisting of five digits from...

    Write an application (SpaceDigits) that: (in java) a. inputs one number consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339, the program should print “4 2 3 3 9.” (Hint: The number input is five digits long, dividing it by 10000 gives the leftmost digit. digit1 = number / 10000 ) b. determines...

  • PLEASE HELP!!!I need help with the following JAVA PROGRAMS. Please ready carefully. So this comes from...

    PLEASE HELP!!!I need help with the following JAVA PROGRAMS. Please ready carefully. So this comes from tony gaddis starting out with java book. I have included the screenshot of the problems for reference. I need HELP implementing these two problems TOGETHER. There should be TWO class files. One which has TWO methods (one to implement problem 8, and one to implement problem 9). The second class should consist of the MAIN program with the MAIN method which calls those methods....

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