LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-30-2009, 12:40 PM   #1
palisetty_suman
Member
 
Registered: Feb 2007
Location: TX, USA
Distribution: fedora
Posts: 191

Rep: Reputation: 33
Thumbs up Program doesn't print output


Hi,
I have written a program on c++. It doesn't give any errors but it does't give any output.my code is as follows.It is to print prime numbers from 3 to 100.

------------------------------------------------------------------

#include<iostream>
using namespace std;
int main()
{
int i,j,num,k;
cout<<"Enter the prime range number"<<endl;
cin>>num;
for(i=3;i<=num;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
k++;
}
if(k==2)
cout<<i<<endl;
}
}
---------------------------------------------------------------------
Please help me out.
 
Old 01-30-2009, 12:56 PM   #2
dorian33
Member
 
Registered: Jan 2003
Location: Poland, Warsaw
Distribution: LFS, Gentoo
Posts: 591

Rep: Reputation: 32
'k' is not initalized, so what 'k++' does mean?
 
Old 01-30-2009, 12:59 PM   #3
palisetty_suman
Member
 
Registered: Feb 2007
Location: TX, USA
Distribution: fedora
Posts: 191

Original Poster
Rep: Reputation: 33
k

K is just like counter. K is initialised.
int k; which is equal to k=o.

The problem is with returning value I guess. Please check there.
 
Old 01-30-2009, 01:12 PM   #4
dorian33
Member
 
Registered: Jan 2003
Location: Poland, Warsaw
Distribution: LFS, Gentoo
Posts: 591

Rep: Reputation: 32
Quote:
Originally Posted by palisetty_suman View Post
int k; which is equal to k=o.
Really?
So try this:
Code:
#include<iostream>
using namespace std;
int main(){
 int k;
   cout<<k<<endl;
}
 
Old 01-30-2009, 01:21 PM   #5
palisetty_suman
Member
 
Registered: Feb 2007
Location: TX, USA
Distribution: fedora
Posts: 191

Original Poster
Rep: Reputation: 33
Dorian,
I tried what you said and for k, i am getting some value 1262164 but not 0. I am wrong with that. I accept but I should get some or the other answer for it. Isn't it. Now I will initialize k to 0.
 
Old 01-30-2009, 01:26 PM   #6
palisetty_suman
Member
 
Registered: Feb 2007
Location: TX, USA
Distribution: fedora
Posts: 191

Original Poster
Rep: Reputation: 33
I have changed my code to this and I am able to print only 3. Please predict where is the mistake i am doing.

-------------------------------------------------------------------------------------


#include<iostream>
using namespace std;
int main()
{
int i,j,k=0;
for(i=3;i<=100;i++)
{
for(j=1;j<=i;j++)
{
if(i%j==0)
k++;
}
if(k==2)
cout<<i<<endl;
}
}



-----------------------------------------------------------------------------------------
 
Old 02-01-2009, 05:59 PM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

If you want help (especially help with homework), please:

1. Put your code in code blocks
2. Explain more about what you're trying to get your code to do
For example, add a few comments at important places in your program.
3. Q: what does "k==2" mean (in terms of your program)?
How does it relate to a "prime number"?
Do you expect "k==2" to occur many times, or just once in your program?
 
Old 02-01-2009, 06:50 PM   #8
palisetty_suman
Member
 
Registered: Feb 2007
Location: TX, USA
Distribution: fedora
Posts: 191

Original Poster
Rep: Reputation: 33
Thumbs up Prime numbers

I am able to run the program. Thanks.I got the result.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass the output of one program to another program without backticks? BrianK Linux - General 4 11-11-2008 06:27 PM
Cannot change print output from kmail martinlinuxuser Linux - Desktop 0 08-17-2008 06:04 AM
How to print a given line from a file on the standard output Fond_of_Opensource Linux - Newbie 1 08-24-2006 02:45 AM
C function to execute a program and return the output of the program ryan.n Programming 4 08-14-2004 10:11 PM
How do I print double sided output rdaves@earthlink.net Linux - General 1 06-27-2001 02:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration