Friday, January 15, 2021

Q.11: How to find the sum of digits of a positive integer?

 Ans.: The user will input a positive integer and the method should return the sum of all the digits in that integer.

  • input: 168, output: 15
internal static void SumOfDigits(int num)
{
int sum = 0;
while (num > 0)
{
sum += num % 10;
num /= 10;
}
Console.WriteLine(sum);
}

No comments:

Post a Comment

Get max value for identity column without a table scan

  You can use   IDENT_CURRENT   to look up the last identity value to be inserted, e.g. IDENT_CURRENT( 'MyTable' ) However, be caut...