C
C#2y ago
𝙔⁴⁴

❔ hw help

using System;

public class Program
{
public static int CountEven (int num)
{
int digit;
int evencount = 0;
while (num != 0)
{
digit = num % 10;
if (num % 2 == 0)
{
evencount++;
}
num = num/10;
}
return evencount;
}
public static void Main()
{
int n;
Console.WriteLine("Write how many numbers are there?");
n = int.Parse(Console.ReadLine());
for ( int i = 1 ; i <= n ; i++)
{
int c;int y = 0;int Max = 0;
c= int.Parse(Console.ReadLine());
Console.WriteLine( "This number has this much even numbers: " + CountEven(c));
if (CountEven(c) > y)
{
y = CountEven(c);
Max = Math.Max(y,Max);
Console.WriteLine("This number has the most even numbers:" + Max);
}
}
}
}
using System;

public class Program
{
public static int CountEven (int num)
{
int digit;
int evencount = 0;
while (num != 0)
{
digit = num % 10;
if (num % 2 == 0)
{
evencount++;
}
num = num/10;
}
return evencount;
}
public static void Main()
{
int n;
Console.WriteLine("Write how many numbers are there?");
n = int.Parse(Console.ReadLine());
for ( int i = 1 ; i <= n ; i++)
{
int c;int y = 0;int Max = 0;
c= int.Parse(Console.ReadLine());
Console.WriteLine( "This number has this much even numbers: " + CountEven(c));
if (CountEven(c) > y)
{
y = CountEven(c);
Max = Math.Max(y,Max);
Console.WriteLine("This number has the most even numbers:" + Max);
}
}
}
}
now i want this code to show me the number that has the most even numbers but it keeps showing me how much even numbers the numbers has instead even tho i have the max command
3 Replies
Brent
Brent2y ago
The issue with your code is that the Console.WriteLine("This number has the most even numbers:" + Max); statement is inside the for loop, so it will be executed multiple times. As a result, the last number that has the most even numbers will be printed multiple times. To fix this, you can move the Console.WriteLine("This number has the most even numbers:" + Max); statement outside of the for loop so that it is only executed once. Additionally, you should initialize the Max variable to the first number's even count instead of 0, so that the first number is always considered as a candidate for having the most even numbers. So you should actually just store the number that has the most even numbers in Max, and always check if the new number has more even numbers or not, if that's the case, overwrite it. Only in the end (after all of the numbers were checked for the amount of even numbers) the Max should be printed This should do it:
using System;
public class Program { public static int CountEven(int num) { int digit; int evencount = 0; while (num != 0) { digit = num % 10; if (digit % 2 == 0) // changed here { evencount++; } num = num/10; } return evencount; } public static void Main() { int n; Console.WriteLine("Write how many numbers are there?"); n = int.Parse(Console.ReadLine()); int Max = 0; // added here int MaxNum = 0; // added here for (int i = 1; i <= n; i++) { int c = int.Parse(Console.ReadLine()); int evenCount = CountEven(c); Console.WriteLine("This number has this much even numbers: " + evenCount); if (evenCount > Max) // changed here { Max = evenCount; MaxNum = c; // added here } } Console.WriteLine("The number with the most even numbers is: " + MaxNum + " with " + Max + " even numbers."); // added here } }
𝙔⁴⁴
𝙔⁴⁴2y ago
thank you so much
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.