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

  • SEARCH
  • Home
  • 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 6906549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:19:27+00:00 2026-05-27T08:19:27+00:00

I wrote a program, which takes 10 integers from user, and then counts the

  • 0

I wrote a program, which takes 10 integers from user, and then counts the prime numbers in it and displays the sum. The problem is that it is taking all odd numbers as prime numbers. I have tried very long but couldn’t figure it out. Here is the program.

#include<iostream>
#include<conio>
#include<math>

int isPrime(int);

int main()
{
    int sum_of_prime;
    int count=0;
    int a[10];
    for(int i=1; i<=10; i++){
        cout<<"Enter a number: ";
        cin>>a[i];
        if( isPrime(a[i]) ){
            sum_of_prime+=a[i];
            countp++;
        }
    }
    cout<<"Total Prime Numbers in given Numbers: "<<count<<endl;
    cout<<"Sum of All the prime numbers in given numbers"<<sum_of_prime<<endl;
    getch();
}

int isPrime(int n){
    for(int i=2; i<=sqrt(n); i++){
        if(n%i==0)
            return 0;
        else
            return 1;
    }
}
  • 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-27T08:19:28+00:00Added an answer on May 27, 2026 at 8:19 am

    Your isPrime method returns during the first pass through the loop, and NEVER does subsequent passes through the loop.
    It checks 2, but never checks 3, 4, 5, 6, etc.

    You need to return 1; ONLY after you’ve run though the entire loop:

    int isPrime(int n)
    {
        for(int i=2; i<=sqrt(n); i++)
        {
            if(n%i==0)
                return 0;  // n has a factor, therefore is not prime, ==> False
        }
    
        return 1;  // All possible factors were checked, none are factors, therefore prime ==> True
    }
    

    P.S. For a speed improvement, just check 2, then 3,5,7,9,11, etc.
    There is no point in checking all the other even numbers (4,6,8,etc).
    So you can run about twice as fast if you only check odd numbers.

    You can also improve the speed by not doing a sqrt(n) on every pass through the loop.

    This function would look like:

    int isPrime(int n)
    {
        if (n%2 == 0) return 0;  // Check 2 separately.
    
        int Root = (int)sqrt(n); // Pre-calculate SQRT once!
    
        for(int i=3; i<=Root; i+=2)  // Increment by TWO at a time, getting 3,5,7,9
        {
            if(n%i==0)
                return 0;
        }
        return 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a program which includes writing and reading from database. When I run
I recently wrote a program that takes inputted char data, tests if it is
I recently inherited a small Java program that takes information from a large database,
I wrote a program to solve the problems for my physics chapter which takes
I wrote a simple program which takes a predefined number to a predefined power
I have a program Sample which takes input both from stdin and a non-standard
I wrote a program that takes in a partial rss feed and outputs a
I wrote a quick program which executes every statement before giving a seg fault
I wrote a program out, which was all in one file, and the methods
I wrote a test program to monitor my Picture folder which points to c:\users[username]\Pictures

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.