0% found this document useful (0 votes)
6 views

Example Programs in C++

The document contains multiple C++ example programs demonstrating the use of control structures such as if statements, if-else statements, for loops, while loops, and do-while loops. Each program prompts the user for input and performs specific operations based on the input, such as checking conditions or iterating through a set number of times. Additionally, there is an example of bubble sorting an array of numbers.

Uploaded by

kawirab90
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Example Programs in C++

The document contains multiple C++ example programs demonstrating the use of control structures such as if statements, if-else statements, for loops, while loops, and do-while loops. Each program prompts the user for input and performs specific operations based on the input, such as checking conditions or iterating through a set number of times. Additionally, there is an example of bubble sorting an array of numbers.

Uploaded by

kawirab90
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

* Example Program For If Statement In C++

little drops @ thiyagaraaj.com

Coded By:THIYAGARAAJ MP */

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

using namespace std;


int main()
{
// Variable Declaration
int a;

//Get Input Value


cout<<"Enter the Number :";
cin>>a;

//If Condition Check


if(a > 10)
{
// Block For Condition Success
cout<<a<<" Is Greater than 10";
}

// Wait For Output Screen


getch();
return 0;
}

Burble sorting

#include<iostream>
using namespace std;

int main(){
//declaring array
int array[5];
cout<<"Enter 5 numbers randomly : "<<
endl;
for(int i=0; i<5; i++)
{
//Taking input in array
cin>>array[i];
}
cout<<endl;
cout<<"Input array is: "<<endl;

for(int j=0; j<5; j++)


{
//Displaying Array
cout<<"\t\t\tValue at "<<j<<" Index:
"<<array[j]<<endl;
}
cout<<endl;
// Bubble Sort Starts Here
int temp;
for(int i2=0; i2<=4; i2++)
{
for(int j=0; j<4; j++)
{

//Swapping element in if statement


if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
// Displaying Sorted array
cout<<" Sorted Array is: "<<endl;
for(int i3=0; i3<5; i3++)
{
cout<<"\t\t\tValue at "<<i3<<" Index:
"<<array[i3]<<endl;
}
return 0;
}

If…else… statement
Example Program For if...else Statement In C++
little drops @ thiyagaraaj.com

Coded By:THIYAGARAAJ MP */

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

using namespace std;

int main()
{

// Variable Declaration
int a;

// Get Input Value


cout<<"Enter the Number :";
cin>>a;

//If Condition Check


if(a > 10)
{
// Block For Condition Success
cout<<a<<" Is Greater than 10";
}
else
{
// Block For Condition Fail
cout<<a<<" Is Less than/Equal 10";
}

// Wait For Output Screen


getch();
return 0;
}

For loop

Example Program For for Loop In C++


little drops @ thiyagaraaj.com

Coded By:THIYAGARAAJ MP */

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

using namespace std;

int main()
{

// Variable Declaration
int a;

// Get Input Value


cout<<"Enter the Number :";
cin>>a;

//for Loop Block


for (int counter = 1; counter <= a; counter++)
{
cout<<"Execute "<<counter<<" time"<<endl;
}

// Wait For Output


Return 0;
}
While loop
* Example Program For While Loop In C++
little drops @ thiyagaraaj.com

Coded By:THIYAGARAAJ MP */

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

using namespace std;

int main()
{
// Variable Declaration
int a;

// Get Input Value


cout<<"Enter the Number :";
cin>>a;

int counter = 1;
//while Loop Block
while (counter <= a)
{
cout<<"Execute While "<<counter<<" time"<<endl;
counter++;
}

// Wait For Output Screen


getch();
return 0;
}

Do while loop
* Example Program For Do..While In C++
little drops @ thiyagaraaj.com

Coded By:THIYAGARAAJ MP */

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
// Variable Declaration
int a;

// Get Input Value


cout<<"Enter the Number :";
cin>>a;

int counter = 1;
//Do while Loop Block
do
{
cout<<"Execute Do While "<<counter<<" time"<<endl;
counter++;
}while (counter <= a);

// Wait For Output Screen


getch();
return 0;
}

You might also like