This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
What is a debugger and how can it help me diagnose problems?
(2 answers)
Closed 6 months ago.
I'm fifteen and I'm new in this job.I try to learn this language for my future life.I entered 1,2,3,4,5. Finally he says conclusion is zero i searched but i couldn't find anything.can you help me please.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project3
{
internal class Program
{
static void Main(string[] args)
{
//variable assigment
int evennumber = 0;
int oddnumber = 0;
float ratio;
//its for get numbers
for(int i = 1; i <= 5; i++)
{
Console.WriteLine("Enter" + i ". number : ");
int number=Convert.ToInt32(Console.ReadLine());
//its for determine even and odd number
if (number%2 == 0)
{
evennumber = evennumber + number;
}
else
{
oddnumber = oddnumber + number;
}
}
ratio =evennumber/oddnumber;
Console.WriteLine("Ratio : " + ratio);
Console.Read();
}
}
}
Related
So I've been trying to make a Luhn-Algorithm what technically should now work but I searched a bit through the Internet and no one uses a Main method so neither do I, but my Questions or more like my Problem is how do I run the code without a Main Method??
Where do I put my Main method?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace LuhnAlgorithm
{
public static class Luhn
{
static bool IsValid(string number)
{
if (string.IsNullOrEmpty(number)) return false;
bool onSecondDigit = false;
int count = 0;
int sum = 0;
for (int i = number.Length - 1; i >= 0; i--)
{
char c = number[i];
if (!char.IsDigit(c))
{
if (c == ' ') continue;
return false;
}
int digit = c - '0';
if (onSecondDigit)
{
digit *= 2;
if (digit > 9) digit -= 9;
}
sum += digit;
count++;
onSecondDigit = !onSecondDigit;
}
return count > 1 && sum % 10 == 0;
}
}
}
You always need an entry point for your application which is the Main method of your program class. Since C#9 you can use top-level-statements in console applications where C# will add a class and Main method for you. So you don't have to implement it but technically it exists.
See https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/program-structure/top-level-statements
You just implemented a class and most code examples don't do more because it is expected that you know how to write a simple C# application where you can use that class.
This question already has answers here:
Meanings of declaring, instantiating, initializing and assigning an object
(2 answers)
Closed 2 years ago.
Why does the code add up all the elements of the arr array just by using sum += x doesn't that mean
" 0 = 0 + x"?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main(string[] args)
{
int[ ] arr = {11, 35, 62, 555, 989};
int sum = 0;
foreach (int x in arr) {
sum += x;
}
Console.WriteLine(sum);
}
}
}
It is to initialize the sum parameter to start with 0 and to add to it the number in the array.
In C# the default value of int is 0 so the row can also be: int sum;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
always one is the result of sum four numbers that less than one.
i changed the number to decimal and float
i used math,truncate
i wrote double before each number in calculation
still the result of sum is one
this my code and I added comment in the code in the place of my quistion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm3 : System.Web.UI.Page
{
public double[,] array = new double[4, 4];
public double b1vsb1c1=1.0;
public double b1vsb2c2;
public double b1vsb3c3;
public double b1vsb4c4;
// // .....here cotinue declear variable ,I deleteed to shoritng the code;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
string isChecked2 = RadioButtonList1.SelectedItem.Value.ToString();
if (RadioButtonList1.SelectedItem.ToString() == "1")
{
b1vsb1c1 = 1.0;
b2vsb1c1 = 1.0;
}
else if (RadioButtonList1.SelectedItem.ToString() == "3")
{
b1vsb2c2 =3.0;
b2vsb1c1 =1.0/3.0;
}
// .....here cotinue test radio button value ,I deleteed to shoritng the code;
array[0,0]=b1vsb1c1;
array[0,1]=b1vsb2c2;
//....here cotinue assign value to array ,I deleteed to shoritng the code;
for (int i = 0; i < 4; i++)
{
sum = 0.0;
av = 0.0;
for (int j = 0; j < 4; j++)
{
sum=(double)sum + (double)array[i,j];
}
// here the sum always one why?!!
av = (double)sum/(double)4.0;
Response.Write(av + "| |");
// here is always result 0.25 why?
}
}
}
}
Try to use an Implicit typing : var to declare your varibales
I tried what you said; I have logic result : here is a sample exmaple:
public class Program
{
public static void Main(string[] args)
{
var p1 = 0.120336943441637;
var p5 = 0.01323706377858;
var p9 = 0.120336943441637;
var p13 = 0.120336943441637;
var sum = p1 + p5 + p9 + p13;
Console.WriteLine(sum);
}
}
Result = 0.374247894103491
Code Run
I was trying to solve this problem projecteuler,problem125
this is my solution in python(just for understanding the logic)
lim = 10**8
total=0
found= set([])
for start in xrange(1,int(lim**0.5)):
s=start**2
for i in xrange(start+1,int(lim**0.5)):
s += i**2
if s>lim:
break
if str(s) == str(s)[::-1]:
found.add(s)
print sum(found)
the same code I wrote in C# is as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static bool isPalindrome(string s)
{
string temp = "";
for (int i=s.Length-1;i>=0;i-=1){temp+=s[i];}
return (temp == s);
}
static void Main(string[] args)
{
int lim = Convert.ToInt32(Math.Pow(10,8));
var found = new HashSet<int>();
for (int start = 1; start < Math.Sqrt(lim); start += 1)
{
int s = start *start;
for (int i = start + 1; start < Math.Sqrt(lim); i += 1)
{
s += i * i;
if (s > lim) { break; }
if (isPalindrome(s.ToString()))
{ found.Add(s); }
}
}
Console.WriteLine(found.Sum());
}
}
}
the code debugs fine until it gives an exception at Console.WriteLine(found.Sum()); (line31). Why can't I find Sum() of the set found
The sum is: 2,906,969,179.
That is 759,485,532 greater than int.MaxValue;
Change int to long in var found = new HashSet<long>(); To handle the value.
You can also use uint however instead of long, however I would recommend using long.
Here is my current code for computing Pi using the chudnovsky method in c#:
using System;
using System.Diagnostics;
using System.IO;
using java.math;
namespace pi.chudnovsky
{
public class Program
{
static Double Factorial(Double fact)
{
//begin factorial function
if (fact <= 1)
return 1;
else
return fact * Factorial(fact - 1); //loops multiplication until the factorial is reached
}
static Double doSummation(Double maxPower)
{
//begin chudnovsky summation function
Double sum = 0;
for (int i = 0; i <= maxPower; i++) //starts at i=0
{
sum += ((Math.Pow(-1, i)) * Factorial(6 * i) * (13591409 + 5451401 * i)) / (Factorial(3 * i) * Factorial(i) * Factorial(i) * Factorial(i) * Math.Pow(640320, (3 * i + 1.5))); //chudnovsky algorithm
}
return sum;
}
static void Main(string[] args)
{
int num;
Console.WriteLine("Enter how many terms to compute Chudnovsky summation: ");
//begin stopwatch
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
//parse user input
num = Convert.ToInt32(Console.ReadLine());
//perform calculation
Double inv = 1 / (12 * doSummation(num));
//stop stopwatch
stopwatch.Stop();
//display info
Console.WriteLine(inv);
Console.WriteLine("3.14159265358979323846264338327950288419716939937510");
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed.TotalMilliseconds);
//write to pi.txt
TextWriter pi = new StreamWriter("pi.txt");
pi.WriteLine(inv);
pi.Close();
//write to stats.txt
TextWriter stats = new StreamWriter("stats.txt");
stats.WriteLine(stopwatch.Elapsed.TotalMilliseconds);
stats.Close();
}
}
}
So, I've included the J# library, and included java.math. Now when I replace all the "double"s with "BigDecimal"s, I get these compile errors:
http://f.cl.ly/items/1r2X26470d0d0n260p0p/Image%202011-11-14%20at%206.16.19%20PM.png
I know that this isn't the problem with me using Int for the loops, as it worked perfectly with Doubles. My question is, how do you resolve these errors relating to int and BigDecimal, or can you recommend another arbitrary precision library?
I've tried using XMPIR, have gotten everything to compile, but I get:
http://f.cl.ly/items/1l3C371j2u3z3n2g3a0j/Image%202011-11-14%20at%206.20.24%20PM.png
So I can I use p/invoke to include xmpir so I can use whatever the bigdecimal class is?
Thank you for your time!
Can you not convert your int's to BigDecimal's before comparing them?
I assume you understand the problem here is that there is no operator overload for the greater, less than, etc. signs on the BigDecimal class that accepts an int.