Below is my code, and I can't get it to work the way it should.
I have to find prime numbers (this works fine). Then, if the prime numbers are 7 and 3
(63 = 7 * 3 * 3
or 7 = 7
) the number is magical, and if it contains any others (98 = 7 * 7 * 2
or 42 = 7 * 3 * 2
) it's not.
I'm kind of stuck here:
if (b != 7 && b != 3)
Console.WriteLine(k);
else
Console.WriteLine(j);
I don't know how to fix it. Here is the whole code:
string k="isnt magical";
string j = "is magical";
int a, b;
Console.WriteLine("Vnesite svoje stevilo: ");
a = Convert.ToInt32(Console.ReadLine());
for (b = 2; a > 1; b++)/
if (a % b == 0)
{
while (a % b == 0)
{
a /= b;
}
if (b != 7 && b != 3)
Console.WriteLine(k);
else
Console.WriteLine(j);
}
You are printing "isnt magical"
or "is magical"
for every factor.
Your code should look like this:
string k = "isnt magical";
string j = "is magical";
int a, b;
Console.WriteLine("Vnesite svoje stevilo: ");
a = Convert.ToInt32(Console.ReadLine());
var allMagical = true;
for(b = 2; a > 1; b++) if(a % b == 0)
{
while(a % b == 0) a /= b;
if(b != 7 && b != 3) allMagical = false;
}
Console.WriteLine(allMagical ? j : k);