Please run and test the code please. Thank you very much. Write down a C program
ID: 3790953 • Letter: P
Question
Please run and test the code please. Thank you very much.
Write down a C program to generate a matrix (2-D Array) of integers in the range 1 to 100. The program should take the matrix dimensions (number of rows and number of columns) from the user. Print the matrix in such a way that only the prime numbers are printed. Print the character ‘*’ for the non-prime numbers. [Note that 1 is not a prime number].
Example: if the generated matrix is: 1 2 3 4 5 6 7 8 9 10 11 12
Print: * 2 3 * 5 * 7 * * * 11 *
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main() {
int row,col;
int i,j,k=1,c,flag,l,m;
int arr[100][100];
clrscr();
printf("Enter the No of Rows in matrix ");
scanf("%d",&row);
printf(" Enter the No of Columns in matrix ");
scanf("%d",&col);
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
arr[i][j] = k;
k++;
}
}
printf("generated matrix is :");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
printf(" %d",arr[i][j]);
}
}
printf(" Print:");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
m = arr[i][j];
flag = 0;
if( m == 1)
{
flag = 1;
}
for(l=2; l<=m/2; ++l)
{
// condition for nonprime number
if(m%l==0)
{
flag=1;
break;
}
}
if (flag==0)
{
printf("% d",m);
}
else
{
printf(" *");
}
}
}
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.