Monday, May 31, 2021

Prime Number in C++

 Machine Problem 

A number which is only divisible by itself and 1 is known as prime number,  for example: 7 is a prime number because it is only divisible by itself and 1.

Write a program to ask the user to give a number and then the problem will check and determine if the given number is a prime number or not.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me at the following email address for further details.  If you want to advertise on my website, kindly contact me also at my email address also. Thank you.

My email address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com

My mobile number here in the Philippines is 09173084360.








Program Listing

prime_number.cpp

// prime_number.cpp
//
// Machine Problem
//
// A number which is only divisible by itself and 1 is known as prime number, 
// for example: 7 is a prime number because it is only divisible by itself and 1.
//
// Write a program to ask the user to give a number and then the program will
// check and determine if the given number is a prime number or not.
// 
//  Jake Rodriguez Pomperada, MAED-IT, MIT
//  www.jakerpomperada.com   www.jakerpomperada.blogspot.com
// jakerpomperada@gmail.com
// Bacolod City, Negros Occidental Philippines.

#include <iostream>

int num_val=0,i=0;

bool flag = true;
   

int main(){
   
   std::cout << "\n\n";
   std::cout << "\tPrime Number in C++";
   std::cout << "\n\n";
   std::cout<<"\tGive a Number (Positive Number Only) : ";
   std::cin>>num_val;

   std::cout << "\n";
     
   for(i = 2; i <= num_val / 2; i++) {
      if(num_val % i == 0) {
         flag = false;
         break;
      }
   }
   
   if (flag==true) {
         std::cout<<"\tThe given number "<<num_val<<" is a prime number.";
       }
   else {
      std::cout<<"\tThe given number "<<num_val<<" is not a prime number.";
    }
   std::cout <<"\n\n";
   std::cout <<"\tEnd of Program\n";
 }



No comments:

Post a Comment