I want to make a program in C language which will take the user input and I would not be able to understand the logic of the loop.
for ( c = 2 ; c <= n - 1 ; c++ )
The program code is given below:-
#include<stdio.h>
#include<conio.h>
void main()
{
int n, c;
printf("Enter a number to check if it is prime\n");
scanf("%d", &n);
for ( c = 2 ; c <= n - 1 ; c++ )
{
if ( n % c == 0 )
{
printf("%d is not prime.\n", n);
break;
}
}
if ( c == n )
printf("%d is prime.\n", n);
getch();
}
I have used the for loop which will end up the statement of n - 1
in for loop. If I will give the input 11
then it will end up on 11 - 1 = 10
then how it will give up the logic of if(c == n) { printf("%d", n);
?