Prime Number in C++

Published on 28 April 2022 (Updated: 25 September 2022)

Prime Number in C++

Welcome to the Prime Number in C++ page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

#include<iostream>
#include<stdlib.h>
#include<string.h>

using namespace std;

int main(int argc, char **argv)
{
    if (argc == 1){
        cout<<"Usage: please input a non-negative integer\n";
        return 1;
    }
    string tmp = argv[1];
    if (argc == 1 || argv[1][0] == '\0' || (atoi(argv[1]) == 0 && strcmp(argv[1], "0") != 0) || atoi(argv[1]) < 0 || tmp.find(".") != string::npos ) {
        cout<<"Usage: please input a non-negative integer\n";
    } 
    else {
        int input = atoi(argv[1]);
        if(input == 0 || input ==1){
            cout<<"composite\n";
            return 0;
        }
        for(int i = 2; i < input; ++i){
            if(input % i == 0){
                cout<<"composite\n";
                return 0;
            }
        }
        cout<<"Prime\n";
    }

    return 0;
}

Prime Number in C++ was written by:

If you see anything you'd like to change or update, please consider contributing.

Note: The solution shown above is the current solution in the Sample Programs repository as of Oct 19 2019 23:17:34. The solution was first committed on Oct 14 2019 00:44:05. As a result, documentation below may be outdated.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.