Â
Hello Readers,
In this post we are going to learn about prime numbers, what is prime numbers ?
Prime numbers are the numbers which are only divisible by itself and only by 1. This same for both mathematic and programming languages. Here 0 and 1 are both prime and whole numbers. In the complete mathematics 2 is the only even prime number.
Prime numbers is one of the most basic concept in Mathematics. But when it comes to programming it becomes quite difficult for coders/programmers to create a syntax to find the prime number and the list of prime numbers.
So we brought here today the syntax to find the list of prime numbers and also to check whether any number is prime or not....
C
#include <stdio.h>
int differ()
{
int n;
int a,b;
int i, Number = 1, count;
int inp;
printf("Please enter the operation you want to operate \n");
printf(" Enter 1 to calcuate whether the number is prime or not \n Enter 2 to find all the prime numbers from 1 to n \n");
scanf("%d",&n);
switch (n)
{
case 1:
printf("pleaase enter the number \n");
scanf("%d", &a);
int flag =0;
b=a/2;
for (int i = 2; i <=a; i++)
{
if (a % i == 0)
{
flag = 0;
}
else if (a % i == 1)
{
flag = 1;
}
}
if (flag == 1)
{
printf("the number is prime \n");
}
else
{
printf("the number is not a prime number \n");
}
break;
case 2:
printf(" Prime Numbers are: \n");
scanf("%d",&);
while(Number <= inp)
{
count = 0;
i = 2;
while(i <= Number/2)
{
if(Number%i == 0)
{
count++;
break;
}
i++;
}
if(count == 0 && Number != 1 )
{
printf(" %d \n", Number);
}
Number++;
}
}
return 0;
}
int cont()
{
char z;
scanf("%c",&z);
if (z == "y" || z == "Y")
{
differ();
}
else
{
printf("Thank you for using ");
}
return 0;
}
int main()
{
differ();
cont();
return 0;
}
  Â
Output for C++
C++
// # HELP FOR CODERS
// A program to check whether a number is prime or non-prime.
// In this program you can also get all the prime numbers
// from 1 to n.
#include <iostream>
using namespace std;
// Declaring a Function Switch of type Integer
int Switch()
{
//Initializing variable prime of type bool
bool prime = true;
int a;
cout << "Please select your operation from below.\n\n"
<< "Enter the corresponding number for your Operation.\n"
<< "1. Check whether a number is Prime or Non-prime.\n"
<< "2. Find all the prime numbers from 1 to n.\n";
cin >> a;
//Applying Switch case for user input
switch (a)
{
//First case to check the number is prime or non-prime
case 1:
int number, i;
cout << "Check whether a number is prime or non-prime\n";
cout << "\nPlease enter the number to check\n";
cin >> number;
for (i = 2; i <= number / 2; ++i)
{
if (number % i == 0)
{
prime = false;
}
}
if (prime)
{
cout << number << " is a Prime number";
}
else
{
cout << number << " is a Non-prime number";
}
break;
//Second case to print all prime numbers till user's input
case 2:
int nums, j;
bool allprime;
cout << "\nFind all the prime numbers from 1 to n\n";
cout << "Enter the number till where you want the prime numbers\n";
cin >> nums;
cout << "\nPrime numbers between 1 and "
<< nums << " are:\n";
for (i = 1; i <= nums; i++)
{
//Ignoring 1 and 0
if (i == 1 || i == 0)
continue;
allprime = 1;
//Calculating all the prime numbers till user's input
for (j = 2; j <= i / 2; ++j)
{
if (i % j == 0)
{
allprime = 0;
break;
}
}
//Printinf the prime numbers
if (allprime == 1)
cout << i << endl;
}
break;
//This is a default case
default:
cout<<"ERROR Occurred";
}
return 0;
}
//This is another function loop of type integer
//It is used to ask user whether he wants to continue or not.
int loop()
{
char cont;
do
{
cout<<"\n\n---------------------------------\n";
cout << "\nDo you want to continue ??\n"
<< "For YES 'y'"
<< " For NO 'n'" << endl;
cin >> cont;
if (cont == 'y')
{
Switch();
}
else
{
cout << "***Thank You***";
}
} while (cont == 'y');
return 0;
}
//This is the main function of program
int main()
{
cout << "\n\n ****This is a Program for Prime numbers****\n\n";
//Callimg the function Switch
Switch();
//Calling the Function loop
loop();
return 0;
}
  Â
