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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:26:08+00:00 2026-05-29T11:26:08+00:00

//Testing numbers for primality #include <iostream> #include <cmath> using namespace std; int main() {

  • 0
//Testing numbers for primality

#include <iostream>
#include <cmath>
using namespace std;

int main() {

int n;          //User input number
int i;          //Input number will be divided by i
int count;      

count = 0;

cout << endl;   

while(n != 'q') {

    cout << "#: ";
    cin >> n;       

    for(i = 1; i <= n; i++) {   
                    //increase the divisor until it's equal to n                
        if(n % i == 0) {
             count++;
        }       
        //If there is no remainder, count increases by 1. Since prime numbers are only divisible by 1 and themselves, count will be exactly 2 in the case of a prime number.

    }

    if(count == 2) {
        cout << "   1\n";       //1 = yes prime

    }else if(count != 2) {      
        cout << "   0\n";       //0 = not prime

    }

    count = 0;

}

if(n == 'q') {
    return(0);
}
}

Here I am testing numbers to see if they are prime. Count increases every time the remainder of division n/i is 0 so when count=2, output is 1 for yes, otherwise 0 for no. I’ve gotten the program to test as many numbers as I want correctly during a session, but I am trying to create an escape sequence.

I tried using the condition (n==’q’) for quit, but when I input q, the program loops infinitely. I tried putting a break; statement for this condition within the while loop but the result is the same. I am guessing this problem has to do with char-int/int-char conversion. Can someone hint to me how I can create a working exit sequence?

  • 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-29T11:26:09+00:00Added an answer on May 29, 2026 at 11:26 am

    You have no code that can read a q. Your input logic only accepts a number. You then test if that number is equivalent to a q character. The equivalent integer to the letter q is 113. If you try that, it will quit.

    Since you really want to input a number or a letter, you’ll need to write input logic that can accept either. Then you’ll need to check what input you got, and then process it accordingly.

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

Sidebar

Related Questions

I am aware that there are a number of primality testing algorithms used in
I'm writing a program that calculates input a user gives, simple numbers. 2, 4,
Which tool is the best one for accesing large bit numbers for testing Crypto
I'm testing a C++ class with a number of functions that all have basically
I'm creating script for automated testing of financial application. Using VbScript as language. Need
I'm using Watir to do some automated testing for a website. This particular test
I have a regular expression testing for numbers( 0-9 ) and/or forward slashes (
I'm testing one application where Regex pattern match credit card then such numbers should
I'm testing a web page using selenium. I want to get all the 'leaf
I need to generate some random numbers in C for testing and debugging the

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.