(check(k.ToString() == 1)) [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a maybe simple problem. When i start program it show "Operator '==' cannot be applied to operands of type 'string' and 'int'. I dont know what to do with it. Thanks for every help.
Here is code:
class Program
{
static bool check(string input_number)
{
for (int i = 0; i < input_number.Length / 2; i++)
if (input_number[i] != input_number[input_number.Length - i - 1])
return false;
return true;
}
static void Main(string[] args)
{
var results = from i in Enumerable.Range(100, 900)
from j in Enumerable.Range(i, 1000 - i)
let k = i * j
where (check(k.ToString() == 1)
orderby k descending
select new { i, j, k };
var highestResult = results.FirstOrDefault();
if (highestResult == null)
Console.WriteLine("There are no palindromes!");
else
Console.WriteLine($"The highest palindrome is {highestResult.i} * {highestResult.j} = {highestResult.k}");
Console.ReadKey();
}
}

You are trying to compare strings (k.ToString() with numbers (1). In your case, I think, you need to do this: where check(k.ToString()) == true).

You can't compare a string and an int directly, due to C#'s strong typing. Instead, you should "convert" the int to a string, using ToString.
edit: or just not call ToString on k?

Related

Kattis - Recount C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I'm trying to solve https://open.kattis.com/problems/recount in C#. I have my code written but I know I'm missing something but I'm stuck and I've been working on this for a few days. Whenever I try to run my code I receive a runtime error and I know it's because I need to be able to add a user inputted list to convert to a dictionary but I'm not sure how.
Running on .NET 3.1 for school
Here is the code I have currently
namespace Kattis_Solution___Recount
{
class Program
{
static void Main(string[] args)
{
SortedDictionary<string, int> votes = new SortedDictionary<string, int>();
List<string> names = new List<string>();
string name = "";
name = Console.ReadLine();
Console.Write(name);
Console.Write("\n");
int max = 0;
string winner = "";
while (name[0] != '*')
{
if (votes[name] == 0)
{
names.Add(name);
}
votes[name]++;
if (votes[name] > max)
{
max = votes[name];
winner = name;
}
name = Console.ReadLine();
Console.Write(name);
Console.Write("\n");
}
Console.Write(winner);
Console.Write("\n");
for (int i = 1; i < names.Count; i++)
{
if (votes[names[i]] == max && winner != names[i])
{
Console.Write("Runoff!");
Console.Write("\n");
break;
}
}
}
}
}
You cannot do this if the entry isnt there
if (votes[name] == 0) {
names.Add(name);
}
(unlike some other languages - c++ for example)
You need to do
if (!votes.ContainsKey(name)) {
names.Add(name);
votes[name] = 0;
}
See docs here https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.item?view=net-6.0
The value associated with the specified key. If the specified key is not found, a get operation throws a KeyNotFoundException, and a set operation creates a new element with the specified key.

Why I got error CS5001 (no static Main method found for entry point)? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am new with C# I got really stuck on this problem.
The task is to Write a C# program that implements bubble sort. The program gets the values as command-line arguments.
Here is my code:
namespace BubbleSort
{
class MySort
{
public void Main(int[] args)
{
int temp;
for (int j = 0; j <= args.Length - 2; j++)
{
for (int i = 0; i <= args.Length - 2; i++)
{
if (args[i] > args[i + 1])
{
temp = args[i + 1];
args[i + 1] = args[i];
args[i] = temp;
}
}
}
Console.WriteLine("Sorted:");
foreach (int p in args)
Console.Write(p + " ");
Console.Read();
}
}
}
And it always gives me error CS5001
Thank you in advance.
Hello for me the problem is the declaration of the main function. It should be:
public static void Main(string[] args)
You shoud cast your args in int: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/how-to-convert-a-string-to-a-number
Don't forget the static key word, without it the function need a target.

Why can't you take a variable and multiply it by itself? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I was trying to raise a number to a power using a for loop. However, when I wrote this and ran it, it gave me a random number.
using System;
class Program {
static void Main(string[] args) {
var a = 5;
for(int i=0; i<5; i++){
a*=a;
}
Console.WriteLine(a);
}
}
Why is it that this doesn't work? Is a copied only once and can't be used this way?
You can multiply by itself but you can not display result because you overflowing integer (exceed int32 size)(var by default in this case is int32).
Your results :
25
625
390625
out of range (exceeded size).
Answer for you question should be:
var initialValue = 2;
var exponent = 4;
var power = initialValue;
for (int i = 0; i < exponent ; i++)
{
initialValue *= power;
}
OR
var exponentiation = Math.Pow(2, 5);

Replace the item in collection with a new item [closed]

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 5 years ago.
Improve this question
I have a observablecollection Named A with properties id,age,name, Am storing the changed things in another collection B. Now I want to replace the same item in object A with that of changed things in B .How can I achieve it.
foreach(var item in A)
{
}
You can use the Zip operator
ObservableCollection<ObjType> obsCollectionA = new ObservableCollection<ObjType>();
ObservableCollection<ObjType> obsCollectionB = new ObservableCollection<ObjType>();
foreach (var pair in obsCollectionA.Zip(obsCollectionB, (a, b) => new { A = a, B = b }))
{
pair.A.Id = pair.B.Id;
pair.A.Name = pair.B.Name;
pair.A.Age = pair.B.Age;
}
Assuming from "yes, I need to replace the items in B with the same index of A"
for(int i = 0; i < A.Count; i++)
{
B[i] = A[i]; //or A[i] = B[i];
// You could compare by: if(A[i].ID == B[i].ID)
}
You can follow this way:
public static IList<T> Swap<T>(this IList<T> list, int indexA, int indexB)
{
if (indexB > -1 && indexB < list.Count)
{
T tmp = list[indexA];
list[indexA] = list[indexB];
list[indexB] = tmp;
}
return list;
}

Calculate factorial in C# using recursion [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I know how to calculate a factorial using a loop. Below is the code for loop, but I am getting an error while doing it by recursion. Below are both the code samples. How can I fix this?
namespace factorial
{
class Program
{
static void Main(string[] args)
{
int i, number, fact;
Console.WriteLine("Enter the Number");
number = int.Parse(Console.ReadLine());
fact = number;
for (i = number - 1; i >= 1; i--)
{
fact = fact * i;
}
Console.WriteLine("\nFactorial of Given Number is: "+fact);
Console.ReadLine();
}
}
}
Factorial using recursion:
Is there something as where I am going wrong? When am I calculating it using recursion?
Factorial using loop:
public double factorial_Recursion(int number)
{
if (number == 1)
return 1;
else
return number * factorial_recursion(number - 1);
}
public double factorial_WhileLoop(int number)
{
double result = 1;
while (number != 1)
{
result = result * number;
}
return result;
}
Your call name is not equal to your method name:
factorial_Recursion is the method name.
factorial_recursion is the call.
This worked for me:
namespace Testing
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(factorial_Recursion(5));
Console.WriteLine("press any Key");
Console.ReadLine();
}
public static double factorial_Recursion(int number)
{
if (number == 1)
return 1;
else
return number*factorial_Recursion(number - 1);
}

Categories

Resources