C # Programming and application tutorials on machine a.2.3 ———— character extraction and integer divide-and-call (Console) __c#

Source: Internet
Author: User

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 ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.