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

C++ Lab Program & Solutions

The document contains a series of C++ programming exercises that cover various topics including finding the sum of digits, generating Fibonacci series, identifying prime numbers, finding the largest and smallest numbers in an array, sorting numbers, managing dynamic memory for student GPAs, handling student records with classes, using friend functions for data manipulation, and implementing copy constructors. Each exercise includes code snippets, input prompts, and expected output examples. The document serves as a practical guide for learning C++ programming concepts through hands-on coding tasks.

Uploaded by

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

C++ Lab Program & Solutions

The document contains a series of C++ programming exercises that cover various topics including finding the sum of digits, generating Fibonacci series, identifying prime numbers, finding the largest and smallest numbers in an array, sorting numbers, managing dynamic memory for student GPAs, handling student records with classes, using friend functions for data manipulation, and implementing copy constructors. Each exercise includes code snippets, input prompts, and expected output examples. The document serves as a practical guide for learning C++ programming concepts through hands-on coding tasks.

Uploaded by

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

C++ Lab

Q1 write a c++ program to find the sum of individual digit of a positive


integer.
#include<iostream.h>
intsum_of_digits(int n)
{
intdigit,sum=0; while(n!=0)
{
}
return sum;
}
int main()
{
digit=n%10; sum=sum+digit; n=n/10;
intnumber,digits_sum;
cout<<"Enter Positive integer within the range:"; cin>>number;
digits_sum=sum_of_digits(number);
cout<<"sum of digts of "<<number<<" is "<<digits_sum; return 0;
}
Input:
Enter Positive integer within the range:4321
Output:
sum of digits of 4321 is 10

Q2 write a c++ program to generate first N terms of fibonacci series.


#include<iostream.h> void fib(int n)
{
int f0,f1,f,count=0; f0=0;
f1=1;
while(count<n)
{
cout<<f0<<"\t"; count++; f=f0+f1;
f0=f1; f1=f;
}
}
int main()
{
int terms;
cout<<"Enter How many terms to be printed:"; cin>>terms;
fib(terms); return 0;
}
Input:
Enter How many terms to be printed:10
Output:
0 1 1 2 3 5 8 13 21 34
w
Q3 write a c++ program to generate all prime members between 1 and n
where n is a value given by user.
#include<iostream.h>
void prime(int n)
{
int factors;
cout<<"prime numbers are... ";
for(int i=1;i<=n;i++)
{ factors=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
factors=factors+1;
}
if(factors<=2) cout<<i<<"\t";
}
}
int main()
{
int n;
cout<<"Enter a integer value:"; cin>>n;
prime(n); return 0;
}

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

Q5 write a c++ program to short a list of numbers in ascending orders.


#include<iostream.h>
void sort(int data[],int n)
{
for(int i=0;i<n;i++)// read the elements of an array
for(int j=0;j<n-i-1;j++)
{
int t; if(data[j]>data[j+1])
{
t=data[j]; data[j]=data[j+1]; data[j+1]=t;
}
}
}
int main()
{
int a[50],i,n;
cout<<"Enter How many elements to sort:"; cin>>n;
cout<<"Enter Elements:";
for(i=0;i<n;i++) // read the elements of an array
cin>>a[i];
sort(a,n)
cout<<"Sorted array is \n"; for(i=0;i<n;i++) cout<<a[i]<<"\t";
return 0;
}

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;

// memory allocation of num number of floats


ptr = new float[num];

cout << "Enter GPA of students." << endl;


for (int i = 0; i < num; ++i) {
cout << "Student" << i + 1 << ": ";
cin >> *(ptr + i);
}

cout << "\nDisplaying GPA of students." << endl;


for (int i = 0; i < num; ++i) {
cout << "Student" << i + 1 << ": " << *(ptr + i) << endl;
}

// ptr memory is released


delete[] ptr;
ptr = nullptr;

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;

// Function to read student details


void readData() {
cout << "Enter Name: ";
cin >> name;
cout << "Enter Roll Number: ";
cin >> rollNumber;
cout << "Enter Grade: ";
cin >> grade;
}

// Function to display student details


void displayData() {
cout << "Name: " << name << "\tRoll Number: " << rollNumber <<
"\tGrade: " << grade << endl;
}
};

int main() {
const int numStudents = 3; // Number of students
Student students[numStudents]; // Array of objects

// Reading student details


cout << "Enter details for " << numStudents << " students:" << endl;
for (int i = 0; i < numStudents; i++) {
cout << "\nStudent " << i + 1 << ":\n";
students[i].readData();
}

// Displaying student details


cout << "\nStudent Details:\n";
for (int i = 0; i < numStudents; i++) {
students[i].displayData();
}

return 0;
}

Output

Enter details for 3 students:

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

// Friend function declaration


friend int findMax(MaxFinder obj);
};

// Friend function definition


int findMax(MaxFinder obj) {
return (obj.num1 > obj.num2) ? obj.num1 : obj.num2;
}

int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;

MaxFinder obj(a, b);


cout << "Maximum number is: " << findMax(obj) << endl;

return 0;
}

Output
Enter two numbers: 10 20
Maximum number is: 20

Q9 Write a program to swap private data members of classes named as class-


1,class-2 using friend function.
#include <iostream>
using namespace std;

class Class2; // Forward declaration

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

// Friend function declaration


friend void swapValues(Class1 &obj1, Class2 &obj2);

void display() {
cout << "Class2 value: " << value2 << endl;
}
};

// Friend function definition


void swapValues(Class1 &obj1, Class2 &obj2) {
int temp = obj1.value1;
obj1.value1 = obj2.value2;
obj2.value2 = temp;
}

int main() {
Class1 obj1(10);
Class2 obj2(20);

cout << "Before Swapping:" << endl;


obj1.display();
obj2.display();

// Swapping values
swapValues(obj1, obj2);

cout << "\nAfter Swapping:" << endl;


obj1.display();
obj2.display();

return 0;
}

Output
Before Swapping:
Class1 value: 10
Class2 value: 20

After Swapping:
Class1 value: 20
Class2 value: 10

Q10 Write a program using copy constructors to copy data of an object to


another object.
#include <iostream>
using namespace std;

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

cout << "Original Object (obj1): ";


obj1.display();

cout << "Copied Object (obj2): ";


obj2.display();
return 0;
}

Output
Original Object (obj1): Data: 100
Copied Object (obj2): Data: 100

You might also like