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

C++ LabActivity

The document contains multiple C++ programs that print half pyramids, patterns, and calculate volumes and surface areas using formulas. It contains the code to calculate the sum of a series from 1 to n terms.

Uploaded by

Sharaf Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

C++ LabActivity

The document contains multiple C++ programs that print half pyramids, patterns, and calculate volumes and surface areas using formulas. It contains the code to calculate the sum of a series from 1 to n terms.

Uploaded by

Sharaf Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

//C++ program to print half Pyramid of numbers

#include<iostream>
using namespace std;
int main(){
int m,n,rows;
cout<<"Ener the number of rows:";
cin>>rows;
for( int m=0;m<=rows;m++){
for(int n=0;n<=m;n++){
cout<<n<<" ";
}
cout<<"\n";
}
return 0;
}
___________________________________________________________________________________
___
#include <iostream>
using namespace std;

int main()
{
int rows, coef = 1;

cout << "Enter number of rows: ";


cin >> rows;

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


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

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


{
if (j == 0 || i == 0)
coef = 1;
else
coef = coef*(i-j+1)/j;

cout << coef << " ";


}
cout << endl;
}

return 0;
}
___________________________________________________________________________________
__________________________
#include <iostream>
using namespace std;

int main()
{
int rows;

cout << "Enter number of rows: ";


cin >> rows;

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


{
for(int space = 0; space < rows-i; ++space)
cout << " ";

for(int j = i; j <= 2*i-1; ++j)


cout << "* ";

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


cout << "* ";

cout << endl;


}

return 0;
}
___________________________________________________________________________________
_______________-
#include <iostream>
using namespace std;

int main()
{
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;
}
___________________________________________________________________________________
____________________-
#include <iostream>
using namespace std;
int main()
{
int space, rows;

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 << endl;
}
return 0;
}
___________________________________________________________________________________
___________
#include <iostream>
using namespace std;

int main()
{
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 << endl;
}

return 0;
}
___________________________________________________________________________________
_-
#include <iostream>
using namespace std;

int main()
{
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 << endl;
}

return 0;
}
______________________________________________________________________________
#include <iostream>
using namespace std;

int main()
{
char input, alphabet = 'A';

cout << "Enter the uppercase character you want to print in the last row: ";
cin >> input;

for(int i = 1; i <= (input-'A'+1); ++i)


{
for(int j = 1; j <= i; ++j)
{
cout << alphabet << " ";
}
++alphabet;

cout << endl;


}
return 0;
}
___________________________________________________________________________________
___
#include <iostream>
using namespace std;

int main()
{
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;
}
___________________________________________________________________________________
___________________________________________
/*C++ program for Calculation of the surface and the volume of a cone
Write a program that calculates the volume of a solid cone. The formula is:
volume=(PI x radius² x height)/3 Read in the values for the parameters
height and radius(as floating point numbers). Then the program should
calculate the volume considering the following restriction :radius and height
must be greater than0 If the user types in a correct value for one of the
variables ,the program should print out an error message and quit .
Otherwise ,it displays the result.*/

// CPP program to calculate Volume


// and Surface area of Cone
#include<iostream>
using namespace std;

float pi = 3.14159;

// Function to calculate
// Volume of cone
float volume(float r, float h)
{
return (float(1) / float(3)) * pi *
r * r * h;
}

// Function to calculate
// Surface area of cone
float surface_area(float r, float s)
{
return pi * r * s + pi * r * r;
}

// Driver Code
int main()
{
float radius = 5;
float slant_height = 13;
float height = 12;
float vol, sur_area;

// Printing value of volume


// and surface area
cout << "Volume Of Cone : "
<< volume(radius, height) << endl;
cout << "Surface Area Of Cone : "
<< surface_area(radius, slant_height);
return 0;
}
___________________________________________________________________________________
_____________________________
/*C++ program for Calculation of the surface and the volume of a cone
Write a program that calculates the volume of a solid cone. The formula is:
volume=(PI x radius² x height)/3 Read in the values for the parameters
height and radius(as floating point numbers). Then the program should
calculate the volume considering the following restriction :radius and height
must be greater than0 If the user types in a correct value for one of the
variables ,the program should print out an error message and quit .
Otherwise ,it displays the result.*/
// CPP program to calculate Volume
// and Surface area of Cone
#include<iostream>
using namespace std;

float pi=0;

// Function to calculate
// Volume of cone
float volume(float r, float h)
{
return (float(1) / float(3)) * pi * r * r * h;
}

// Function to calculate
// Surface area of cone
float surface_area(float r, float s)
{
return pi * r * s + pi * r * r;
}

// Driver Code
int main()
{
float radius = 5;
float slant_height = 13;
float height = 12;
float vol, sur_area;
cin>>pi;

// Printing value of volume


// and surface area
cout << "Volume Of Cone : "
<< volume(radius, height) << endl;
cout << "Surface Area Of Cone : "
<< surface_area(radius, slant_height)<<endl;
cout<<endl;
return 0;
}

___________________________________________________________________________________
______________________________

//C++ Program to calculate the series (1) + (1+2) + (1+2+3) + (1+2+3+4) + … +


//(1+2+3+4+…+n)
#include <iostream>
using namespace std;

int main()
{
int i, j, n, sum = 0, tsum;
cout << "\n\n Find the sum of the series (1) + (1+2) + (1+2+3) + (1+2+3+4)
+ ... + (1+2+3+4+...+n):\n";
cout <<
"----------------------------------------------------------------------------------
--------\n";
cout << " Input the value for nth term: ";
cin >> n;
for (i = 1; i <= n; i++)
{
tsum = 0;
for (j = 1; j <= i; j++)
{
sum += j;
tsum += j;
cout << j;
if (j < i)
{
cout << "+";
}
}
cout << " = " << tsum << endl;
}
cout << " The sum of the above series is: " << sum << endl;
}

You might also like