C C++ program to check prime number

 

Flowchart

Checking whether given number is prime or not flowchart

 

Coding in C

#include<stdio.h>
int main()
{
int num, c=2;
char prime=’t’;
printf(“Enter a number: “);
scanf(“%d”,&num);
while(c<num)
{
if(num%c==0)
{
prime=’f’;
break;
}
c++;
}
if(prime==’t’)
{
printf(“Prime number”);
}
else
{
printf(“Not a prime number”);
}
return 0;
}

 

Coding in C++

#include<iostream>
using namespace std;
int main()
{
int num, c=2;
char prime=’t’;
cout<<“Enter a number: “;
cin>>num;
while(c<num)
{
if(num%c==0)
{
prime=’f’;
break;
}
c++;
}
if(prime==’t’)
{
cout<<“Prime number”;
}
else
{
cout<<“Not a prime number”;
}
return 0;
}

 

Output

Checking whether given number is prime or not output

 

Published by

admin

I am a computer teacher, programmer and web developer