Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Half Pyramid using “*”

#include <iostream>

using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {

int rows;

cout << "enter number of rows: ";

cin >> rows;

for(int i=1; i<=rows; ++i)

for(int j = 1; j<=i; ++j)

cout<< "* ";

cout<<"\n";

return 0;

Inverted Half Pyramid using “*”

#include <iostream>

using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {

int rows;

cout << "enter number of rows: ";


cin >> rows;

for(int i=rows; i>=1; --i)

for(int j = 1; j<=i; ++j)

cout<< "* ";

cout<<"\n";

return 0;

Full Pyramid using “*”

#include <iostream>

using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {

int rows, space;

cout << "enter number of rows: ";

cin >> rows;

for(int i=1, k=0; i<=rows; ++i, k=0)

for(space = 1; space<=rows-i; ++space)

cout << " ";

while(k != 2*i-1)

{
cout<<"* ";

++k;

cout<<"\n";

return 0;

Half Pyramid Using Numbers

#include <iostream>

using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {

int rows;

cout << "enter number of rows: ";

cin >> rows;

for(int i=1; i<=rows; ++i)

for(int j = 1; j<=i; ++j)

cout << j << " ";

cout<<"\n";

return 0;

}
Inverted Pyramid Using Numbers

#include <iostream>

using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {

int rows;

cout << "enter number of rows: ";

cin >> rows;

for(int i=rows; i>=1; --i)

for(int j = 1; j<=i; ++j)

cout << j << " ";

cout<<"\n";

return 0;

Full Pyramid using Numbers

#include <iostream>

using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {

int rows, count=0, count1=0, k=0;

cout << "enter number of rows: ";


cin >> rows;

for(int i=1; i<=rows; ++i)

for(int space =1; space <= rows-i; ++space)

cout<<" ";

++count;

while(k != 2*i-1)

if(count <= rows-1)

cout<<i+k<< " ";

++count;

else

++count1;

cout << i+k-2*count1 << " ";

++k;

count1 = count = k = 0;

cout << endl;

return 0;

}
Array Programs

Sum using Array

#include <iostream>

using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {

int numbers[5], sum = 0;

cout<<"Enter 5 Numbers: ";

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

cin>> numbers[i];

sum+= numbers[i];

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

return 0;

Sum using array with the limit of 100

#include<iostream>

using namespace std;

int main(){

int n, i;

float num[100], sum = 0.0, ave;


cout<< "Enter the number of data: ";

cin>>n;

while (n>100 || n<=0)

cout<<"Error! number must be in range of 1 to 100" <<endl;

cout<<"Enter the number again: ";

cin>>n;

for (i=0; i<n; ++i)

cout<< i+1 << ". Enter Number: ";

cin>> num[i];

sum += num[i];

ave = sum / n;

cout << "Average = "<<ave;

return 0;

Display highest number using Array

#include <iostream>

using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {

int i, n;

float arr[100];
cout<<"Enter total number of elements: ";

cin>>n;

cout<<endl;

for(i = 0; i < n; ++i)

cout<<"Enter Number "<< i + 1 <<" : ";

cin>>arr[i];

for(i=1; i<n; ++i)

if (arr[0] < arr[i])

arr[0] = arr[i];

cout<< "Largest element = "<< arr[0];

return 0;

Standard Deviation

#include <iostream>

#include<cmath>

using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

float calculateSD(float data[]);

int main(int argc, char** argv) {

int i;

float data[10];

cout << "Enter 10 elements: ";


for(i=0; i < 10; ++i)

cin >> data[i];

cout << endl << "Stsndard Deviation = " << calculateSD(data);

return 0;

float calculateSD(float data[])

float sum = 0.0, mean, standardDeviation = 0.0;

int i;

for(i=0; i < 10; ++i)

sum += data[i];

mean = sum/10;

for(i=0; i < 10; ++i)

standardDeviation += pow(data[i] - mean, 2);

return sqrt(standardDeviation / 10);

You might also like