Javatpoint Logo
Javatpoint Logo

Program to print the sum of digits without using modulus

In this program, we have to add the digits of the entry number without the logic of using modulus(%).

Example:

149: the sum of its digits (1, 4, 9) is 14.

Go through the algorithm to code for this program:

Algorithm

  • STEP 1: START
  • STEP 2: ENTER n as a string
  • STEP 3: SET sum =0
  • STEP 4: SET i = 0
  • STEP 4: REPEAT STEP 5 and 6 UNTIL (i<length(n))
  • STEP 5: sum =sum + n[i]
  • STEP 6: sum = sum - 48
  • STEP 7: i = i+1
  • STEP 7: PRINT sum
  • STEP 8: END

Java Program

Output:

Enter the number?
345
sum of digits: 12

C Program

Output:

Enter the number? 45
sum of digits: 9

C# Program

Output:

Enter the number? 75
sum of digits: 12

Python Program

Output:

	Enter the number? 175
	sum of digits: 13

PHP Program

Output:

Enter the number? 175
sum of digits: 13
Next Topic#




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA