Use a console application to accept an integer greater than 100 from the keyboard, and then output the value of each digit of the integer, and output the results of these bit additions. It is required to use character extraction method and certificate Division method respectively. Character extraction means that the integer is converted to a string, followed by each character in the string, and each character is converted to an integer summation. Integer division method is to find the value of each bit by using the method of rounding and remainder, in order to find the number of these bits.
The program works as shown in the figure
The code is as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
namespace A._2._3 character extraction and integer divisibility exercises
{
Class Program
{
static void Main (string[] args)
{
Console.Write ("Please enter an integer greater than 100:");
int a = Int. Parse (Console.ReadLine ());
string s = a.tostring ();
Console.WriteLine ("This integer has {0} bits.") ", s.length);
string m;
int sum1 = 0,sum2=0;
Console.Write ("Realization idea 1: Each digit value is");
for (int i = 0; i < s.length i++)//To be implemented by character extraction
{
m = s.substring (i, 1);
sum1 = Int. Parse (m);
Console.Write (m);
if (i!= s.length-1)
Console.Write (",");
}
Console.WriteLine (", the sum of these bits is" + sum1);
Console.Write ("Realization idea 2: Each digit value is");
int Temp1;
int temp2;
int ten=1;
for (int i= s.length; i>=1;i--)//Integer divide method
{
for (int j = 1; J <= I; j + +)
Ten *= 10;
Temp1 = a% ten;
Temp2 = Temp1/(TEN/10);
Console.Write (TEMP2);
Sum2 + = Temp2;
if (i!=1)
Console.Write (",");
Ten = 1;
}
Console.WriteLine (", the sum of these bits is" + sum2);
Console.readkey ();
}
}
}