Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6603747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:03:24+00:00 2026-05-25T19:03:24+00:00

#include <iostream> using namespace std; int checkIfPrime(int num) { for (int i = 1;

  • 0
#include <iostream>
using namespace std;

int checkIfPrime(int num) {
    for (int i = 1; i < num; i++) {
        int result = num / i;
        if (num == result * i) {
                return 0;
        }
    }
}


int main() {
    int i = 3;
    while(1) {  
             int c = checkIfPrime(i);
             if (c != 0) {
                cout << c << "\n";
             }
             i = i + 2;
      }
}

Sorry about posting the wrong code!

When I run this, nothing happens.. Can someone tell me what I am doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:03:25+00:00Added an answer on May 25, 2026 at 7:03 pm

    After the question has completely changed its meaning:

    Your CheckIfPrime is just wrong. You’re checking divisibility in a very strange way. The general way to check whether a is divisible by b is if(a % b == 0)

    That said, your error is that your loop starts with 1. Of course every number is divisible by 1, therefore by your logic, no number is prime. Start with for(int i = 2; .... (Depending on whether you want to consider 1 as prime or not, you might want to test specially for num == 1 initially.)

    Also, the end condition is very inefficient. It is enough to check before the square root of num, that is i <= sqrt(num), but since sqrt is a rather slow and imprecise operation, MUCH better is to loop this way:

     for(int i  = 2; i * i < = num; ++i)
    

    Another note – to generate all prime numbers from 1 to some MAX_VAL, your approach is very inefficient. Use the Sieve of Erastothenes.

    Some stylistic note: your function should ideally return bool rather than int, and don’t forget to return true or 1 after the loop has finished without returning.

    Original Answer:

    First of all, you need

    fprintf(OUTPUT_FILE, "%d", Num); //d instead of s, no & before Num
    

    instead of

    fprintf(OUTPUT_FILE, "%s", &Num); 
    

    Second, you use the file I/O extremely inefficiently. Why do you open and close the file for every number? You should open it once, write all numbers, and then close it.

    And thirdly, your algorithm doesn’t seem to have anything to do with prime numbers… 🙂

    By the way, since the first issue results in Undefined Behavior, you can’t complain about any behavior of the program, since it’s… well, undefined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> using namespace std; int main() { char buffer[8]; int field=534; memcpy(buffer,&field,sizeof(field)); cout<<buffer<<endl;
#include <iostream> using namespace std; int main (void) { cout << 1\t2\t3\t4\t5\t6\t7\t8\t9 << endl
#include <iostream> using namespace std; int main() { double u = 0; double w
#include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000,
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
I have this program in c++: #include <iostream> using namespace std; int main() {
#include iostream #include string using namespace std; #define AA(bb) \ string(::##bb); int main (int
#include <iostream> using namespace std; typedef int MYINT; int main() { int y =
#include <iostream> using namespace std; int main() { int i; for(i=0; i <= 11;
#include<iostream> using namespace std; class Something { public: int j; Something():j(20) {cout<<Something initialized. j=<<j<<endl;}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.