Solutions
Solutions
1. Define a function which receives a number as argument and returns 1 if the number is prime
otherwise returns 0. Use this function in main function to display all prime numbers between two
numbers.
#include<stdio.h>
int isprime(int n) {
if (n % i == 0) {
int main() {
scanf("%d", &start);
scanf("%d", &end);
printf("\n");
return 0;
*/
/*
2. Write a program which asks order of two matrices and read two matrices of given order. Subtract
second matrix from first and display the resultant matrix.
#include<stdio.h>
int main()
int i, j, m, n;
scanf("%d", &m);
scanf("%d", &n);
scanf("%d", &mat1[i][j]);
//for matrix2
scanf("%d", &mat2[i][j]);
//for result
result[i][j]=mat1[i][j]-mat2[i][j];
return 0;
//3. Write a program to read a string from user and reverse it without using string related library
function.
#include<stdio.h>
#include<stdlib.h>
int i, len=0;
while(str[len]!='\0')
len++;
reversestr[i]= str[len-i-1];
reversestr[len]='\0';
}
}
int main()
scanf("%s", str);
//function call
reverse(str, reversestr);
return 0;
4. Write a program that determine the largest of three numbers using pointer.
#include<stdio.h>
*large= *n1;
*large= *n2;
}
else
*large=*n3;
int main()
return 0;
5. Write a program to read 10 numbers and reorders them in ascending order. Define separate
function for reading , sorting and displaying array elements.
#include<stdio.h>
scanf("%d", &arr[i]);
}
void sorting(int arr[], int size)
if(arr[j]>arr[j+1])
arr[j]=arr[j+1];
arr[j+1]=temp;
printf("%d", arr[i]);
printf("\n");
int main()
int numbers[10];
int size=10;
reading(numbers, size);
sorting(numbers, size);
displaying(numbers, size);
return 0;
6. Define two functions getArraynum() to read an array from user and determine the greatest
number respectively. Write main program to declare as floating point type array of size 10 and pass this
array to first function getarraynum() to read its elements and then pass the same array to another function
greatestnum() to determine the greatest number among elements of the array.
#include<stdio.h>
scanf("%f", &arr[i]);
if(arr[i]>max)
max= arr[i];
return max;
int main()
float numbers[10];
int size=10;
getarraynum(numbers, size);
return 0;
7. Create a structure named student that has name, roll, marks and remarks as members. Assume
appropriate types and size of member. Use this structure to read and display records of 4 students. Create
two functions: one is to read information of students and other is to display the information. Pass array of
structure to these functions.
#include <stdio.h>
struct student {
};
printf("Student Name\tRoll\tMarks\tRemarks\n");
}
}
int main() {
printf("Student information\n");
printf("Detail information:\n");
return 0;
8. Define a class named distance with meter and cm as private data members and appropriate
function members. Use this class to read two objects of the distance class , add them by passing these two
objects to a function members and finally display result object in main() function.
#include<iostream>
class distance
private:
int meter;
int cm;
public:
getvalue()
cin>>meter;
}; incomplete one
9. Define a class shape with dim1 and dim2 as private members. Create two constructors of the
class, one with one argument and other with two arguments. Write a main() program to define object
rectangle with two dimensions and another object square with only one dimension using appropriate
constructor and calculate their area.
*/
#include<iostream>
class shape
private:
public:
int area()
{
return dim1*dim2;
};
int main()
shape rectangle(5,6);
shape square(4);
return 0;
/*1. Design two classes, Rectangle and Circle. Each class has private members representing the
dimensions:
#include<iostream>
class circle ;
class rectangle
{
private:
float length;
float breadth;
public:
rectangle(float l, float b)
length=l;
breadth=b;
};
class circle
private:
float radius;
public:
circle(float r)
radius =r;
};
float rectarea=rect.length*rect.breadth;
int main()
rectangle rect(10.0,10.0);
circle circ(10.0);
calculatearea(rect, circ);
return 0;
2.write a program which contains a class named box with appropriate data members and function
members. Initialize an object of the class with parameterized constructor and copy this object into another
object using copy constructor.
#include<iostream>
class box
private:
public:
length=len;
breadth= br;
height= he;
length=b2.length;
breadth=b2.breadth;
height=b2.height;
void display()
cout<<"length:"<<length<<endl;
cout<<"breadth:"<<breadth<<endl;
cout<<"height:"<<height<<endl;
float volume()
return length*breadth*height;
};
int main()
float vol;
box b1(2,2,2);
box b2(b1);
b1.display();
vol= b1.volume();
b2.display();
vol= b2.volume();
return 0;
3. write a program to illustrate the use of destructor in program for destroying variables created
dynamically using new operator.
#include<iostream>
class sample
private:
int *ptr;
public:
sample(int value){
ptr=new int;
*ptr=value;
~sample()
delete ptr;
void display()
cout<<"value of ptr:"<<*ptr<<endl;
};
int main()
sample obj(42);
obj.display();
return 0;
4.create a class polygon with data members: dimension1 and dimension2 and a member
function:readdimension() to read data members. Derive two classes rectangle and polygon class with
appropriate member function to calculate area of each rectangle and triangle.
#include<iostream>
class polygon
protected:
public:
float readdimension()
cout<<"enter dimension1:"<<endl;
cin>>dimension1;
cout<<"enter dimension2:"<<endl;
cin>>dimension2;
};
public:
float area()
return dimension1*dimension2;
};
public:
float area()
return 0.5*dimension1*dimension2;
};
int main()
{
rectangle r;
triangle t;
r.readdimension();
t.readdimension();
return 0;
• A member function withdraw() to remove money from the account (with a condition that balance
should not go negative).
Write a main program to demonstrate the usage of the constructor, destructor, and member functions.
#include<iostream>
class BankAccount
private:
int accountnumber;
string accountHolderName;
float balance;
public:
accountnumber= accnum;
accountHolderName= ahname;
balance=initialbalance;
~BankAccount()
balance= balance+amount;
if(amount>balance)
{
cout<<"insufficient balance!"<<endl;
else
balance=balance-amount;
void display()
};
int main()
B.deposit(500);
B.withdraw(500);
B.display();
return 0;
Write a main program to demonstrate function overloading by calling each of these functions.
#include<iostream>
class mathoperations
public:
return a+b;
return a+b;
return a+b+c;
};
int main()
mathoperations mathop;
float result2=mathop.add(25.0f,20.0f);
cout<<"The addition of two float numbers is:"<<result2<<endl;
return 0;
8. Create a base class Shape with a virtual function area(). Derive two classes Rectangle and Circle from
Shape. Override the area() function in both derived classes to calculate:
Write a program to demonstrate the concept of function overriding and calculate the area of a rectangle
and a circle using the base class pointer.
*/
#include <iostream>
class shape {
public:
};
private:
int length;
int breadth;
public:
rectangle(int l, int b) {
length = l;
breadth = b;
};
private:
float radius;
public:
circle(float r) {
radius = r;
};
int main() {
cout << "Area of the rectangle: " << shape1->area() << endl;
cout << "Area of the circle: " << shape2->area() << endl;
delete shape1;
delete shape2;
return 0;
/*
9. Create a base class Person with private members: name and age. Derive a class Student that inherits
from Person and adds private members:
rollNumber and marks. Write a friend function that accepts a Student object as a parameter and
displays all the details (both Person
/*
#include<iostream>
class person {
private:
string name;
int age;
public:
// Constructor for person class
person(string n, int a) {
name = n;
age = a;
};
private:
int rollno;
double marks;
public:
rollno = r;
marks = m;
};
int main() {
// Create a student object and pass name, age, rollno, and marks
displaydetails(s1);
return 0;
10. Write a class called Student that includes private member variables for name, class, roll number, and
marks. Include methods to calculate the grade
#include<iostream>
class student
private:
string name;
int classs;
int rollno;
double marks;
public:
string grade()
string grade;
if (marks>80)
cout<<"Grade A+"<<endl;
else if(marks>60)
cout<<"Grade B"<<endl;
else{
cout<<"Grade c";
void display()
cout<<"enter name"<<endl;
cin>>name;
cout<<"enter class"<<endl;
cin>>classs;
cout<<"enter rollno"<<endl;
cin>>rollno;
cout<<"enter marks"<<endl;
cin>>marks;
cout<<"Detailed information of student is:"<<endl;
cout<<"Name:"<<name<<endl;
cout<<"class:"<<classs<<endl;
cout<<"rollno:"<<rollno<<endl;
cout<<"marks"<<marks<<endl;
};
int main()
student s;
s.display();
return 0;
Create a class Matrix for handling 2D matrices. Overload the + and - operators to perform matrix addition
and subtraction.
#include<iostream>
class matrix
private:
int rows;
int cols;
int mat[3][3];
public:
matrix()
rows=3;
cols=3;
mat[i][j]=0;
matrix temp;
};
*/
#include <iostream>
class Matrix {
private:
int rows, cols;
public:
Matrix() {
rows = 3;
cols = 3;
Matrix temp;
return temp;
Matrix temp;
for (int i = 0; i < rows; i++) {
return temp;
void display() {
};
int main() {
m1.display();
m2.display();
// Perform matrix addition and subtraction
sum = m1 + m2;
diff = m1 - m2;
sum.display();
diff.display();
return 0;