How to write a program in C++?

How to write a program in any language?

How to write a program in C++? . Its always difficult when you are asked to write a program for the first time. Programming is something most of which students are afraid of and end up on “learning” the program. There are certain tips you need to follow when you want to write a program in any programming language. If you follow them, you can be a good programmer. I will explain these tips one by one.

  1. The first and foremost thing you need to know is to develop a “pseudo code” for the particular task you want to perform. You can have a rough idea initially how to do. Dry run the pseudo code for random values and test your results.
  2. Draw the flow chart for that pseudo code and see what kind of decision making, testing conditions can be involved for solving a particular problem.
  3. The next step is to know the syntax of that particular language in which you want to write a program. You should have clear cut idea of the thing you want to do or implement. That’s necessary actually. Because once you have the “idea” which statement does what, you can plan to write a program.
  4. Once you are familiar with the syntax you can step forward for writing a program. The first attempt may not be successful, you can make changes and get the desired results.

Example:

Lets consider the case of finding of testing a number is prime or not. We know that if a number is divisible by 1 and itself only then it is said to be a prime number. So, when we look at this problem the first thing that clicks in our mind is to find the factors of the number. If the factors are only 1 and that particular number, then that number will pass the test otherwise not.

Another point is we dont need to test till the actual number. We can test only till the half of the actual number. This will save running time of our algorithm.

int n;

cin>>n;

for( int i=2;i<=n/2;i++)

{

if(n%i==0)

{ cout<<n<<“is not a prime number”<<endl;

exit(0); }

cout<<n<<“it is a prime number”<<endl;

}

flowchart design
flowchart design

Also read here: 

https://eevibes.com/loops-repetitive-statements-forwhileand-do-while/

What are the repetitive and loop statements in C++?

Leave a Reply

Your email address will not be published. Required fields are marked *