Question

Script 1: Sum of Numbers Write a python program that asks the user to enter a...

Script 1: Sum of Numbers Write a python program that asks the user to enter a series of single-digit numbers with nothing separating them. The program should display the sum of all the single digit numbers in the string. For example, if the user enters 2514, the method should return 12, which is the sum of 2, 5, 1, and 4.

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

Program:

import java.util.Scanner;
public class SumofDigits { // Here taking the class name as SumofDigits
public static int SumofDigits(int num) // Definition of SumofDigits() method
{
int n=0,sum=0; // Here Initializing integer varaibles called n,sum
while(num!=0)
{ // Loop Iteration
n=num%10; // Here calculating n value
sum+=n; // Here adding n value to sum
num=num/10; // Here dividing num with 10
} // End of while loop
return sum; // Here Sum is Returned to main()
} // End of SumofDigits() method
public static void main(String args[]) { //Start of main()
int Num;// Here declaring integer varaible called NUm
Scanner in = new Scanner(System.in);
System.out.println("Enter Series of single-digit numbers: ");
Num = in.nextInt(); // HEre scanning User input
SumofDigits ob=new SumofDigits(); // Here creating Object called ob for class SumofDigits
System.out.println("The sum is "+ob.SumofDigits(Num)); // Here calling SumofDigits() method using object ob
} // End of main()
}// End of class SumofDigits

Output:

Add a comment
Know the answer?
Add Answer to:
Script 1: Sum of Numbers Write a python program that asks the user to enter a...
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
  • Design a program in pseudocode that asks the user to enter a string containing a series...

    Design a program in pseudocode that asks the user to enter a string containing a series of ten single-digit numbers with nothing separating them. The program should display the sum of all the single digit numbers in the string except for the highest value digit. You may only use one main module and one sorting function; otherwise, do not modularize your program. For example, if the user enters 5612286470, the sum would be 33. (0+1+2+2+4+5+6+6+7) If there are multiple highest...

  • 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....

  • c++ pleased Assignment 6a (15 points] - Character and String related processing... Listed below are two...

    c++ pleased Assignment 6a (15 points] - Character and String related processing... Listed below are two interesting programming challenges that will require a bit of character and/or string related processing with functions. (Carefully read the specifications for each programming challenge and select ONE.) (1) Sum of Digits in a String Write a program that asks the user to enter a series of single-digit numbers with nothing separating them. Read the input as a C-string or a string object. The program...

  • In Java, write a program using a loop that asks the user to enter a series...

    In Java, write a program using a loop that asks the user to enter a series of decimal numbers. The user must enter -88 to end the input of the decimal numbers. After the user enters all numbers, the program should display the sum of all numbers entered.

  • Write a program that asks the user to enter a string and then asks the user...

    Write a program that asks the user to enter a string and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the string. python programming

  • Extra Credit Assignment 4 Write a PYTHON program that asks the user to enter a series...

    Extra Credit Assignment 4 Write a PYTHON program that asks the user to enter a series of numbers. You can decide how many numbers to enter or the user can. As the user enters each number, add it to a list. After all the numbers are added, determine: What is the highest number in the list What is the lowest number in the list What is the sum of all the numbers in the list No error checking is required....

  • Write a python script that takes a six-digit integer from the user and separate the number...

    Write a python script that takes a six-digit integer from the user and separate the number into its individual digits. You program should then display each digit separated by a comma each. For example, if the user types in the number 654321, the script should display: 6,5,4,3,2,1 You can assume that the user enters the correct number of digits.

  • Write a python program that does the following. a. It asks the user to enter a...

    Write a python program that does the following. a. It asks the user to enter a 5-digit integer value, n. b. The program reads a value entered by the user. If the value is not in the right range, the program should terminate. c. The program calculates and stores the 5 individual digits of n. d. The program outputs a “bar code” made of 5 lines of stars that represent the digits of the number n. For example, the following...

  • IN PYTHON write a program that asks the user to enter 10 numbers and store them...

    IN PYTHON write a program that asks the user to enter 10 numbers and store them in the list and then find the followings: • Maximum • Minimum prime numbers in the list • even numbers in the list • odd numbers in the list.

  • Using loops, write a C# program that asks the user to enter repeatedly an integer number,...

    Using loops, write a C# program that asks the user to enter repeatedly an integer number, it stops when the user enters -1, then the program should display how many numbers were entered, their the sum and their average

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