i want to replace the prime numbers by 0 my code is not working

This is a discussion on i want to replace the prime numbers by 0 my code is not working within the C++ Programming forums, part of the General Programming Boards category; Code: #include<fstream.h> #include<iostream.h> void main() { int k=0; fstream f; f.open("num.txt",ios::in|ios::out); for(int i=1;i<=100;i++) { f<<i; for(int j=2;j<i/2;j++) { if(i&#37;j==0) continue; ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    8

    i want to replace the prime numbers by 0 my code is not working

    Code:
    #include<fstream.h>
    #include<iostream.h>
    void main()
    {  int k=0;
    	fstream f;
    	f.open("num.txt",ios::in|ios::out);
    	for(int i=1;i<=100;i++)
    	{
    
    		f<<i;
    
    	for(int j=2;j<i/2;j++)
    	{
    		if(i&#37;j==0)
            continue;
            if(i%j==0)
    			f<<k;
    		 
    	}
    			 
    		
    	}	
    			
    
    	
    }
    Last edited by Salem; 06-09-2007 at 01:40 AM. Reason: [CODE][/CODE] goes AROUND THE CODE!!!!!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Mmmkay... First of all.

    1) Post with code tags.
    2) Indent properly (Dunno if you did that, since not using code tags means it's not indented properly.).
    3) void main() should be int main().

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Not to mention that you are using fstream.h and iostream.h which are deprecated header files, use fstream and iostream (without .h) instead. You should also tell us what you are trying to do *in details*, what your code does instead, what you have tried to do to fix it (if it doesn't work) or what you have tried to do to implement it, if you have troubles figuring how to code whatever you are trying to code. Just don't put unformatted code bluntly like that, you won't get much from us that way. It's considered quite rude on programming forums.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    if(i&#37;j==0)
       continue;
    if(i%j==0) //this will never be true
       f<<k; //so this will neve execute
       //and even if it will - k is always 0 - you never change it
    And Why do you open file for reading if you only write?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Also why do you have two ifs for the same thing with the continue?
    Code:
    #include<fstream>
    #include<iostream>
    using namespace std;
    
    int main(void){
    	int k=0;
    	fstream f;
    	f.open("num.txt",ios::in|ios:ut);
    	for(int i=1;i<=100;i++){
    		f<<i;
    		for(int j=2;j<i/2;j++){
    			if(i%j==0)
    				continue;
    			if(i%j==0)
    				f<<k;
    		}
    	}
            return 0;
    }
    There now just fix it up so we can understand

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    Good call =D

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,828
    Now there is a bit of JS which is supposed to check whether code has [code][/code] tags around it.

    It is not sufficient to simply throw tags into the post just to make the error message go away.
    Read the intro threads to learn how to do it properly.

    This is the 2nd time I've fixed a post of theirs, it will not happen again.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime numbers
    By whiterebbit in forum C++ Programming
    Replies: 21
    Last Post: 12-12-2008, 12:18 AM
  2. C code not working
    By D3ciph3r in forum C Programming
    Replies: 2
    Last Post: 05-27-2005, 05:13 PM
  3. prime numbers?
    By tu_user in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2004, 01:19 PM
  4. More Prime Numbers
    By mmuhlenb in forum C Programming
    Replies: 3
    Last Post: 02-21-2003, 10:06 AM
  5. Prime Numbers
    By Korn1699 in forum C++ Programming
    Replies: 7
    Last Post: 11-03-2001, 09:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21