In: Computer Science
PLEASE DO THIS IN C#.All Hail Modulus Agustus! The modulus operator is used all the time. Realize that if you “mod” any number by a number “n”, you’ll get back a number between 0 and n-1. For example, “modding” any number by 20 will always give you a number between 0-19. Your job is to design (pseudocode) and implement (source code) a program to sum the total of all digits in an input integer number between 0 and 1000, inclusive. Notice that you need to extract individual digits from the input number using the remainder (modulus) and division mathematical operators. For example, if the input number is 123, the sum of its digits is 6. Document your code and properly label the input prompts and the outputs as shown below. Sample run 1: Entered number: 123 Sum of digits: 6
Following are the things which you need to take into consideration the above program
Following is the code for the same in C#
using System;
public class SumOfDigits
{
public static void Main(string[] args)
{
// Variables
int n,sum=0,m;
// Print statements before letting the user enter the number
Console.Write("Entered Number: ");
// input the number
n= int.Parse(Console.ReadLine());
while(n>0)
{
// Going for each digit from
behind
// which will be stored in m
m=n%10;
// Adding the each m in sum
sum=sum+m;
// removing the last digit
n=n/10;
}
Console.Write("Sum of Digits= "+sum);
}
}
Following is the snippet of the output in a Run.
That was a nice
question to answer
Friend, If you have any doubts in understanding do let me know in
the comment section. I will be happy to help you further.
Please like if you think effort deserves a like.
Thanks