#include<stdio.h>
#include<conio.h>
#include<string.h>
//typedef int BOOL;
//#define TRUE 1
//#define FALSE 0

int a[3][4];
int Prime(int *x);
void input();
void printOut();
void checkPrime();

int Prime(int *x)
{
        int i;
        for( i=2; i< (int)sqrt(*x); i++ )
        {
           if( *x%i == 0 )
           return 0;
        }
        return 1;   
}


void input()
{   int i,j;
    printf("Enter element for matrix: \n");
    for(i=0; i<3; i++)
        for(j=0; j<4; j++)
        {
                 printf("element a[%d][%d]: ",i,j);
                 scanf("%d",&a[i][j]);
        }
} 


void printOut()  
{
   int i,j;
   for(i=0; i<3; i++)
   {
      printf("\n");
      for(j=0; j<4; j++)
         printf(" %d ",a[i][j]);
   }  
}   

void checkPrime()
{
     int i,j;
     printf("\nMatrix after check Prime\n");
     for(i=0; i<3; i++)
     { 
     printf("\n");
     for(j=0; j<4; j++)
     {
       if( Prime(&a[i][j]) )
       printf(" %d(pr) ",a[i][j]);
       else 
       printf(" %d ",a[i][j]);
      }
     }
}

int main()
{
    input();
    printOut();   
    checkPrime();
    getch();
    return 0;
}

My source code does not work properly everytime.
I have not found where the mistake was
Please help me
Thanks a lot!

Dani AI

Generated

The bug is almost certainly the prime-test loop bound and a confusing return convention. In the first post the loop stops at i < (int)sqrt(...), so divisors equal to sqrt(n) are never checked (examples: 4, 9, 25 will slip through). Using sqrt() also brings in floating-point rounding and the need for <math.h> and -lm. Passing matrix elements by pointer is unnecessary and makes the code harder to read. In post #2 moved to a value parameter — good — but returned 0 for prime and 1 for composite, which works if you remember to invert the test everywhere, but is easy to misread. Prefer an is_prime function that returns 1 for prime and 0 otherwise.

A simple, robust implementation that avoids sqrt() and floating point, handles small and negative inputs, and skips even divisors:

#include <stdio.h>

/* return 1 if n is prime, 0 otherwise */
int is_prime(int n)
{
    if (n < 2) return 0;
    if (n == 2) return 1;
    if (n % 2 == 0) return 0;
    for (int i = 3; (long long)i * i <= n; i += 2)
        if (n % i == 0) return 0;
    return 1;
}

/* print a rows x cols matrix, appending (pr) to primes */
void print_with_pr(int rows, int cols, int mat[rows][cols])
{
    for (int r = 0; r < rows; ++r) {
        for (int c = 0; c < cols; ++c) {
            int v = mat[r][c];
            printf("%4d", v);
            if (is_prime(v)) printf("(pr)");
            putchar(' ');
        }
        putchar('\n');
    }
}

Notes: compile with a standards-compliant compiler (e.g. gcc -std=c11 -Wall). Avoid nonstandard headers like <conio.h> and getch() if portability matters. Test with edge cases (0, 1, 2, 4, 9, negatives and some large primes) to confirm behavior. If you need to handle very large values, switch to 64-bit types and consider probabilistic tests (Miller–Rabin).

int Prime(int x)
{      
       int i;
        if (x==2) 
           return 0;
           else if (x < 2) 
                    return 1;
                 else
                 {
                    for( i=2; i<= (int)sqrt(x); i++ )
                    {
                       if( x % i == 0 )
                       return 1;
                    }
                    return 0; 
                 }  
}
void checkPrime()
{
     int i,j,k;
     printf("\nMatrix after check Prime\n");
     for(i=0; i<3; i++)
     { 
     printf("\n");
     for(j=0; j<4; j++)
     {
       printf(" %d",a[i][j]);
       if(! Prime(a[i][j]) )
       printf("(pr)",a[i][j]); 
     }
     }
}

Here is my improvement!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.