Output for C++
Java
package com.company;
import java.util.*;
//This is the main Class
class Main
{
//check prime function :
static void checkPrime(int n) {
int flag;
flag = 0;
int m = n / 2;
if (n == 0 || n == 1)
{
System.out.println(n + " is not prime number");
}
else
{
for (int i = 2; i <= m; i++)
{
if (n % i == 0)
{
System.out.println(n + " is not prime number");
flag = 1;
break;
}
}
if (flag == 0)
{
System.out.println(n + " is prime number");
}
}//end of else
}
static void listPrime(int num)
{
int i;
int j;
//Empty String
StringBuilder primeNumbers = new StringBuilder();
for (i = 1; i <= num; i++)
{
int counter = 0;
for (j = i; j >= 1; j--)
{
if (i % j == 0)
{
counter = counter + 1;
}
}
//Appended the Prime number to the String
if (counter == 2) primeNumbers.append("\n ").append(i);
}
System.out.println("The prime numbers till " + num + " are");
System.out.println(primeNumbers);
}
public static void main(String[] args)
{
int number;
System.out.println("\n\n\t\tHello, User!!!");
Scanner obj = new Scanner(System.in);
//System.in is a standard input stream
char repeat;
char option;
do
{
System.out.println("This ia a Program for Prime numbers");
System.out.println("Enter c or C to check for prime number");
System.out.println("\t\t\t\tOR\nEnter l or L to list prime numbers");
option = obj.next().charAt(0);
if (option == 'c' || option == 'C')
{
System.out.println("Enter number to check whether its prime or not");
number = obj.nextInt();
checkPrime(number);
}
else if (option == 'l' || option == 'L')
{
System.out.println("Enter number till which you want Prime numbers");
number = obj.nextInt();
listPrime(number);
}
else
{
System.out.println("invalid input! please run te program again");
}
System.out.println("Do you want to continue");
System.out.println("Enter 'y' to continue or 'n' to exit");
repeat = obj.next().charAt(0);
}
while (repeat == 'y');
System.out.println("Thank You!");
}
}
Output for JAVA
Python
# check whether given number n is prime
# Or
# List prime numbers till n
repeat = "y"
print("\n\n***This is a program for Prime Numbers*** \n")
# This is the operating function
def mainPrime():
print(" Enter 1 to list all prime number till a given input number")
print("\t\t\t\t\t\tOR")
print(" Enter 2 to check whether a given input number is prime or not\n ")
choice = int(input(""))
def listPrime(num): # This is the function for the list of prime numbers
for i in range(2, num + 1):
if i > 1:
for j in range(2, i):
if (i % j) == 0:
break
else:
print(i)
def checkPrime(num): # This is the function to check the prime number
check = ""
flag = 0
if num == 2:
check = str(num) + " is prime"
for i in range(2, num):
if num % i == 0:
check = str(num) + " is not prime"
flag = 1
elif flag == 0:
check = str(num) + " is prime"
return check
if choice == 1:
# list all prime till n
n = int(input("Enter number to list down all prime numbers till your number\n "))
listPrime(n)
elif choice == 2:
# check prime
n = int(input(" Enter number to check whether it's prime or not\n "))
print(checkPrime(n))
else: # Ask to repeat the operation
print(" Error ! Invalid input please Run the program again.")
print("------------------------------------")
print("\nDo you want to repeat ?\n")
global repeat
repeat = input("Enter y to continue or any other key to exit\n")
# repeat = "y"
# return repeat
while repeat == "y":
# calling the main function
mainPrime()
print("\n\n------ Thank You! ------")
Output for PYTHON
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP