CP Lab Practical File
CP Lab Practical File
Submitted By
AKSHAT CHAURASIA (0901IT231007)
Information Technology (I Semester)
Faculty Mentor
Dr. Dhananjay Bisen
Assistant Professor, Department of IT
List of Programs
int main(){
int num1,num2;
cin>>num1;
cin>>num2;
return 0;
OUTPUT :
2.||C++ PROGRAM TO calculate and display volume of
cylinder
#include<iostream>
using namespace std;
int main(){
float r,h;
float pi = 3.14;
cout<<"enter the radius of cylinder : ";
cin>>r;
#include<cmath>
int main(){
float b,T;
float u,a,c,t;
float s,V;
cout<<"enter a :: ";
cin>>a;
cout<<"enter b :: ";
cin>>b;
cout<<"enter c :: ";
cin>>c;
cin>>t;
V = (u+(a*t));
s= ((u*t) + (0.5*a));
return 0;
} OUTPUT ::
4.C++ program to take student information and calculate
percentage secured ||
#include<iostream>
int main(){
string name;
int roll_no;
int marks[5];
float percentage;
float totalmarks=0;
getline(cin,name);
cin>>roll_no;
cin>>marks[i];
totalmarks += marks[i];
}
float hello = (totalmarks)/500;
percentage=(hello) *100;
cout<<name;
cout<<roll_no;
return 0;
OUTPUT::
5.C++ PROGRAM TO SWAP VALUE OF VARIABLES WITH
AND WITHOUT USING THIRD VARIABLE ||
#include<iostream>
int main(){
int a=8;
int b=9;
a=a+b;
b=a-b;
a=a-b;
int c;
c=a;
a=b;
b=c;
return 0; }
OUTPUT::
6.C++ PROGRAM TO USE PREFFIX INCREMENT AND
DECREMENT AND POSTFIX INCREMENT AND
DECREMENT ||
#include <iostream>
using namespace std;
int main() {
int num = 5;
// Unary Prefix Increment
cout << "Unary Prefix Increment: num = " << num ;
int result = ++num;
cout << " , result "<< result << endl;
return 0;
}
OUTPUT ::
7.C++ PROGRAM TO FIND LARGEST TO THREE
NUMBERS USING TERNARY OPERATOR ||
#include <iostream>
int main() {
int largest = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ?
num2 : num3);
cout << "The largest number is: " << largest <<endl;
return 0;
} OUTPUT ::
8.C++ PROGRAM TO FIND ROOTS OF A QUADRATIC
EQUTION ||
#include<iostream>
#include<cmath>
int main(){
double a,b,c;
cin>>a;
cin>>b;
cin>>c;
double disc;
disc = b * b - (4 *a *c);
double root1,root2;
if (disc>0)
else if(disc == 0)
root1 = (-b/(2*a));
else
} return 0;
OUTPUT ::
9.C++ PROGRAM TO CHECK WHETHER A NUMBER IS PRIME
OR NOT ||
#include<iostream>
using namespace std;
int main(){
int i,count = 0,j;
cout<<"enter the value of p :: ";
cin>>i;
for (j = 1; j <= i; j++)
{
if (i%j == 0)
{
count++;
}
}
if (count == 2)
{
cout<<"it is a prime number";
}
else
{
cout<<"not a prime number";
}
return 0;
}
OUTPUT ::
10.C++ PROGRAM TO CALCULATE GRADE A/C TO MITS
LADDER ||
#include<iostream>
int main() {
float a;
cin>>a;
if(a>=90) {
}
else if(a>=45 && a<50) {
else if(a==45) {
else {
return 0;
OUTPUT ::
11.C++ PROGRAM TO CHECK WHETHER A YEAR IS LEAP
OR NOT ||
#include<iostream>
using namespace std;
int main(){
int n,sum=0;
cout<<"enter the value of n :: ";
cin>>n;
for ( ; n > 0; n =(n/10))
{
sum = sum + n%10;
}
cout<<"the sum of number is :: "<<sum;
return 0;
}
OUTPUT ::
13.C++ PROGRAM TO PRINT FOLLOWING LOOPS USING FOR
LOOPS ||
(i). #include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
cout<<" ";
cout<<" * ";
cout<<endl;
} return 0;
}
(ii). #include<iostream>
using namespace std;
int main(){
int n;
cout<<"enter the value of n:: ";
cin>>n;
for (int i = 1; i <= n; i=i+1)
{
for(int j=1; j<=i; j=j+1){
cout<<i<<" ";
}
cout<<endl;
}
return 0;
}
(iii). #include<iostream>
using namespace std;
int main(){
int n;
cout<<"enter the value of n :: ";
cin>>n;
for (int i = 1; i <= n; i++)
{
for (int k = 1; k <= n-i; k++) {
cout<<" ";
}
return 0;
}
(iv). #include<iostream>
using namespace std;
int main(){
int n;
cout<<"enter the value of n :: ";
cin>>n;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= n-i; j++)
{
cout<<" * ";
}
cout<<endl;
}
return 0;
}
(v). #include<iostream>
using namespace std;
int main(){
int n;
cout<<"enter the value of n :: ";
cin>>n;
return 0;
}
(vi). #include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
cout<<" ";
char ch = 'A';
cout<<ch<<" ";
ch++;
cout<<endl;
return 0;
(vii). #include<iostream>
using namespace std;
int main(){
int n;
cout<<"Enter the value of n:"<<endl;
cin>>n;
cout<<endl;
}
return 0;
}
(viii). #include <iostream>
using namespace std;
int main() {
return 0;
OUTPUT ::
(i). 14.C++ PROGRAM TO CALCULATE RECURSION USING
(ii)
FACTORIAL || .
#include<iostream>
using namespace std;
int factorial(int n){
if(n<=1){
(iii). return 1; (iv)
} .
return n* factorial(n-1);
}
int main(){
int a;
cout<<"enter the number :: "; (vi) (v).
cin>>a; .
cout<<"the factorial of "<<a<< " is "<<factorial(a)<< endl;
return 0;
}
OUTPUT ::
(vii). (viii).
15.C++ PROGRAM TO ADD TWO MATRICES||
#include<iostream>
using namespace std;
int main()
{
int matarray[3][3];
int matarray2[3][3];
int matarray3[3][3];
cout<<"enter first array"<<endl;
for (int i=0;i<3;i++)
{
for(int k=0;k<3;k++)
{
cin>>matarray[i][k];
}
cout<<endl;
}
#include <iostream>
#include <string>
class Student {
private:
std::string name;
int id;
int semester;
float cgpa;
public:
this->name = name;
this->id = id;
this->semester = semester;
this->cgpa = cgpa;
}
// Member function to set details
this->name = name;
this->id = id;
this->semester = semester;
this->cgpa = cgpa;
maxCGPA[semester - 1] = newCGPA;
};
int main() {
Student students[10] = {
{"John", 1, 1, 3.8},
{"Alice", 2, 2, 3.9},
};
// Displaying student details and updating max CGPA for each semester
students[i].getDetails();
Student::updateMaxCGPA(students[i].semester, students[i].cgpa);
std::cout << "Maximum CGPA for Semester " << i + 1 << ": " << Student::maxCGPA[i] << std::endl;
return 0;
}
OUTPUT :-