Computer Science, asked by tiyarawat1, 5 months ago

1. Write a program to input
a number find the sum
of digits and the number of eligits, Display the output

Answers

Answered by Laraleorapathi
2

Explanation:

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

Answered by imransaila8
0

Answer:

example 1 : Input : n = 687

Output : 21

example 2 : Input : n = 12

Output : 3

hope it helps you . If yes then please mark my answer as BRAINLIEST

Similar questions
Math, 11 months ago