Mashail thumbnail
18th Anniversary Thumbnail Navigator Thumbnail
Posted: 17 years ago
#1

Hey guys.... im taking this programming course in college... and i actually need some help............ i need to make a program using C++ to print prime numbers...............

plzzz help!!!!

thanks in advance!

Created

Last reply

Replies

8

Views

836

Users

6

Frequent Posters

pinka28 thumbnail
18th Anniversary Thumbnail Rocker Thumbnail + 2
Posted: 17 years ago
#2
/*prime number check between 1 to a range , range will be enter by user*/
#include<iostream.h>
#include<conio.h>

void main()
{
int num;
clrscr();
cout<<"enter value till to be check";
cin>>num;/*all prime number will be chacke between 1 to num*/
if( num!=1)
{
for(int i=2;i<num+1;i++)
{
int flag=0;
for(int j=2;j<i;j++)
{ if (i%j==0)
flag=1;
}
if(flag==0)
cout<<"\n"<< i; /* print the prime number between 1 to num*/
}
}
else
cout<< num<<"is prime number";
getch();
}
Edited by pinka_anshu - 17 years ago
akhl thumbnail
18th Anniversary Thumbnail Dazzler Thumbnail Fascinator 1 Thumbnail
Posted: 17 years ago
#3
I saw this post just now. May be I am too late. If not, then let me know from which number to which number, do you want to print prime numbers? For example, do you want to print all primes between 1 and 100?
akhl thumbnail
18th Anniversary Thumbnail Dazzler Thumbnail Fascinator 1 Thumbnail
Posted: 17 years ago
#4
BTW, check out what pinka_anshu has posted.
But Pinka,
You have assumed that 1 is prime, which is not true. 1 is NOT a prime number.
Also some suggestions to improve the efficiency of your program: -

1. If i%j is zero, then you set flag = 1. After that you should write code to break the for loop, because when flag has been set to 1 or in other words, when you have found a value of j, which divides i, then there is no need to check with other values of j.

2. You are checking with j from 2 to i-1. Your program will be faster if you check upto only i/2. Actually, sqrt(i) will also do, but that will require calculating square root, which will take its own time. So, you can use i/2.
Yahoo2 thumbnail
18th Anniversary Thumbnail Voyager Thumbnail
Posted: 17 years ago
#5

Originally posted by: pinka_anshu

/*prime number check between 1 to a range , range will be enter by user*/
#include<iostream.h>
#include<conio.h>

void main()
{
int num;
clrscr();
cout<<"enter value till to be check";
cin>>num;/*all prime number will be chacke between 1 to num*/
if( num!=1)
{
for(int i=2;i<num+1;i++)
{
int flag=0;
for(int j=2;j<i;j++)
{ if (i%j==0)
flag=1;
}
if(flag==0)
cout<<"\n"<< i; /* print the prime number between 1 to num*/
}
}
else
cout<< num<<"is prime number";
getch();
}



good one 😛
Posted: 17 years ago
#6
Wow 👏

So amazing that you all know this...C++ is hard...Good work, 😊
Yahoo2 thumbnail
18th Anniversary Thumbnail Voyager Thumbnail
Posted: 17 years ago
#7

Originally posted by: AsliiGuy

Wow 👏

So amazing that you all know this...C++ is hard...Good work, 😊



Thats true, C++ is hard, I only know some of it.
akhl thumbnail
18th Anniversary Thumbnail Dazzler Thumbnail Fascinator 1 Thumbnail
Posted: 17 years ago
#8
Please post your doubts in C++ in the following thread: -
https://www.indiaforums.com/forum/computers-science-technology/911809/post-your-doubts-in-c

I will definitely answer.
dannyk thumbnail
19th Anniversary Thumbnail Navigator Thumbnail Engager Level 1 Thumbnail
Posted: 17 years ago
#9
lol nice to know that this forum help to complete the school work ..

Related Topics

Top

Stay Connected with IndiaForums!

Be the first to know about the latest news, updates, and exclusive content.

Add to Home Screen!

Install this web app on your iPhone for the best experience. It's easy, just tap and then "Add to Home Screen".