Skip to main content

What is the program to find that the given number is prime number or not in C language?

 A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In C++, we can write a program to check whether a given number is prime or not.

The program to find if a given number is prime or not in C++ can be written in several ways. Here, we will discuss two different approaches to write this program.

Approach 1: Using a for loop

In this approach, we will use a for loop to check if the given number is divisible by any number between 2 and n/2. If the number is divisible by any number in this range, then it is not a prime number. Otherwise, it is a prime number.

Here is the code for the same:

#include <iostream> using namespace std; int main() { int n, i; bool isPrime = true; cout << "Enter a positive integer: "; cin >> n; for (i = 2; i <= n / 2; ++i) { if (n % i == 0) { isPrime = false; break; } } if (isPrime) cout << n << " is a prime number"; else cout << n << " is not a prime number"; return 0; }

Here, we have used a variable isPrime to keep track of whether the number is prime or not. We have initialized it to true at the beginning of the program. Then, we have used a for loop to iterate from 2 to n/2. Inside the loop, we have checked if the given number is divisible by the current number. If it is, then we have set isPrime to false and exited the loop using the break statement.

After the loop, we have checked the value of isPrime. If it is true, then we have printed that the given number is a prime number. Otherwise, we have printed that it is not a prime number.

Approach 2: Using a while loop

In this approach, we will use a while loop to check if the given number is divisible by any number between 2 and n/2. If the number is divisible by any number in this range, then it is not a prime number. Otherwise, it is a prime number.

Here is the code for the same:

#include <iostream> using namespace std; int main() { int n, i = 2; bool isPrime = true; cout << "Enter a positive integer: "; cin >> n; while (i <= n / 2) { if (n % i == 0) { isPrime = false; break; } ++i; } if (isPrime) cout << n << " is a prime number"; else cout << n << " is not a prime number"; return 0; }

Here, we have initialized a variable i to 2 and used a while loop to iterate while i is less than or equal to n/2. Inside the loop, we have checked if the given number is divisible by the current number. If it is, then we have set isPrime to false and exited the loop using the break statement.

After the loop, we have checked the value of isPrime. If it is true, then we have printed that the given number is a prime number. Otherwise, we have printed that it is not a prime number.

Comments

Popular posts from this blog

Gold Price Today: Unraveling the Surge, Analysing the Trends, and What Lies Ahead for Investors in India

  In the intricate tapestry of Indian culture and economy, few threads are woven as tightly as gold. More than just a precious metal, gold is an emotion, a tradition, a safe haven, and a cornerstone of financial security for millions of households. From elaborate weddings to auspicious festivals like Akshaya Tritiya and Diwali, gold is ubiquitous. Yet, beyond its cultural brilliance lies a complex financial asset that moves in response to a global ballet of economic indicators. As we stand today, the price of gold continues to capture headlines, hovering at levels that have both excited long-term investors and alarmed prospective buyers. In this comprehensive analysis, we decode " Gold Price Today ," exploring the factors driving the current rally, the historic context of its trajectory, and the essential considerations for those looking to invest in this timeless asset. Today’s Gold Price: A Quick Snapshot (Note: Prices of precious metals are highly volatile and change multi...

A Galaxy of Misfits: Why We Fell in Love with the Guardians of the Galaxy

  If you rewind the clock to early 2014 and asked the average moviegoer who the " Guardians of the Galaxy " were, you would have likely been met with a blank stare. At the time, they were a C-list comic book team operating on the fringes of the Marvel universe . The pitch sounded more like a fever dream than a guaranteed blockbuster: a 1980s pop-culture-obsessed thief, a green-skinned assassin, a fiercely literal warrior, a genetically modified talking raccoon, and a walking, fighting tree. It was widely considered Marvel Studios' biggest gamble. Yet, when the lights went down and Blue Swede’s “Hooked on a Feeling” blasted through theater speakers, that gamble paid off in astronomical ways. The Guardians didn't just succeed; they redefined the modern superhero genre , proving that audiences were hungry for something weirder, wilder, and fundamentally more human. Here is a deep dive into how a ragtag group of cosmic outlaws stole the galaxy's heart—and ours. The Ro...

How do I write a C program to find the factorial of a number?

  Finding the factorial of a number is a common problem in computer programming. A factorial of a number is the product of all positive integers up to and including that number. For example, the factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120. In C programming, we can write a program to find the factorial of a number using a loop. Here's an example program: c Copy code # include <stdio.h> int main () { int num; unsigned long long fact = 1 ; printf ( "Enter a positive integer: " ); scanf ( "%d" , &num); for ( int i= 1 ; i<=num; ++i) { fact *= i; } printf ( "Factorial of %d = %llu\n" , num, fact); return 0 ; } In this program, we first declare the variables num and fact . We then prompt the user to enter a positive integer using the printf() and scanf() functions. Note that we use the unsigned long long data type for the variable fact because the factorial of a large number can become very ...