C++ Lab Program & Solutions
C++ Lab Program & Solutions
Input:
Enter a integer value:10
Output:
prime numbers are....1 2 3 5 7
Q4 write a c++ program to find both of largest and smallest number in a list
of integer.
#include<iostream.h> int main()
{
int a[50],i,n,small,large;
cout<<"Enter The Array Size:";
cin>>n;
cout<<"ENTER ELEMENTS OF ARRAY";
for(i=0;i<n;i++)
cin>>a[i];
small=a[0]; large=a[0]; for(i=0;i<n;i++)
{
if(a[i]<small)
small=a[i];
if(a[i]>large)
large=a[i];
}
cout<<"largest value is"<<large<<endl;
cout<<"smallest value is:"<<small<<endl;
return 0;
}
Input:
Enter The Array Size:5
ENTER ELEMENTS OF ARRAY5 4 3 2 1
Output:
largest value is5
smallest value is:1
Input:
Enter How many elements to sort:5 Enter Elements5 4 3 2 1
Output:
Sorted array is
54321
6. C++ program to store GPA of n number of students and display it where
n is the number of students entered by the user. Using new and delete
keyword for dynamic memory allocation.
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter total number of students: ";
cin >> num;
float * ptr;
return 0;
}
Output
Enter total number of students: 4
Enter GPA of students.
Student1: 3.6
Student2: 3.1
Student3: 3.9
Student4: 2.9
Displaying GPA of students.
Student1: 3.6
Student2: 3.1
Student3: 3.9
Student4: 2.9
7. write a c++ program to display names, roll numbers and grades of three
students who have appear in the examination declare the class of name, roll
number and grade .Create an array of class objects read and display the
contains of the array.
#include <iostream>
using namespace std;
class Student {
public:
string name;
int rollNumber;
char grade;
int main() {
const int numStudents = 3; // Number of students
Student students[numStudents]; // Array of objects
return 0;
}
Output
Student 1:
Enter Name: John
Enter Roll Number: 101
Enter Grade: A
Student 2:
Enter Name: Alice
Enter Roll Number: 102
Enter Grade: B
Student 3:
Enter Name: Bob
Enter Roll Number: 103
Enter Grade: A
Student Details:
Name: John Roll Number: 101 Grade: A
Name: Alice Roll Number: 102 Grade: B
Name: Bob Roll Number: 103 Grade: A
Q8 Write a c++ program to find maximum out of two numbers using friend
function.
#include <iostream>
using namespace std;
class MaxFinder {
private:
int num1, num2;
public:
MaxFinder(int a, int b) {
num1 = a;
num2 = b;
}
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
return 0;
}
Output
Enter two numbers: 10 20
Maximum number is: 20
class Class1 {
private:
int value1;
public:
Class1(int v1) { value1 = v1; }
// Friend function declaration
friend void swapValues(Class1 &obj1, Class2 &obj2);
void display() {
cout << "Class1 value: " << value1 << endl;
}
};
class Class2 {
private:
int value2;
public:
Class2(int v2) { value2 = v2; }
void display() {
cout << "Class2 value: " << value2 << endl;
}
};
int main() {
Class1 obj1(10);
Class2 obj2(20);
// Swapping values
swapValues(obj1, obj2);
return 0;
}
Output
Before Swapping:
Class1 value: 10
Class2 value: 20
After Swapping:
Class1 value: 20
Class2 value: 10
class Sample {
private:
int data;
public:
// Parameterized constructor
Sample(int value) {
data = value;
}
// Copy constructor
Sample(const Sample &obj) {
data = obj.data; // Copying data from obj to new object
}
void display() {
cout << "Data: " << data << endl;
}
};
int main() {
Sample obj1(100); // Creating an object with value 100
Sample obj2 = obj1; // Copying obj1 data to obj2 using copy constructor
Output
Original Object (obj1): Data: 100
Copied Object (obj2): Data: 100