You must Sign In to post a response.
• # C# Program for an Addition of Whole Number in Alternative Way

I have a Doubt in C# program. I was going through an program.
This was the problem i faced.
There is a whole number 7634
So here Adding (73) & (64) i.e First Digit & Third Digit with Second digit & fourth digit. So answer will be 137

I tried different techniques for creating this program. No use.
So please suggest me the techniques or program for this one (C#)
• Hi,

In this case logic you need follow,
1. First split all the values.

see the simple code for that below,

` string[] strNum = TextBox1.Text.Split(' '); string strFirstNum = strNum[0]; string strSecNum = strNUM[1]; string strLastNum = strNum[2]; Response.Write(strFirstNum); Response.Write("<br>"); Response.Write(strSecNum); Response.Write("<br>"); Response.Write(strLastNum);`

I believe once you split the value you will be able to add it. Let me know if you still require any help from me.

Regards,
Asheej T K

• You can use following code snippet for Addition of Whole Numbers in C#
`public class Addition { public static void Main( string[] args ) {int num1; int num2; int sum; Console.Write( "Enter first integer: " ); num1 = Convert.ToInt32( Console.ReadLine() ); Console.Write( "Enter second integer: " ); num2 = Convert.ToInt32( Console.ReadLine() ); sum = num1 + num2; Console.WriteLine( "Sum is {0}", sum ); } Results OutputEnter first integer: 50Enter second integer: 70Sum is 120`

• Hi Mohamed,

In this case, You need to split the numbers into digits. Please follow the below snippet:

int number = 7634;
string FirstNum;
string SecondNum;
string ThirdNum;
string FourthNum;

string Temp1;
string Temp2;

int Result;

FirstNum = number.ToString().Substring(0, 1);
SecondNum = number.ToString().Substring(1, 1);
ThirdNum = number.ToString().Substring(2, 1);
FourthNum = number.ToString().Substring(3, 1);

Temp1 = FirstNum + ThirdNum;
Temp2 = SecondNum + FourthNum;

Result = Convert.ToInt32(Temp1) + Convert.ToInt32(Temp2);

Result:137

Thanks,

• Thanks For All Your Responses. I have utilized all your concepts & created Program in two different ways. So if any one wanna see my Coding. Request me, I will Post Over Here

• Hai Mohamed Sheik,
You can use the below generic code where you can enter the number and then it will count and get the sum of even and odd digits:
`using System;namespace DemoOops{ class Program { static void Main(string[] args) { Console.WriteLine("please enter the number..."); string originalNumber = Console.ReadLine().ToString(); int i = 0; string x = string.Empty, y = string.Empty; foreach (char c in originalNumber.ToCharArray()) { if (i % 2 == 0) x += c.ToString(); else y += c.ToString(); i++; } Console.WriteLine("First Number:" + x.ToString()); Console.WriteLine("Second Number:" + y.ToString()); Console.WriteLine("Sum is: " + (Convert.ToInt32(x) + Convert.ToInt32(y))); Console.ReadLine(); } }}`
Hope it will be helpful to you.

Regards,
Pawan Awasthi(DNS MVM)
+91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
pawansoftit@gmail.com