Question #1497

Write a program that let the user to enter any positive number and the program will tell the user if the number is a prime number or not a prime number. Allow the user to continue the process until the user enters a sentinel. For this program, you are required to use an alphabet as the sentinel. Use do-while loop for this program.

Expert's answer

#include <stdbool.h>#include <stdio.h>using namespace std;bool is_simple(int count){    for(int i =2; i < count; i++){        if(count % i == 0)            return false;    }    return true;}int main(int argc, char * argv[]){    char * str;    do{        int count;        str = new char[30];        cin >> str;        count = atoi(str);        if(is_simple(count)){            printf("Is simple.");        }else{            printf("Is not simple!");        }    }while(!isalpha((int)(*str)));    return 0;}
LATEST TUTORIALS
APPROVED BY CLIENTS