write c++ program to print right triangle pattern of prime numbers
Answers
Answer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<stdio.h>
int isPrimeNumber(int num);
int main() {
int i, j, rows;
int counter = 2;
printf("Enter the number of rows\n");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
/* Try to find next prime number by
incrementing counter and testing it for primality */
while(!isPrimeNumber(counter)){
counter++;
}
printf("%d ", counter);
counter++;
}
printf("\n");
}
return(0);
}
int isPrimeNumber(int num) {
int i, isPrime = 1;
for (i = 2; i <= (num/2); i++) {
if (num % i == 0){
isPrime = 0;
break;
}
}
if (isPrime==1 || num==2)
return 1;
else
return 0;
}
Output
Enter the number of rows
4
2
3 5
7 11 13
17 19 23 29
Answer:
int isPrimeNumber(int num);
int main() {
int i, j, rows;
int counter = 2;
printf("Enter the number of rows\n");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++) {
/* Try to find next prime number by
incrementing counter and testing it for primality */
while(!isPrimeNumber(counter)){
counter++;
}
printf("%d ", counter);
counter++;
}
printf("\n");
}
return(0);
}
int isPrimeNumber(int num) {
int i, isPrime = 1;
for (i = 2; i <= (num/2); i++) {
if (num % i == 0){
isPrime = 0;
break;
}
}
if (isPrime==1 || num==2)
return 1;
else
return 0;
}