Printing alternate even and prime numbers?
The required output of this program was to print alternate even and prime
numbers till 'n' terms.But the problem is, even though there is no error
reported, I'm unable to get the desired result. Can anyone please point
out the bug, or give me another coding for this to work.
required output:"2,2,4,3,6,5,8,7...n terms".
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{clrscr();
int k=0,c=2,g=0,n=10,f=0; //'c'-prime number counter,'k'-even no
counter,'n'-number of terms
do
{ k+=2;cout<<k;g++; //for printing even numbers(k). 'g'is the counter
for(int i=0;i<1000;i++) //F-1. run this loop large numbr of times
{ f=0;
for(int j=0;j<c/2;j++) //F-2
{
if((c%j)==0) { f++;} //checking prime
}//end of loop F-2
if(f==0){cout<<c;c++;break;}//to come out of loop F-1 as 'c' is a prime
number.
else {c++; } //checking for next prime number
}//end of loop F-1
} while(g<n);
getch();
}
No comments:
Post a Comment