0% found this document useful (0 votes)
68 views7 pages

C++ LABACTIV 1&2 Sharaf Ahmed Sharaf

The document contains code snippets from multiple C++ programs. The first program prints a half pyramid of characters based on a user-input character. The second program calculates the volume and surface area of a cone by taking radius and height as user input. The third program calculates the sum of a series from 1 to n terms as entered by the user.

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)
68 views7 pages

C++ LABACTIV 1&2 Sharaf Ahmed Sharaf

The document contains code snippets from multiple C++ programs. The first program prints a half pyramid of characters based on a user-input character. The second program calculates the volume and surface area of a cone by taking radius and height as user input. The third program calculates the sum of a series from 1 to n terms as entered by the user.

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 Char

#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;
}

___________________________________________________________________________________
_
/*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.*/

#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 ;
float slant_height ;
float height ;
float vol, sur_area;

// Printing value of volume


// and surface area
cout << "Volume Of Cone : ";
cin>>radius>>height;
cout<< "Volume Of Cone : " << volume(radius, height) << endl;
cout << "Surface Area Of Cone : ";
cin>>radius>>slant_height;
cout<<"Surface Area Of Cone : " <<surface_area(radius, slant_height)<<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;
}

___________________________________________________________________________________
___________________________________________________________________________________
______________

______________________________________________________LabActivy
2__________________________________________________________________________________
_______________________
//Write a program to replace one element in
//a binary array with another element
#include <iostream>
using namespace std;
void updateArray(int arr[], int y)
{
// Update array
for (int i = 0; i <= y - 2; i++)
arr[i] = arr[i + 1];

// Change the last element to -1


arr[y - 1] = -1;

// Print the updated array


for (int i = 0; i < y; ++i)
cout << arr[i] << " ";
}
int main()
{
int arr[] = { 56, 5, 45, 23, 44 };
int R = sizeof(arr) / sizeof(arr[0]);
updateArray(arr, R);
cout<<endl;
return 0;
}

___________________________________________________________________________________
__________________________
/*2.A numeric integer grade is between 20 and 66. Using integer division or
otherwise obtain a
int value that is 2, 3, 4, 5, or 6 from the numeric grade. Write code using a
switch statement
using this number in the selector expression that assigns letter grades based on
this 10 point
scheme:*/
#include<iostream>
using namespace std;
int main()
{
f:
char l;
int mark,lo,sum;
double ave;
cout<<"\n\n";
sum=0;
for(lo=1;lo<=7;lo++)
{
cout<<"Enter the mark of subject "<<lo<<":";cin>>mark;
sum=sum+mark;
}
cout<<"\n\n";
cout<<"The summation for all subjects marks is :"<<sum<<endl;
cout<<"\n";
ave=sum/7;
cout<<"The average of marks is :"<<ave<<endl;
cout<<"\n";
if(ave>=90 & ave<=100) {l='A';}
else if(ave>=80 & ave<90) {l='B';}
else if(ave>=70 & ave<80) {l='C';}
else if(ave>=60 & ave<70) {l='D';}
else if(ave>=50 & ave<60) {l='E';}
else if(ave>=0 & ave<50) {l='F';}
else cout<<"\n\n";
switch(l)
{
case 'A':
cout<<"Excellent\n\n";
break;

case 'B':
cout<<"Very good\n\n";
break;

case 'l3':
cout<<"Good\n\n";
break;

case 'C':
cout<<"Accepted\n\n";
break;

case 'D':
cout<<"Weak\n";
break;

case 'E':
cout<<"Failed";
break;
default:
cout<<"Wrong marks or average!\n\n";
break;
}

goto f;

___________________________________________________________________________________
__________________________
// C++ Program to Delete an Element from an Array
#include<iostream>
using namespace std;
int main()
{
int arr[5], tot=10, i, elem, j, found=0;
cout<<"Enter 10 Array Elements: ";
for(i=0; i<tot; i++)
cin>>arr[i];
cout<<"\nEnter Element to Delete: ";
cin>>elem;
for(i=0; i<tot; i++)
{
if(arr[i]==elem)
{
for(j=i; j<(tot-1); j++)
arr[j] = arr[j+1];
found++;
i--;
tot--;
}
}
if(found==0)
cout<<"\nElement doesn't found in the Array!";
else
cout<<"\nElement Deleted Successfully!";
cout<<endl;
return 0;
}
___________________________________________________________________________________
_______________________
// Program for reading the elements of two single
// arrays and combining them into one matrix.
#include<iostream>
using namespace std;
int main()
{
int arrOne[5], arrTwo[8], arrMerge[20];
int sizeOne, sizeTwo, a, y;
cout<<"Enter the Size for First Array: ";
cin>>sizeOne;
cout<<"Enter "<<sizeOne<<" Elements for First Array: ";
for(a=0; a<sizeOne; a++)
{
cin>>arrOne[a];
arrMerge[a] = arrOne[a];
}
y = a;
cout<<"\nEnter the Size for Second Array: ";
cin>>sizeTwo;
cout<<"Enter "<<sizeTwo<<" Elements for Second Array: ";
for(a=0; a<sizeTwo; a++)
{
cin>>arrTwo[a];
arrMerge[y] = arrTwo[a];
y++;
}
cout<<"\nThe New Array (Merged Array):\n";
for(a=0; a<y; a++)
cout<<arrMerge[a]<<" ";
cout<<endl;
return 0;
}

___________________________________________________________________________________
____

//C++ Passing Array to Function Example: Print maximum number


#include <iostream>
using namespace std;
void printMax(int arr[10]);
void printMin(int arr[10]);
int main()
{
int arr1[5] = { 25, 10, 54, 15, 40 };
int arr2[5] = { 12, 23, 44, 67, 54 };
int arr3[5] = { 30, 10, 20, 40, 50 };
int arr4[5] = { 5, 15, 25, 35, 45 };
printMax(arr1); //Passing array to function
printMax(arr2);

printMin(arr3);//passing array to function


printMin(arr4);
}
void printMax(int arr[5])
{
int max = arr[0];
for (int i = 0; i < 5; i++)
{
if (max < arr[i])
{
max = arr[i];
}
}
cout<< "Maximum element is: "<< max <<"\n";
}
void printMin(int arr[5])
{
int min = arr[0];
for (int i = 0; i > 5; i++)
{
if (min > arr[i])
{
min = arr[i];
}
}
cout<< "Minimum element is: "<< min <<"\n";
}

___________________________________________________________________________________
__________________________________

//Write a program to read binary elements


//of a matrix and print placed elementsMatrimonial
#include<iostream>
using namespace std;
int main ()
{
char arr[10], ele;
int i, n, pos;
cout << "Enter size of the array : ";
cin >> n;
cout << "\nEnter elements of the array : ";
for (i = 0; i <= n; i++)
cin >> arr[i];
cout << "\nEnter the position : ";
cin >> pos;
for (i = 0; i <= n; i++)
if (pos == i)
ele = arr[i];
cout << "\nElement at position " << pos << " is : " << ele;
cout<<endl;
return 0;
}
___________________________________________________________________________________
__________________________________________

You might also like