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 9121539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:53:09+00:00 2026-06-17T05:53:09+00:00

I need to create a program that takes ‘N’ as Input and show the

  • 0

I need to create a program that takes ‘N’ as Input and show the first N prime numbers and finally sum them…
Here’s my code, but it doesn’t work. (It loops at: “if ((num == 2) || (num == 3))”)

Can you help me?
Thanks in advance.

I can’t use functions and arrays.

Code:

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    int n, num = 1, rest, cont = 0, sum = 0;
    bool exit = false;
    int e = 1;

    cout<<"How many prime number do you want to show? ";
    cin>>n;

    while (cont <= n)
    {
        num++;
        if ((num == 2) || (num == 3))
        {
            cout<<num<<endl;
            sum += num;
            cont++;
        }
        else if (num%2 == 0)
        {
        }
        else
        {
            bool exit = false;
            int div = 3;
            while (!exit)
            {
                rest = num%div;

                if (num == div)
                {
                    exit = true;
                    cout<<num<<endl;
                    sum += num;
                    cont++;
                }

                if (rest == 0)
                    exit = true;                    
                div++;
            }
        }       
        num++;
    }    
    cout<<"Sum: "<<sum<<endl;

    return 0;
}
  • 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-06-17T05:53:10+00:00Added an answer on June 17, 2026 at 5:53 am

    To begin with, you are using a variable name resto instead of rest, which is the name with which you declared it (if (resto == 0)).

    Second, you increase num twice: at the beginning and at the end of your loop. Remove the last num++ and it should work better.

    Third, initialize cont to 1 and not to 0, or it will compute sum of the first N+1 primes instead of the first N primes.

    Here is the fixed version of your code:

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int n, num = 1, rest, cont = 1, sum = 0;
        bool exit = false;
        int e = 1;
    
        cout<<"How many prime number do you want to show? ";
        cin>>n;
    
        while (cont <= n)
        {
            num++;
            if ((num == 2) || (num == 3))
            {
                cout<<num<<endl;
                sum += num;
                cont++;
            }
            else if (num%2 == 0)
            {
            }
            else
            {
                bool exit = false;
                int div = 3;
                while (!exit)
                {
                    rest = num%div;
    
                    if (num == div)
                    {
                        exit = true;
                        cout<<num<<endl;
                        sum += num;
                        cont++;
                    }
    
                    if (rest == 0)
                        exit = true;
    
                    div++;
                }
            }
        }
        cout<<"Sum: "<<sum<<endl;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create program that takes input from stdin in this format: abcde
I am writing a program that takes in input various lecture requests that need
I need to create program that creates a XML schema like below using System.Xml.XmlSchema
I want to create a program that produces an executable slideshow. So I need
Here is my problem, I need to create a javascript function that takes 2
I have a program that creates multiple text files of rdf triples. I need
The folder I need to delete is one that is created from my program.
For a school project I need to create a program and it'd be nice
I need to create a simple drawing program in c for graphs, i.e. nodes
I need to create several Raphael objects in my program. field1 and field2 are

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.