what seems to be wrong with this C program. Please debug it for me. Copy-paste the new source code then provide a screenshot that yours is working here is the code: #include int main() { //prompting user for non negative integer printf("Enter a non-negative integer number: "); //reading number int n;//to store number scanf("%d",&n);//reading integer //now finding integer is prime or not //note: a number is prime it is divisible by 1 and the number itself int flag=1;//initially assuming n is prime, 1:prime, 0:not prime //now checking if any other number other than 1 and n, divides n, then it is not prime for(int i=2;i
what seems to be wrong with this C program. Please debug it for me. Copy-paste the new source code then provide a screenshot that yours is working
here is the code:
#include <stdio.h>
int main()
{
//prompting user for non negative integer
printf("Enter a non-negative integer number: ");
//reading number
int n;//to store number
scanf("%d",&n);//reading integer
//now finding integer is prime or not
//note: a number is prime it is divisible by 1 and the number itself
int flag=1;//initially assuming n is prime, 1:prime, 0:not prime
//now checking if any other number other than 1 and n, divides n, then it is not prime
for(int i=2;i<n;i++)//i is from i=2 to n-1
if(n%i==0)//if such i found, (i divides n)
{//then n is to prime, since we found i that divides n, where i is not 1 and n
flag=0;//setting flag to 0, indicates it is not prime
break;//breaking loop
}
//displaying output
if(flag==1)//flag 1 means n is prime
{
printf("%d is a prime number\n",n);
}
else{//flag=0 means n is not prime
printf("%d is a non-prime number\n",n);
}
return 0;
}
output:
Step by step
Solved in 3 steps with 1 images