☞ Write a program to print following pattern (Jan,2020)
Sample Input1
3
Sample Output1
* 2 3
* 5 *
7 * *
Sample Input2
4
Sample Output2
* 2 3 *
5 * 7 *
* * 11 *
13 * * *
int main() {
int num, i, j, k, prime=1,c;
scanf("%d",&num);
for(i=1;i<=num;i++){
for(j=1;j<=num;j++){
c=0;
for(k=1;k<=prime/2;k++){
if(prime%k==0)
c++;
}
if(c==1)
printf("%d",prime);
else
printf("*");
if(j<num)
printf(" ");
prime++;
}
if(i<num)
printf("\n");
}
}
public class PrimePattern {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int num, prime=1;
num = in.nextInt();
for(int i=1; i<=num; i++){
for(int j=1; j<=num; j++){
int c=0;
for(int k=1; k<=prime/2; k++){
if(prime%k==0)
c++;
}
if(c==1){
System.out.print(prime);
}
else{
System.out.print("*");
}
if(j<num)
System.out.print(" ");
prime++;
}
if(i<num)
System.out.println();
}
}
}
Sample Input1
3
Sample Output1
* 2 3
* 5 *
7 * *
Sample Input2
4
Sample Output2
* 2 3 *
5 * 7 *
* * 11 *
13 * * *
SOLUTION IN C
#include<stdio.h>int main() {
int num, i, j, k, prime=1,c;
scanf("%d",&num);
for(i=1;i<=num;i++){
for(j=1;j<=num;j++){
c=0;
for(k=1;k<=prime/2;k++){
if(prime%k==0)
c++;
}
if(c==1)
printf("%d",prime);
else
printf("*");
if(j<num)
printf(" ");
prime++;
}
if(i<num)
printf("\n");
}
}
SOLUTION IN JAVA
import java.util.*;public class PrimePattern {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int num, prime=1;
num = in.nextInt();
for(int i=1; i<=num; i++){
for(int j=1; j<=num; j++){
int c=0;
for(int k=1; k<=prime/2; k++){
if(prime%k==0)
c++;
}
if(c==1){
System.out.print(prime);
}
else{
System.out.print("*");
}
if(j<num)
System.out.print(" ");
prime++;
}
if(i<num)
System.out.println();
}
}
}