Sei sulla pagina 1di 7

Programm 1

#include <iostream>

using namespace std;

int main()

int numbers[5], sum = 0;

cout << "Enter 5 numbers: ";

// Storing 5 number entered by user in an array

// Finding the sum of numbers entered

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

cin >> numbers[i];

sum += numbers[i];

cout << "Sum = " << sum << endl;

return 0;

Output

Enter 5 numbers: 3

5
4

Sum = 18

Pragramm 2

/* C++ Selection Statements - C++ if Statement */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"Enter a number: ";
cin>>num;
if(num%2==0)
{
cout<<"You entered an even number";
}
getch();
}

Here is the sample run of this C++ program:

Programm 3

/* C++ Selection Statements - C++ if Statement */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x, y, z, max;
cout<<"Enter any three numbers: ";
cin>>x>>y>>z;
max = x;
if(y>max)
{
max = y;
}
if(z>max)
{
max = z;
}
cout<<"\n"<<"The largest of "<<x<<", "<<y<<" and "<<z<<" is "<<max;
getch();
}

When the above C++ program is compile and executed, it will produce the following
output. This is the output, if 3rd number is biggest.

Here is another output, if 2nd number is biggest.

Now, here is the last output, if 1st number is biggest:


Programm 4

/* C++ Selection Statements - C++ if-else Statement */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"Enter a number: ";
cin>>num;
if(num%2==0)
{
cout<<"You entered an even number";
}
else
{
cout<<"You entered an odd number";
}
getch();
}

Below are the two sample run of the above C++ program:

Programm 5

/* C++ Selection Statements - C++ if-else Statement */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x, y ;
cout<<"Enter two numbers: ";
cin>>x>>y;
if(x>y)
{
cout<<x<<" is largest";
}
else
{
cout<<y<<" is largest";
}
getch();
}

When the above C++ program is compile and executed, it will produce the following
output:

Programm 6

/* C++ Arrays - C++ Arrays Example Program */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr1[5] = {1, 2, 3, 4, 5};
int arr2[] = {10, 20, 30, 40, 50};
int i;
cout<<"Array arr1 contains:\n";
for(i=0; i<5; i++)
{
cout<<arr1[i]<<"\t";
}
cout<<"\n\n";
cout<<"Array arr2 contains:\n";
for(i=0; i<5; i++)
{
cout<<arr2[i]<<"\t";
}

getch();
}

Here is the sample output of the above C++ program:


Programm 7

/* C++ Arrays - C++ Arrays Example Program */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[10];
int i;
cout<<"Enter 10 array elements: ";
for(i=0; i<10; i++)
{
cin>>arr[i];
}
cout<<"\nArray contains:\n";
for(i=0; i<10; i++)
{
cout<<arr[i]<<" ";
}
getch();
}

Below is the sample run of this C++ program:

Programm 8
/* C++ Iteration Statements - C++ for Loop */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1; i<=10; i++)
{
cout<<i<<" ";
}
getch();
}

When the above program is compile and executed, it will produce the following output:

Programm 9

Potrebbero piacerti anche