The Rule of 10 check This one gets more complicated, starting on the right side,
ID: 3916647 • Letter: T
Question
The Rule of 10 check
This one gets more complicated, starting on the right side, add up each of the odd digits, the very last digit is where you start, it is the the first number you add and then go backwards adding up the digits.
Starting with the next to last digit in the number, work backwards going every other digit as before, use the following formula to determine what you add to the sum.
If the doubled value < 10, add it to your sum
If the doubled value >10, split it again into its individual digits and add both of them to your sum
Example: 5 would double to 10 which would add 1 + 0 or 1 to the sum, 6 would double to 12 which would add 1+2 or 3 to the sum, etc.
When you are done, if the operation of that sum modulus 10 is 0, the number is valid. if not, then it is invalid
A sum of 75 would not be valid where a sum of 80 would be
I have no idea how to complete this.... I need to figure it out asap! Please help! Need it in C#
Explanation / Answer
Ques : starting on the right side, add up each of the odd digits, the very last digit is where you start, it is the the first number you add and then go backwards adding up the digits?
Ans:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
//Your code goes here
Console.WriteLine("Enter number : ");
// read integer from user
int x = int.Parse(Console.ReadLine());
int sum = 0;
// loop untill we traverse through each digit in x
while( x > 0 )
{
// get the right most digit in x
int temp = x % 10;
// if the current digit is odd
if( temp % 2 != 0 )
sum += temp;
// remove the digit at ones place
x = x / 10;
}
if( sum % 10 == 0 )
Console.WriteLine("Number is valid.");
else
Console.WriteLine("Number is invalid.");
}
}
}
Ques .Starting with the next to last digit in the number, work backwards going every other digit as before, use the following formula to determine what you add to the sum /
Ans :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
//Your code goes here
Console.WriteLine("Enter number : ");
// read integer from user
int n = int.Parse(Console.ReadLine());;
// remove the last digit from n as we have to start
// from next to last digit
int x = n / 10;
int sum = 0;
// loop untill we traverse through each digit in x
while( x > 0 )
{
// get the right most digit in x
int temp = x % 10;
// find the double of temp
int y = 2 * temp;
// If the doubled value < 10, add it to your sum
if( y < 10 )
sum += y;
// If the doubled value >10, split it again into its individual digits and add both of them to your sum
else
{
// y % 10 gives the last digit
// y / 10 removes the last digit
sum += ( y % 10 ) + ( y / 10 );
}
// remove the digit at ones place
x = x / 10;
}
if( sum % 10 == 0 )
Console.WriteLine("Number is valid.");
else
Console.WriteLine("Number is invalid.");
}
}
}
Sample Output
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.