In fact my teacher has passed the program that calculates the prime numbers from 1 to N. But I did not understand some things in the code and would like to help.
In line 18 we have the following: for(j=2; j<=i/2; j++), because j was divided by 2? and why j start in 2? should not start i and j on 1?
#include <stdio.h>
int main() {
int i, j, n, isPrime; //isPrime is used as flag variable
/* Reads upper limit to print prime */
printf("Find prime numbers between 1 to : ");
scanf("%d", &n);
printf("\nAll prime numbers between 1 to %d are:\n", n);
/* Finds all Prime numbers between 1 to n */
for(i=2; i<=n; i++) {
/* Assume that the current number is Prime */
isPrime = 1;
/* Check if the current number i is prime or not */
for(j=2; j<=i/2; j++) {
/*
* If i is divisible by any number other than 1 and self
* then it is not prime number
*/
if(i%j==0) {
isPrime = 0;
break;
}
}
/* If the number is prime then print */
if(isPrime==1) {
printf("%d is Prime number\n", i);
}
}
return 0;
}
should not start i ... on 1?
Yes, i should at 1. Consider that the current code will evolve. Later code may look like the below. Notice the comment and code matches your contract "numbers between 1 and N". No implied short-cut starting at 2, but clarity that code is properly testing all numbers [1...N].
/* Finds all Prime numbers between 1 to n */
for(i=1; i<=n; i++) {
if (IsPrime(i)) {
printf("%d is Prime number\n", i);
}
}
The point is that as a programmer, you are given the task "Finding prime number between 1 and N". A common sub-task is to code bool IsPrime(int i). Certainly if you code everything together, i can start at 2 - we know 1 is not a prime. Yet good programming begins with software architecture and that involves divide and conquer. So making a stand-alone helper function that works for all input is a good first step.
should not start j ... on 1?
No. Now code is into its find-a-prime algorithm and starting j=1 would fail.
bool IsPrime(int i) {
if (i <= 1) return false; // Cope with negative, 0, and 1.
for (int j=2; j<=i/2; j++) {
if (i%j==0) {
return false;
}
}
return true;
}
Of course, we can optimize IsPrime() in many ways.
The first prime number is 2 -- which is why you start as 2 -- everything is divisible by 1, and if you start there your algo will fail.
You end with N/2 because testing larger number will not result in anything that you would not have found, simply because to be a non-prime means that you will have to have to have at least 2 factors, and the smallest prime is 2 and 2*(i/2) >= i -- but in reality it is better and safe to stop at square root of N (but maybe that is in the next lesson).
This starting at 2 and increment the loop by one is wasteful -- since all primes except 2 is odd it would be better to start at 3 and increment by 2, and just make a special test for dividing by 2 outside the loop.
The first prime number is known to be 2, so there is no reason to start with 1. Given number i, there is no reason to try to divide it by numbers greater than its half, as the result will always be less than 2 and yield a quotient of 0 and a non-zero remainder (unless we divide it by itself, which is pointless in the prime test).
This is the easiest way:
#include <iostream>
using namespace std;
int main (){
int first,second;
cout<<"Enter starting of limit";
cin>>first; //First value
cout<<"Enter Ending of limit";
cin>>second; //Second value
L:
if(first < second){ //Checking starts here
if(first % 2 != 0 && first % 5 != 0 || first - 5 == 0 && first % 7 != 0){
cout<<first<<endl;
first++ ;
goto L; //Iteration
}
}
}
#include<stdio.h>
int main()
{
int num,i,count,n;
printf("Enter max range:");
scanf("%d",&n);
for(num=1; num<=n; num++)
{
count=0;
for(i=2; i<=num/2; i++)
{
if(num%i==0)
{
count++;
break;
}
}
if(count==0 && num!=1)
printf("%d ",num);
}
return 0;
}
Related
#include<stdio.h>
int main(){
int i, n, prime=0;
printf("Enter any positive integer:\n");
scanf("%d", n);
for(i=2; i<=n; i++){
if(n%i==0){
prime=1;
break;
}
}
if(n==1){
printf("1 is neither prime not composite.");
}
else{
if(prime==0){
printf("%d is a prime number.", n);
}
else{
printf("%d is not a prime number.", n);
}
}
return 0;
}
I don't know what the problem here is but it never works.
It only takes in the input but then after that just shows a blank line and then nothing happens. I also compared it with another code for the same problem online and it looked almost looked similar and I couldn't find the problem here.
Enable all compiler warnings to save time.
"%d" in scanf() expects a matching int *, not an int.
// scanf("%d", n);
scanf("%d", &n);
Code iterates too far, should iterate less than n
// for(i=2; i<=n; i++){
for(i=2; i<n; i++){
// or even better and faster
for(i=2; i<=n/i; i++){
Test Inverted?
When n%i==0 is true, it imples n is not a prime.
I tried running your program but instead of blank output, I got a Segmentation fault:
Enter any positive integer:
12
Segmentation fault (core dumped)
You need to pass address of variable rather than actual variable to scanf method, see examples here. So scanf statement which is like this:
scanf("%d", n);
becomes this:
scanf("%d", &n);
I tested and your program started giving output as expected:
c-posts : $ ./a.out
Enter any positive integer:
14
14 is not a prime number.
Edit:
From #Jabberwocky's comment I noticed that your prime number calculation logic seems doesn't seem to be working okay. You're currently iterating from [2..n] . Note that n%(i=n) would always be zero. You need to exclude n from loop range like this:
for(i=2; i < n; i++){
// ...
}
Or you can also iterate till sqrt(n) which would have less iterations:
for(i=2; i< sqrt(n); i++){
// ...
}
make it:
scanf("%d", &n);
For starters the argument of the call of scanf is incorrect.
scanf("%d", n);
You have to write
scanf("%d", &n);
Also there is no great sense to use a signed integer number to check whether it is a prime number. Use an unsigned integer type as for example unsigned int.
And any prime number is divisible by itself. So due to the condition in the for loop
for(i=2; i<=n; i++){
the variable prime will be always set to 1 because when i is equal to n then n % i is equal to 0.
Change the for loop the following way
for( i = 2; prime == 0 && i <= n / i; i++ )
{
if ( n % i == 0 ) prime = 1;
}
However if the number is an even number and is not equal to 2 then it is clear that it is not a prime number. You can check this before the for loop to skip it for even numbers.
Pay attention to that the user can enter 0. In this case your program will output that 0 is a prime number though it is not a prime number.
The program can look the following way
#include <stdio.h>
int main(void)
{
unsigned int n = 0;
printf( "Enter any positive integer: " );
scanf( "%u", &n);
int prime = n % 2 == 0 ? n == 2 : n != 1;
for ( unsigned int i = 2; prime && i <= n / i; i++ )
{
if ( n % i == 0 ) prime = 0;
}
if ( n < 2 )
{
printf( "%u is neither prime not composite.\n", n );
}
else if ( prime )
{
printf( "%u is a prime number.\n", n );
}
else
{
printf( "%u is not a prime number.\n", n );
}
return 0;
}
I am learning C on my own with a book and I cannot for the life of me figure out how to solve this exercise. I'm obviously looking at it in the wrong way or something. Here is an explanation below.
Listed below are some functions and the main function at the bottom. This program is compiled to generate a certain number of random numbers and determine the min and the max of the random numbers. If you copy and paste this code, you will see how it works. Anyways, an exercise asks me to go to the function "prn_random_numbers()" and change the for loop from "for (i = 1; i < k; ++i)" to for (i = 2; i <= k; ++i). This causes the first line format to print incorrectly. The exercise is to further modify the program in the body of the for loop to get the output to be formatted correctly.
To sum it up, the "prn_random_numbers()" function is written to print out 5 random numbers before moving to the next line. Hence the" i % 5" if statement. Now, for some reason, when you make the slight adjustment to the for loop, as the exercise asks above, it causes the first line to only print 4 numbers before moving to the next line. I have tried a number of things, including trying to force it to print the 5th number, but it only duplicated one of the random numbers. I even tried "i % 4" to see if it would print 4 numbers for each row, but it only prints 3 numbers for the first row instead of 4! So it always prints one less number on the first line than it is supposed to. I have n clue why it is doing that and the book does not give an exercise. Do you have any idea?
Bear with me if you think this is a stupid question. I am just learning on my own and I want to make sure I have a good foundation and understand everything as I learn it, before moving forward. I appreciate any help or advice!
prn_random_numbers(k) /* print k random numbers */
int k;
{
int i, r, smallest, biggest;
r = smallest = biggest = rand();
printf("\n%12d", r);
for (i = 1; i < k; ++i)
{
if (i % 5 == 0)
printf("\n");
r = rand();
smallest = min(r, smallest);
biggest = max(r, biggest);
printf("%12d", r);
}
printf("\n\n%d random numbers printed.\n", k);
printf("Minimum:%12d\nMaximum:%12d\n", smallest, biggest);
}
int main()
{
int n;
printf("Some random numbers are to be printed.\n");
printf("How many would you like to see? ");
scanf("%d", &n);
while (n < 1)
{
printf("ERROR! Please enter a positive integer.\n");
printf("How many would you like to see? ");
scanf("%d", &n);
}
prn_random_numbers(n);
return (EXIT_SUCCESS);
}
the following proposed code:
properly initializes the random number generator
cleanly compiles
properly checks for and handles errors
performs the desired functionality
avoids having to list instructions twice
follows the axiom: Only one statement per line and (at most) one variable declaration per statement.
does not use undefined functions like: max() and min()
and now the proposed code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void prn_random_numbers(int k)
{
int count = 1;
int r;
int smallest;
int biggest;
r = smallest = biggest = rand();
printf("\n%12d", r);
for ( int i = 2; i <= k; i++, count++)
{
if (count % 5 == 0)
{
count = 0;
printf("\n");
}
r = rand();
smallest = (r < smallest)? r : smallest;
biggest = (r > biggest)? r : biggest;
printf("%12d", r);
}
printf("\n\n%d random numbers printed.\n", k);
printf("Minimum:%12d\nMaximum:%12d\n", smallest, biggest);
}
int main( void )
{
int n;
srand( (unsigned)time( NULL ) );
do
{
printf("Please enter a positive integer, greater than 0.\n");
printf("How many would you like to see? ");
if( scanf("%d", &n) != 1 )
{
fprintf( stderr, "scanf for number of random numbers failed\n" );
exit( EXIT_FAILURE );
}
} while( n < 1 );
prn_random_numbers(n);
// in modern C, if the returned value from `main()` is 0 then no `return 0;` statement needed
}
a typical run, no input problems is:
Please enter a positive integer, greater than 0.
How many would you like to see? 20
98697066 2110217332 1247184349 421403769 1643589269
1440322693 985220171 1915371488 1920726601 1637143133
2070012356 541419813 1708523311 1237437366 1058236022
926434075 1422865093 2113527574 626328197 1618571881
20 random numbers printed.
Minimum: 98697066
Maximum: 2113527574
Try to use a debugger to solve your problem, it's easy to use and really helpfull :)
SOLUTION:
Your i variable don't count the number of numbers because it is initialize at 1 (in the for statement), so you need to declare a new variable to count properly.
If you have still a problem:
void prn_random_numbers(int k)
{
int count = 1;
int i, r, smallest, biggest;
r = smallest = biggest = rand();
printf("\n%12d", r);
for (i = 2; i <= k; i++, count++) {
if (count % 5 == 0) {
count = 0;
printf("\n");
}
r = rand();
smallest = min(r, smallest);
biggest = max(r, biggest);
printf("%12d", r);
}
printf("\n\n%d random numbers printed.\n", k);
printf("Minimum:%12d\nMaximum:%12d\n", smallest, biggest);
}
I am solving this problem:
Peter wants to generate some prime numbers for his cryptosystem. Help
him! Your task is to generate all prime numbers between two given
numbers!
Input
The input begins with the number t of test cases in a single line
(t<=10). In each of the next t lines there are two numbers m and n (1
<= m <= n <= 1000000000, n-m<=100000) separated by a space.
Output
For every test case print all prime numbers p such that m <= p <= n,
one number per line, test cases separated by an empty line.
Example
Input:
2
1 10
3 5
Output:
2
3
5
7
3
5
I am getting memory location values after I get prime numbers in the output.
Can you explain about how I can terminate after getting the final output.
Here is my code:
#include <stdio.h>
int main()
{
int t, i, m[10], n[10], j, k, l, isPrime;
// t is test case, m[] and n[] are the lower and upper value of the range of prime numbers
// isPrime is to check the condition True or False.
j = 0;
scanf(" %d \n", &t);
for(i=0; i<t; i++)
{
scanf("%d%d",&m[i],&n[i]);
}
while(j<=i)
{
for(k = m[j]; k<= n[j]; k++)
{ isPrime = 0;
for(l = 2; l<= (k/2); l++){
if(k%l == 0)
{
isPrime = 1;
break;
}
}
if(isPrime==0 && n[j]!= 1)
printf(" %d \n", k);
}
j++;
}
getch();
return 0;
}
You are using getch() which will stop the program until you press any key right after the execution of all your program. remove getch(), compile and run the .exe from the source folder file after closing the IDE.
Well, I executed the code, without getch() and it worked fine!
Even though this question has been asked a million times I just haven't found an answer that actually helps my case, or I simply can't see the solution.
I've been given the task to make a program that takes in a whole number and counts how many times each digit appears in it and also not showing the same information twice. Since we're working with arrays currently I had to do it with arrays of course so since my code is messy due to my lack of knowledge in C I'll try to explain my thought process along with giving you the code.
After entering a number, I took each digit by dividing the number by 10 and putting those digits into an array, then (since the array is reversed) I reversed the reverse array to get it to look nicer (even though it isn't required). After that, I have a bunch of disgusting for loops in which I try to loop through the whole array while comparing the first element to all the elements again, so for each element of the array, I compare it to each element of the array again. I also add the checked element to a new array after each check so I can primarily check if the element has been compared before so I don't have to do the whole thing again but that's where my problem is. I've tried a ton of manipulations with continue or goto but I just can't find the solution. So I just used **EDIT: return 0 ** to see if my idea was good in the first place and to me it seems that it is , I just lack the knowledge to go back to the top of the for loop. Help me please?
// With return 0 the program stops completely after trying to check the digit 1 since it's been checked already. I want it to continue checking the other ones but with many versions of putting continue, it just didn't do the job. //
/// Tried to make the code look better. ///
#include <stdio.h>
#define MAX 100
int main()
{
int a[MAX];
int b[MAX];
int c[MAX];
int n;
int i;
int j;
int k;
int counter1;
int counter2;
printf("Enter a whole number: ");
scanf("%i",&n);
while (1)
{
for (i=0,counter1=0;n>10;i++)
{
a[i] = n%10;
n=n/10;
counter1+=1;
if (n<10)
a[counter1] = n;
}
break;
}
printf("\nNumber o elements in the array: %i", counter1);
printf("\nElements of the array a:");
for (i=0;i<=counter1;i++)
{
printf("%i ",a[i]);
}
printf("\nElements of the array b:");
for (i=counter1,j=0;i>=0;i--,j++)
{
b[j] = a[i];
}
for (i=0;i<=counter1;i++)
{
printf("%i ",b[i]);
}
for (i=0;i<=counter1;i++)
{
for(k=0;k<=counter1;k++)
{
if(b[i]==c[k])
{
return 0;
}
}
for(j=0,counter2=0; j<=counter1;j++)
{
if (b[j] == b[i])
{
counter2+=1;
}
}
printf("\nThe number %i appears %i time(s)", b[i], counter2);
c[i]=b[i];
}
}
The task at hand is very straightforward and certainly doesn't need convoluted constructions, let alone goto.
Your idea to place the digits in an array is good, but you increment counter too early. (Remember that arrays in C start with index 0.) So let's fix that:
int n = 1144526; // example number, assumed to be positive
int digits[12]; // array of digits
int ndigit = 0;
while (n) {
digits[ndigit++] = n % 10;
n /= 10;
}
(The ++ after ndigit will increment ndigit after using its value. Using it as array index inside square brackets is very common in C.)
We just want to count the digits, so reversing the array really isn't necessary. Now we want to count all digits. We could do that by counting all digits when we see then for the first time, e.g. in 337223, count all 3s first, then all 7s and then all 2s, but that will get complicated quickly. It's much easier to count all 10 digits:
int i, d;
for (d = 0; d < 10; d++) {
int count = 0;
for (i = 0; i < ndigit; i++) {
if (digit[i] == d) count++;
}
if (count) printf("%d occurs %d times.\n", d, count);
}
The outer loop goes over all ten digits. The inner loop counts all occurrences of d in the digit array. If the count is positive, write it out.
If you think about it, you can do better. The digits can only have values from 0 to 9. We can keep an array of counts for each digit and pass the digit array once, counting the digits as you go:
int count[10] = {0};
for (i = 0; i < ndigit; i++) {
count[digit[i]]++;
}
for (i = 0; i < 10; i++) {
if (count[i]) printf("%d occurs %d times.\n", i, count[i]);
}
(Remember that = {0} sets the first element of count explicitly to zero and the rest of the elements implicitly, so that you start off with an array of ten zeroes.)
If you think about it, you don't even need the array digit; you can count the digits right away:
int count[10] = {0};
while (n) {
count[n % 10]++;
n /= 10;
}
for (i = 0; i < 10; i++) {
if (count[i]) printf("%d occurs %d times.\n", i, count[i]);
}
Lastly, a word of advice: If you find yourself reaching for exceptional tools to rescue complicated code for a simple task, take a step back and try to simplify the problem. I have the impression that you have added more complicated you even you don't really understand instead.
For example, your method to count the digits is very confused. For example, what is the array c for? You read from it before writing sensible values to it. Try to implement a very simple solution, don't try to be clever at first and go for a simple solution. Even if that's not what you as a human would do, remeber that computers are good at carrying out stupid tasks fast.
I think what you need is a "continue" instead of a return 0.
for (i=0;i<=counter1;i++) {
for(k=0;k<=counter1;k++) {
if(b[i]==c[k]) {
continue; /* formerly return 0; */
}
for(j=0,counter2=0; j<=counter1;j++)
if (b[j] == b[i]){
counter2+=1;
}
}
Please try and see if this program can help you.
#include <stdio.h>
int main() {
unsigned n;
int arr[30];
printf("Enter a whole number: ");
scanf("%i", &n);
int f = 0;
while(n)
{
int b = n % 10;
arr[f] = b;
n /= 10;
++f;
}
for(int i=0;i<f;i++){
int count=1;
for(int j=i+1;j<=f-1;j++){
if(arr[i]==arr[j] && arr[i]!='\0'){
count++;
arr[j]='\0';
}
}
if(arr[i]!='\0'){
printf("%d is %d times.\n",arr[i],count);
}
}
}
Test
Enter a whole number: 12234445
5 is 1 times.
4 is 3 times.
3 is 1 times.
2 is 2 times.
1 is 1 times.
Here is another offering that uses only one loop to analyse the input. I made other changes which are commented.
#include <stdio.h>
int main(void)
{
int count[10] = { 0 };
int n;
int digit;
int elems = 0;
int diff = 0;
printf("Enter a whole number: ");
if(scanf("%d", &n) != 1 || n < 0) { // used %d, %i can accept octal input
puts("Please enter a positive number"); // always check result of scanf
return 1;
}
do {
elems++; // number of digits entered
digit = n % 10;
if(count[digit] == 0) { // number of different digits
diff++;
}
count[digit]++; // count occurrence of each
n /= 10;
} while(n); // do-while ensures a lone 0 works
printf("Number of digits entered: %d\n", elems);
printf("Number of different digits: %d\n", diff);
printf("Occurrence:\n");
for(n = 0; n < 10; n++) {
if(count[n]) {
printf(" %d of %d\n", count[n], n);
}
}
return 0;
}
Program session:
Enter a whole number: 82773712
Number of digits entered: 8
Number of different digits: 5
Occurrence:
1 of 1
2 of 2
1 of 3
3 of 7
1 of 8
I used the following code and it showed numbers like 49 as prime and not composite. I am new to programming so please help me with the correct code.
#include <stdio.h>
int main()
{
int n;
int i;
scanf ("%d", &n);
for (i=2; i<n; i++)
{
if (n%i==0)
{
printf ("number is composite");
}
else
{
i=i+1;
}
}
printf("number is prime");
return 0;
}
You didn't stop the loop when you discovered that the number was composite, so composite numbers get both messages.
When i is not a factor of n you are adding 1 to i but then the for loop adds 1 again. This means that for every number that is not a factor of n you skip a number.
The printf("number is prime"); is not surrounded by any kind of if, so it will always be printed regardless of whether or not the number is actually prime. Unlike humans, computers won't think twice about printing conflicting information because computers are incapable of interpreting the actions we make them do.
You can optimize your code by checking less numbers. Only check up to the square root of the number.
This code is untested but should work. Note that this only eliminates the checking of factors above the square root, but not any multiples of previously checked factors (e.g. written like this, the program will check if the number is divisible by 2 but also by 4, 6, 8, 10, etc).
#include <stdio.h>
#include <math.h>
int main()
{
int n;
int i;
int isComposite = 0;
scanf ("%d", &n);
for (i = 2; i <= (int)sqrt((double)n); i++){
if (n % i == 0){
printf ("number is composite");
isComposite = 1;
break;
}
}
if (!isComposite){
printf("number is prime");
}
return 0;
}
You can modify your loop as follows -
if(n%2==0 && n!=2){ //if 2's multiple is entered no need of loop just check this
printf("number is composite");
return 2; // for sake I just returned 2
}
for (i=3; i<n;i=i+2)
{
if (n%i==0) // if this is true
{
printf ("number is composite"); // print this
return 1; // return from function no further iterations
}
}
In this way you stop loop as if condition is true and its code in its block us executed .
You should remove i=i+1 line, you increment i already in for (i=2; i<n; i++)
After that, you must put your conditions after the loop for prevent print result at every check.
#include <stdio.h>
int main()
{
int n;
int i;
scanf ("%d", &n);
for (i = 2; i<n; ++i)
{
if (n%i==0)
{
printf ("number is composite, divisible by %d\n", i);
break;
}
printf("i=%d\n", i);
}
if (n%i != 0)
printf("number is prime\n");
return 0;
}
Combining all suggestions and comments gives:
int q= (int) sqrt(n);
if (n%2==0)
{
printf ("number is composite"); // print this
return 1; // return from function no further iterations
}
for (i=3; i<=q; i += 2)
{
if (n%i==0) // if this is true
{
printf ("number is composite"); // print this
return 1; // return from function no further iterations
}
}
the reason you are getting 49 a s composite is because of this line:
else
{
i=i+1;
}
this causes you to skip odd numbers in your check, and 49 is 7*7, therefore the ouput yo get is that it is prime.
Another problem you have is that you do not stop the loop when you get a composite number, and at the end of it you will invoke this line of code regardless of what happens in the loop.
printf("number is prime");
here is a better practice:
create a function that will return 1 if it is prime, and 0 if it is not.
if you realize that it is a composite number, you should return immediately. if you get to the end of the loop, it is definatley a prime number.
also, you can run your loop to the square root of n to gain a smaller amount of iterations, and not until n.
good luck
int isPrime(int n)
{
int i;
for (i = 2; i < (int) sqrt(n) ; i++)
{
if (n % i == 0)
{
printf("number is composite");
return 0;
}
}
printf("number is prime");
return 1;
}
Here you are.
#include <stdio.h>
int main( void )
{
while ( 1 )
{
unsigned int n = 0;
_Bool prime;
printf( "\nEnter a non-negative number (0-exit): " );
scanf( "%u", &n );
if ( !n ) break;
prime = n == 2 || ( n % 2 && n != 1 );
for ( unsigned i = 3; prime && i * i <= n; i += 2 )
{
prime = n % i;
}
printf( "Number %u is %s\n", n, prime ? "prime" : "composite" );
}
return 0;
}
If to enter sequentially
1 2 3 4 5 6 7 8 9 0
then the output is
Enter a non-negative number (0-exit): 1
Number 1 is composite
Enter a non-negative number (0-exit): 2
Number 2 is prime
Enter a non-negative number (0-exit): 3
Number 3 is prime
Enter a non-negative number (0-exit): 4
Number 4 is composite
Enter a non-negative number (0-exit): 5
Number 5 is prime
Enter a non-negative number (0-exit): 6
Number 6 is composite
Enter a non-negative number (0-exit): 7
Number 7 is prime
Enter a non-negative number (0-exit): 8
Number 8 is composite
Enter a non-negative number (0-exit): 9
Number 9 is composite
Enter a non-negative number (0-exit): 0
Another approach to find whether a number is prime or not can be :-`
#include<stdio.h>
void main()
{
int no,i;
char x='y';
printf("Enter a number : ");
scanf("%d",&no);
for (i=2;i<=no/i;i++)
{
if(no%i==0)
{
x='n';
break;
}
}
if(x=='y')
printf("The number is a prime number. ");
else
printf("The number is not prime number. ");
}
The logic here is to limit the number of test cases to (the quotient of the number being tested and the value of the loop counter) since a divisor cannot exceed this quotient.