Computer Science, asked by rajeswaripurohit1, 6 months ago

int n = 345, s=0.
a. while (n>0)
{
r= n%10;
n= n/10;
s= s + r*2;
}
system.out.println(s)

plz answer only if u know the answer....... don't play its about studing ​

Answers

Answered by kishor9321
0

Answer:

Program for Sum of the digits of a given number

Given a number, find sum of its digits.

Examples :

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 remainder ‘%’ operator by dividing it with 10 and add it to sum.

Divide the number by 10 with help of ‘/’ operator

Print or return the sum

Below are the solutions to get sum of the digits.

1. Iterative:

C++

// C program to compute sum of digits in 

// number.

# include<iostream>

using namespace std;

/* Function to get sum of digits */

class gfg

{

    public:

    int getSum(int n)

    { 

    int sum = 0;

    while (n != 0)

    {

     sum = sum + n % 10;

     n = n/10;

    } 

 return sum;

 }

};

//driver code

int main()

{

 gfg g;

 int n = 687;

 cout<< g.getSum(n);

 return 0;

}

//This code is contributed by Soumik

C

Java

Python3

C#

PHP

Explanation:

HOPE MY ANSWERS HELPS U IF U HAD PLZZZ BRAINLIST ME AND FOLLOW ME

Similar questions