Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

what should i add to this to see when it is not prime u have to find the closest

ID: 3698183 • Letter: W

Question

what should i add to this to see when it is not prime u have to find the closest prime number (higher or a lower number)

{
Console.WriteLine("Enter a number");
int i = Convert.ToInt32(Console.ReadLine());
int o = 0;
int x = 0;
int k = 2;
while (i != -1)
{
int w = 0;
for (k = 2; k < i; k++)
{
if (i % k == 0)
{
w++;
Console.WriteLine("{0} is divisible by {1}", i, k);

}
}
if (w == 0)
{
o++;
{
Console.WriteLine("Number is prime");
}
}

else
Console.WriteLine("Number is not prime");
Console.WriteLine("Enter a number -1 to quit");
i = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("You entered {0} primes, {1} non-primes", o, x);

}
}
}
}

Explanation / Answer

int main()
{
int [] a=new int[20];
int n;
int i,j,k=0;
int flag=0;
int countPrime=0;
int countNonPrime=0;
n = Convert.ToInt32(Console.ReadLine());
for(i=0;i<n;i++) //accepting the numbers in the array.
{
a[i] = Convert.ToInt32(Console.ReadLine());
}

for(i=0;i<n;i++) //iterating through the numbers in the array.
{
flag=0;
for(j=2;j<a[i];j++) //here is the loop for checking if the number is divisible by any other number.
{
if(a[i]%j==0)
{
flag=1; //setting flag true if number is divisible hence not prime
break;
}
}
if(flag!=1) //increase the count of the prime numbers
{
countPrime++;
console.writeline(" {0}",a[i]);
}
else //increase the count of the non prime numbers.
{
countNonPrime++;
}
}
console.writeline(" Total prime numbers are... {0}",countPrime);
console.writeline(" Total non prime numbers are {0}",countNonPrime);
return 0;
}