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

Madhav Institute of Technology & Science Gwalior

(A Govt. Aided UGC Autonomous Institute, Affiliated to RGPV, Bhopal)


NAAC Accredited with A++ Grade

Department of Information Technology

Practical Lab File

“Computer Programming (3160122)”

Submitted By
AKSHAT CHAURASIA (0901IT231007)
Information Technology (I Semester)

Faculty Mentor
Dr. Dhananjay Bisen
Assistant Professor, Department of IT

Session: July-Dec 2023


Madhav Institute of Technology & Science Gwalior
(A Govt. Aided UGC Autonomous & NAAC Accredited Institute Affiliated to RGPV, Bhopal)
NAAC Accredited with A++ Grade

List of Programs

Name and Roll No: AKSHAT CHAURASIA (0901IT231007)


Date:
Place: Gwalior Dr. Dhananjay Bisen
1.Write a C++ program to add two numbers and display its
sum
#include<iostream>
using namespace std;

int main(){

int num1,num2;

cout<<"enter the number:\n";

cin>>num1;

cout<<"enter the number:\n";

cin>>num2;

cout<<"The sum is :"<< num1+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;

cout<<"enter the height of cylinder : ";


cin>>h;

cout<<"the volume of cylinder is : "<<pi*r*r*h;


return 0;
}
OUTPUT ::
3.||C++ PROGRAM TO
CALCULATE NEWTONS
EQUATION’S OF
MOTION
#include<iostream>

#include<cmath>

using namespace std;

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;

cout<<"enter the valve of u : ";


cin>>u;

cout<<"enter the value of t : ";

cin>>t;

T = 2*a + sqrt(b) + 9*c;

V = (u+(a*t));

s= ((u*t) + (0.5*a));

cout<<"the value of V = "<<V<<endl;

cout<<"the value of S : "<<s<<endl;

cout<<"the value of T : "<<T<<endl;

return 0;

} OUTPUT ::
4.C++ program to take student information and calculate
percentage secured ||
#include<iostream>

using namespace std;

int main(){

string name;

int roll_no;

int marks[5];

float percentage;

float totalmarks=0;

cout<<"enter the name of student : ";

getline(cin,name);

cout<<"enter the roll no of student :";

cin>>roll_no;

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

cout<<"enter the marks of subject "<<i+1<<endl;

cin>>marks[i];

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

totalmarks += marks[i];

}
float hello = (totalmarks)/500;

percentage=(hello) *100;

cout<<"the marks in percentage is "<<percentage<<endl;

cout<<name;

cout<<roll_no;

return 0;

OUTPUT::
5.C++ PROGRAM TO SWAP VALUE OF VARIABLES WITH
AND WITHOUT USING THIRD VARIABLE ||
#include<iostream>

using namespace std;

int main(){

int a=8;

int b=9;

cout<<"the value of a is "<<a<<" and the value of b is "<<b<<endl;

//WITHOUT USING THIRD VARIABLE

a=a+b;

b=a-b;

a=a-b;

cout<<"the value of a is "<<a<<" and the value of b is "<<b<<endl;

int c;

cout<<"the value of a is "<<a<<" and the value of b is "<<b<<endl;

//WITH USING THIRD VARIABLE

c=a;

a=b;

b=c;

cout<<"the value of a is "<<a<<" and the value of b is "<<b<<endl;

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;

// Unary Post Increment


cout << "Unary Post Increment: num = " << num << ", result = " ;
result = num++;
cout<< result<<endl;

// Unary Prefix Decrement


cout << "Unary Prefix Decrement: num = " << num << ", result = ";
result = --num;
cout<<result<<endl;
// Unary Post Decrement
cout << "Unary Post Decrement: num = " << num << ", result = " ;
result = num--;
cout<< result << endl;

return 0;
}
OUTPUT ::
7.C++ PROGRAM TO FIND LARGEST TO THREE
NUMBERS USING TERNARY OPERATOR ||
#include <iostream>

