Here is a C program to print right triangle pattern of prime numbers. The C Program to Print Triangle Pattern of Prime Numbers is a challenging exercise that combines the concepts of prime numbers and patterns in programming. Here is a prime number pyramid or triangle pattern of four rows:
Required Knowledge
Algorithm to print right triangle pattern of prime numbers using for loop
This program is similar to right triangle star pattern. The only difference is instead of printing star characters we are printing consecutive prime numbers. I will recommend that, first check how to find a number is prime number or not
This program is similar to right triangle star pattern. The only difference is instead of printing star characters we are printing consecutive prime numbers. I will recommend that, first check how to find a number is prime number or not
- Take the number of rows(N) of right triangle as input from user using scanf function.
- Number of integers in Kth row is always K.
- We will use two for loops to print right triangle of prime numbers.
- Outer for loop will iterate N time. Each iteration of outer loop will print one row of the pattern.
- Inner loop will iterate K times. We will first find next prime number using "isPrimeNumber" function and print it. Each iteration of inner loop will print one prime number in current row.
Tips for Writing the Triangle Pattern of Prime Numbers Program in C
As you embark on the task of writing the Triangle Pattern of Prime Numbers Program in C, consider the following tips to help you successfully complete the program:
- Understanding the Problem : Before you start writing any code, make sure you understand the problem statement clearly. The program should print a triangle pattern where each row contains prime numbers, and the number of prime numbers in each row increases from top to bottom.
- Generating Prime Numbers : To generate prime numbers, you'll need to write a function that checks if a given number is prime. Use this function to generate prime numbers for each row of the triangle pattern.
- Using Nested Loops : Use nested loops to control the number of rows and columns printed in each iteration. The outer loop will control the rows, and the inner loop will print the prime numbers for each row.
- Optimizing Prime Number Generation : To optimize the program, consider using a more efficient algorithm to generate prime numbers, such as the Sieve of Eratosthenes. This will improve the performance of your program, especially for large patterns.
- Formatting the Output : Format the output of the program to ensure that the prime numbers are printed in the correct positions to form the triangle pattern. Use spaces or tabs to align the numbers properly.
- Testing Incrementally : Start with a small pattern and gradually increase its size to test the program. This incremental testing approach will help you identify and fix errors early on, ensuring that your program behaves as expected.
C program to print right triangle pattern of prime numbers
#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
Related Topics