Translate

Ad-Horizontal

Search This Blog

Tuesday, 30 October 2018

How to write a C++ program (Turbo C++) to input a number and check whether the user input number is a prime number or not.

We are gonna see how to write a C++ program (Turbo C++) to input a number and check whether a user input number is a prime number or not.


This is a very basic program used by new comers to make their grip to the IDE and learn the way very way to writing C++ codes.

The following codes have been run and compiled in C++(Turbo C++).

The writing in the right-hand side of this "//" in the codes are called comments of the code. They are not a necessary part of the codes and can be omitted in the codes. They are just mentioned so as to tell you the use/purpose of writing the left-hand side of the "//" or the codes.   In some special cases depending upon the Fond & Size of the text on your device the comments may entered the right hand-side of the "//"(next line) and come in between the main codes. so as to, make it easier for you to understand/distinguish the comments from the main codes; we have written the main code and comments in different sizes.
Example: Main codes: "  #include<iostream.h>  "
                 Comments: "   //It is a header file and used to call the input-output classes(Pre-defined library in C++).  " .


The Variables are user initialized with zero, so that they won't store any garbage value and hamper the final output of the program.

For more accurate, users may replace "int" and "float" with "long" and "double" respectively.


Prime Number is a number that is divisible only by itself and 1.


The code are as follows:


#include<iostream.h>              //It is a header file and used to call the input-output classes(Pre-defined library in C++).
#include<conio.h>      //It is a header file and used to provide a console input/output screen(Pre-defined library in C++).
void main()                                                                               //It is the Main function of the Program/Code.
{
clrscr();                                                      //It is a function and used to clear the previous output in the output screen.
int num=0,i=0,count=0;                            //Declaring variables.
cout<<"Enter the number to be checked:"<<"\n";           //Displaying sentence within " ".
cin>>num;                                                                     //Taking input from the user. Here the input is the number.
for(i=1;i<=num;i++)                                                    //Running a LOOP.
{
if(num%i==0)                                                             //Running a if statement.
{
count=count+1;                                                            //Increment of the count variable.
}
}
if(count==2)                                                            //Running a if statement.
{
cout<<"The entered number,("<<num<<") is a Prime number."<<"\n";            //Displaying output.
}
else                                                                                                               //Running a else statement.
{
cout<<"The entered number,("<<num<<") is not a Prime number."<<"\n";        //Displaying output.
}  
getch();         //It is a function to show the output screen for long.
}


Program:



   






Output:



 






Hope that  the Blog have helped you in your work. 
Do visit again to see more Coding related blogs.

No comments:

Post a Comment