C C++ program to generate series of prime numbers

 

Flowchart

Generating prime numbers upto n flowchart

 

Coding in C

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

 

Coding in C++

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

 

Output

Generating prime numbers upto n output

 

Published by

admin

I am a computer teacher, programmer and web developer