Write a program to input a number from the user and display the sum of all its digits except its first and last digit using the function void dispSum(int num). If the number has less than 3 digits then display sum of all its digits with an appropriate message.
IN JAVA, NO SPAM
CORRECT ANSWER TO BE MARKED AS
" BRAINLIEST "
Answers
Answer:
Program for Sum of the digits of a given number
Given a number, find sum of its digits.
Examples :
Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.
Input : n = 687
Output : 21
Input : n = 12
Output : 3
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
General Algorithm for sum of digits in a given number:
Get the number
Declare a variable to store the sum and set it to 0
Repeat the next two steps till the number is not 0
Get the rightmost digit of the number with help of the remainder ‘%’ operator by dividing it by 10 and add it to sum.
Divide the number by 10 with help of ‘/’ operator to remove the rightmost digit.
Print or return the sum
Below are the solutions to get sum of the digits.
1. Iterative:
Explanation:
Program for Sum of the digits of a given number
Given a number, find sum of its digits.
Examples :
Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.
Input : n = 687
Output : 21
Input : n = 12
Output : 3
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
General Algorithm for sum of digits in a given number:
Get the number
Declare a variable to store the sum and set it to 0
Repeat the next two steps till the number is not 0
Get the rightmost digit of the number with help of the remainder ‘%’ operator by dividing it by 10 and add it to sum.
Divide the number by 10 with help of ‘/’ operator to remove the rightmost digit.
Print or return the sum
Below are the solutions to get sum of the digits.
1. Iterative: