Monday, October 6

Iteration Constructs

The Do-While Loop


Source Code :



// Program Description:
// A Program that uses do-while iteration construct to print a series

#include<iostream.h>
#include<conio.h>

void main()
{
// cleaning the screen
clrscr();


// declaration of variables
int ctr = 0;


// do-while loop starts here
do
{
cout<<endl<<"\t"<<ctr;


Iteration Constructs

The While Loop


Source Code :



// Program Description:
// A Program that uses while iteration construct to print a series

#include<iostream.h>
#include<conio.h>

void main()
{
// cleaning the screen
clrscr();


// declaration of variables
int ctr = 0;


// while loop starts here
while(ctr<=5)
{
cout<<endl<<"\t"<<ctr;
ctr++;

}// while loop ends here


// waiting for the user to analyse the output
cout<<endl<<endl<<"\tPress any key to quit...";
getch();

}// main ends here


Program Flow :



This Iteration Construct is a bit different from the For Iteration Construct because it does not have the Initialising Condition in the Loop statement and the Increment/Decrement Statement is also absent. Instead the Counter Variable is Initialised outside (before) the Loop and the Increment/Decrement statement is present inside the Loop block.

  • The Loop Statement checks the Counter's Value with the specified condition.
  • If the Condition evaluates to true the compiler enters the Loop's body and executes the whole block and then comes back to the conditional statement and the cycle goes on.
  • Once the False condition is met the Compiler exit the Loop and moves on.
  • The compiler then waits for the user to press any key to exit.


Output Screenshots :


Iteration Constructs

The For Loop


Source Code :



// Program Description:
// A Program that uses for iteration construct to print a series

#include<iostream.h>
#include<conio.h>

void main()
{
// cleaning the screen
clrscr();


// declaration of variables
int ctr;


// for loop starts here
for(ctr=0;ctr<=10;ctr++)
{
cout<<endl<<"\t"<<ctr;
}// for loop ends here


// waiting for the user to analyse the output
cout<<endl<<endl<<"\tPress any key to quit...";
getch();

}// main ends here


Program Flow :


  • A counter variable is created in the memory. This variable's value acts as the termination statement for the loop.
  • The Looping Statement is encountered by the compiler. In this statement the compiler checks for the counter variable's initial value to enter the loop.
  • If the Loop Condition is fulfilled by the counter variable the block of code which is placed inside the curly braces gets executed.
  • The Loop then comes back to the Conditional Statement and checks whether the counter variable's value still fulfills a certain criteria.
  • If the value of counter still fulfills the condition the Loop is executed again.
  • This process continues until the Counter's value fulfills the condition.
  • The Compiler exits the Loop and waits for the user to press a key to exit.

Output Screenshots :




Sunday, October 5

Simple Interest Calculator

Another Basic program to Calculate the Simple Interest

Source Code : 


// Program Description :
// A Program that calculates the simple interest.

#include<iostream.h>
#include<conio.h>

void main()
{
// cleaning the screen
clrscr();


// declaration of variables
double p;    float r,si; int t;


// the input of values
cout<<"Enter the Principal Amount : ";
cin>>p;
cout<<"Enter the Rate of Interest : ";
cin>>r;
cout<<"Enter the Time : ";
cin>>t;


// calculation of simple                       Principal x Rate x Time
// interest using the formula   SI   =   -----------------------------
//                                                                         100
si=(p*r*t)/100;


// display the interest that is to paid
cout<<endl<<"The Interest to be paid is "<<si;


// waiting for the user to analyse the output
cout<<endl<<endl<<"Press any key to quit...";
getch();

}  //main ends here

Program Flow : 


  • The message for entering the principal amount is displayed.
  • The user enters the principal amount and presses Enter.
  • The message for entering the rate of interest is displayed.
  • The user enters the rate of interest and presses Enter.
  • The message for entering the time period is displayed.
  • The user enters the time period and presses Enter.
  • The program calculates the Simple Interest using the values provided.
  • The Simple Interest is displayed.
  • The program waits for user to Press any key to exit.


Output Screenshots : 



A Screenshot displaying the Simple Interest to be paid on 20000 @ 12% p.a. after 3 years as 7200


Sum of Numbers

This is the most Basic Program of all time - "The sum of two numbers".



Source Code :


// Program Description :
// To calculate the sum of two numbers

#include<iostream.h>
#include<conio.h>

void main()
{
// cleaning the screen
clrscr();


// declaration of variables
float num1,num2,sum;


// input of values
cout<<"Enter the first number : ";
cin>>num1;
cout<<"Enter the second number : ";
cin>>num2;

// calculating the sum of numbers
sum=num1+num2;


// output the sum
cout<<endl<<"The sum of "<<num1<<" and "<<num2<<" is "<<sum<<endl;


// waiting for the user to analyse the output
cout<<endl<<"Press any key to quit...";
getch();

} // main ends here


Program Flow :



  • The message for entering the first number is displayed.
  • The user enters the number and presses Enter.
  • The message for entering the second number is displayed.
  • The user enters the second number and presses Enter.
  • The program calculates the sum of the numbers.
  • The sum of the numbers is displayed.
  • The program waits for user to Press any key to exit.

Output Screenshots :


A Screenshot of the program displaying the sum of 23 and 23 equal to 46