using namespace std;

int main() {

int num1, num2, num3;

cout << "Enter the first number: ";

cin >> num1;

cout << "Enter the second number: ";

cin >> num2;

cout << "Enter the third number: ";

cin >> num3;

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>

using namespace std;

int main(){

double a,b,c;

cout<<"enter the value of a : ";

cin>>a;

cout<<"enter the value of b : ";

cin>>b;

cout<<"enter the value of c : ";

cin>>c;

cout<<a<<"x^2 + "<<b<<"x + "<<c<<endl;

double disc;

disc = b * b - (4 *a *c);

double root1,root2;

if (disc>0)

root1 = (-b + sqrt(disc))/ (2*a);

root2 = (-b - sqrt(disc))/ (2*a);

cout<<"the roots are "<<root1<<" and "<<root2;


}

else if(disc == 0)

root1 = (-b/(2*a));

cout<<"the roots are "<<root1;

else

double real=(-b/ (2*a));

double image = (sqrt(-disc)/ (2*a));

cout<<"the real and imaginary root is "<<real<<" and "<<image;

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

using namespace std;

int main() {

float a;

cin>>a;

cout<<"Percentage of the Student is "<<a<<endl;

if(a>=90) {

cout<<" Grade of the Student is A+ and Performance is Outstanding"<<endl;

else if(a>=80 && a<90) {

cout<<" Grade of Student is A and Performance is Excellent"<<endl;

else if(a>=70 && a<80) {

cout<<" Grade of Student is B+ and Performance is Very Good"<<endl;

else if(a>=60 && a<70) {

cout<<"Grade of Student is B and Performance is Good"<<endl;

else if(a>=50 && a<60) {

cout<<"Grade of Student is C+ and Performance is Average"<<endl;

}
else if(a>=45 && a<50) {

cout<<"Grade of Student is C and Performance is Satisfactory"<<endl;

else if(a==45) {

cout<<"Grade of Student is D and Performance is Marginal"<<endl;

else {

cout<<"Grade of Student is F and Student is Fail"<<endl;

return 0;

OUTPUT ::
11.C++ PROGRAM TO CHECK WHETHER A YEAR IS LEAP
OR NOT ||
#include<iostream>
using namespace std;

void check(int a){


if ( (a%100 == 0) && (a%400 == 0))
{
cout<<"It is a leap year and century year";
}
else if((a%4 == 0) && (a%100 != 0) )
{
cout<<"it is a leap year";
}
else
{
cout<<"it is a not leap year";
}
}
int main(){ OUTPUT ::
int x;
cout<<"enter the value of x : ";
cin>>x;
check(x);
return 0;
}
12.C++ PROGRAM TO PRINT SUM OF DIGITS OF NUMBERS USING
FOR LOOP||
#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;

cout<<"enter the value of n:: ";

cin>>n;

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

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

cout<<" ";

//for (int star = 1 ; star <= 2 * (n - i) + 1 ; star++)

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

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

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


{
cout<<j;
}
cout<<endl;
}

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;

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


{
char ch = 'A';
for (int j = 0; j <= i; j++)
{
cout<<ch<<" ";
ch++;
}
cout<<endl;
}

return 0;
}
(vi). #include<iostream>
using namespace std;

int main(){

int n;

cout<<"enter the value of n :: ";

cin>>n;

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

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

cout<<" ";

char ch = 'A';

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

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;

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


for(int k=1; k<=n-i; k=k+1){
cout<<" ";
}

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


{
cout<<" * ";
}

cout<<endl;
}

return 0;
}
(viii). #include <iostream>
using namespace std;

int main() {

int n = 5; // Number of rows

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

for (int space = i; space < n; space++) {

cout << " ";

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

cout << j << " ";

for (int k = i - 1; k >= 1; k--) {

cout << k << " ";

cout << endl;

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

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


{
for(int k=0;k<3;k++)
{
cout<<matarray[i][k];
}
cout<<endl; }
cout<<"enter second array"<<endl;
for (int i=0;i<3;i++)
{
for(int k=0;k<3;k++)
{
cin>>matarray2[i][k];
}
cout<<endl;
}

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


{
for(int k=0;k<3;k++)
{
cout<<matarray2[i][k];
}
cout<<endl;
}

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


{
for (int k=0;k<3;k++)
{
matarray3[i][k]=matarray[i][k]+matarray2[i][k];
}
}
cout<<"addition of two array"<<endl;

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


{
for(int k=0;k<3;k++)
{
cout<<matarray3[i][k];
}
cout<<endl;
}
return 0;
} OUTPUT ::
16.C++ PROGRAM TO ADD TWO COMPLEX NUMBERS ||
#include<iostream>
using namespace std;
class Complex
{
private:
int a,b;
public:
void set_value(int, int);
void show_data();
Complex add(Complex C2)
{
Complex temp;
temp.a=a+C2.a;
temp.b=b+C2.b;
return temp;
}
};
void Complex::set_value(int x, int y)
{
a=x;
b=y;
}
void Complex::show_data()
{
cout<<a<<"+"<<b<<"j";}
int main()
{
Complex C1, C2, C3;
C1.set_value(3,4);
C1.show_data();
cout<<endl;
C2.set_value(4,5);
C2.show_data();
C3=C1.add(C2);
cout<<endl;
cout<<"addition of two complex no";
C3.show_data();
return 0;
}
OUTPUT::
17.C++ PROGRAM TO CREATE 10 OBJECTS OF A STUDENT CLASS
CONTAINING THE STUDENT'S NAME, ID, SEMESTER AND CGPA AS DATA
MEMBERS, AND GETDETAILS(), SETDETAILS() AS MEMBER FUNCTIONS.
THE CLASS SHOULD ALSO CONTAIN STATIC VARIABLES WHICH KEEP
TRACK OF THE STUDENT WITH MAXIMUM CGPA IN EACH SEMESTER.
THE CLASS SHOULD ALSO CONTAIN A CONSTRUCTOR TO INITIALIZE
THE DATA MEMBERS ||

#include <iostream>

#include <string>

class Student {

private:

std::string name;

int id;

int semester;

float cgpa;

static float maxCGPA[10]; // Assuming there are 10 semesters

public:

// Constructor to initialize data members

Student(std::string name, int id, int semester, float cgpa) {

this->name = name;

this->id = id;

this->semester = semester;

this->cgpa = cgpa;

}
// Member function to set details

void setDetails(std::string name, int id, int semester, float cgpa) {

this->name = name;

this->id = id;

this->semester = semester;

this->cgpa = cgpa;

// Member function to get details

void getDetails() const {

std::cout << "Name: " << name << std::endl;

std::cout << "ID: " << id << std::endl;

std::cout << "Semester: " << semester << std::endl;

std::cout << "CGPA: " << cgpa << std::endl;

// Static function to update max CGPA for each semester

static void updateMaxCGPA(int semester, float newCGPA) {

if (newCGPA > maxCGPA[semester - 1]) {

maxCGPA[semester - 1] = newCGPA;

};

float Student::maxCGPA[10] = {0}; // Initializing maxCGPA array

int main() {
Student students[10] = {

{"John", 1, 1, 3.8},

{"Alice", 2, 2, 3.9},

// Add more students as needed

};

// Displaying student details and updating max CGPA for each semester

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

students[i].getDetails();

Student::updateMaxCGPA(students[i].semester, students[i].cgpa);

std::cout << "------------------------" << std::endl;

// Displaying maximum CGPA for each semester

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

std::cout << "Maximum CGPA for Semester " << i + 1 << ": " << Student::maxCGPA[i] << std::endl;

return 0;

}
OUTPUT :-

You might also like