Programming in ‘C/C++’ Bsc-I (2023-24)
An
Assignment Report
On
Programming in ‘C/C++’
Submitted in partial fulfillment of the requirements for the award
of Bachelor
Bachelor of Science
(BSC-I)
From
Hemchand yadav Vishwavidyalaya (Durg) (C.G.)
Year: 2023-2024
Guided By Submitted by:
Mr. Rajnish Kumar Rahul Verma
Submitted to
Sai College (Durg) (C.G.)
Street-69, Sec-6, E Market, Bhilai
Rahul Verma Page 1
Programming in ‘C/C++’ Bsc-I (2023-24)
CERTIFICATE OF APPROVAL
This is to certify that the assignment work entitled “Programming in ‘C/C++’
” is carried out by “Rahul Verma” , a student of BSC-I at “Sai College
Sec-6 Bhilai” is here by approved as a credible work in the discipline of computer
science & information technology for the award of degree of Bachelor of Science
during the year 2023-2024 from Hemchand yadav
Vishwavidyalaya (Durg).
Dr. Mrs. Mamta Singh
H.O.D. (CS)
Page 2
Programming in ‘C/C++’ Bsc-I (2023-24)
CERTIFICATE OF GUIDE
This is to certify that the assignment work entitled “Programming in
‘C/C++’” submitted to the “Sai College Sec-6 Bhilai” by “ Rahul Verma ” roll
no. ____________ in partial fulfillment for the requirements relating to nature &
standard of the award of degree of “ Bachelor of Science” diploma
by “Hemchand yadav Vishwavidyalaya (Durg)” for the academic
year 2023-2024.
This project work has been carried out under my guidance.
Mr. Rajnish Kumar
…………………………
Page 3
Programming in ‘C/C++’ Bsc-I (2023-24)
DECLARATION/SELF CERTIFICATE
This is to certify that the assignment work entitled “Programming in
‘C/C++’ ” which is submitted by me in the partial fulfillment for the award of
degree of “ Bachelor of Science” “Sai College Sec-6 Bhilai”
comprises the original work carried out by me.
I further declare that the work reported in the assignment has not been
submitted & will not be submitted, either in part or in full award of any other degree
in this institute or any other institute or university.
PLACE: -Bhilai NAME:- Rahul Verma
DATE: - ……/……/……….. ROLL NO:-
Page 4
Programming in ‘C/C++’ Bsc-I (2023-24)
ACKNOWLEDGEMENT
We are very delighted on the accomplishment to the assignment in visual basic, which
was very educational and practically beneficial. It is due to his encouragement and persistent
motivation that i could extent the scope of project to much useful data processing report.
We have specially thankful to Mr. Rajnish Ku. Sai College Sec-6 Bhilai who helps us
by his deep knowledge and practical experience in computer science, rendered all possible
in fulfillment of the project and for explaining the method of approach. So system
development is the pooling of talents, extends help and cooperation above as a team effort.
Page 5
Programming in ‘C/C++’ Bsc-I (2023-24)
INDEX
S.NO OBJECTS OF PRACTICALS DATE PG.NO SIGN.
1 Write a program in c/c++ for addition of two numbers using float data
type .
2 Write a program in c/c++ to find the biggest number between two
numbers.
3 Write a program in c/c++ to find the factorial value of any entered
number using while loop.
4 Write a program in c/c++ for various arithmetic operation using switch
statements
5 Write a program in c/c++ for multiplication of two 3X3 matrix.
6 Write a program in c/c++ to store five books information using structure.
7 Write a program in c/c++ to store six employee information using union.
8 Write a program in c/c++ to calculate simple interest using call by
value and call by reference method.
9 Write a program in c/c++ for swapping of two numbers using pointer.
10 Write a program in c/c++ to make a text file using file handling.
11
Write a program to count word,space and lines in a text file.
12 Write a program to demonstrate work of calloc().
13 Write a program to demonstrate work of malloc(),realloc() and free().
14 Write a program in C++ to find the sum and average of five numbers
using class and objects.
15 Write a program in C++ to multiply two numbers using private and
public member functions.
16 Write a program in C++ to print structure like this using scope
resolution operator
1
12
123
1234
12345
17 Write a program in C++ for constructor and Destructor.
Page 6
Programming in ‘C/C++’ Bsc-I (2023-24)
18 Write a program in C++ for multiple inheritance.
19 Write a program in C++ for operator overloading.
20 Write a program in C++ for friend class and friend function.
21 Write a program in C++ for virtual function and virtual class.
22 Write a program in C++ for Exception handling.
23 Write a program in C++ to open and close a file using file Handling.
24 Given two ordered arrays of integer, write a program to merge the two-
arrays to get an ordered array.
25 Write a program to display Fibonacci series (i) using recursion,
(ii) using iteration.
Page 7
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 1
WAP in c/c++ for addition of two numbers using float
data type .
Source code :
#include<stdio.h>
#include<conio.h>
void main()
float a,b,sum;
Clrscr();
printf(“Enter first number: “);
Scanf(“%f”,&a);
printf(“Enter second number: “);
scanf(“%f”,&b);
Sum=a+b;
printf(“Sum of two numbers = %f” , sum);
getch();
Output:
Page 8
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 2
WAP in c to find out greatest number among three numbers.
Source code :
#include<stdio.h>
#include<conio.h>
void main()
int a,b;
a=10,b=14
if(a==b)
printf("both are equal");
else if(a>b)
printf("%d is greater",a);
else
printf("%d is greater",b);
return 0;
Output:
Page 9
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 3
Write a program in c/c++ to find the factorial
value of any entered number using while loop.
Source code :
#include<iostream.h>
#include<conio.h>
void main()
int a, f=1,i=1;
Clrscr();
Cout<<” Enter The Number : “;
Cin>>a;
While(i<=a)
{
f=f*I;
i++;
}
Cout<<”The Factorial of “<<a<<” is “<<f;
getch();
Output:
Page 10
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 4
Write a program in c/c++ for various arithmetic operation
using switch statements.
Source code :
#include<stdio.h>
#include<conio.h>
void main()
int a,b,op;
Clrscr();
Printf(“Enter 1st number:”);
Scanf(“%d”,&a);
Printf(“Enter 2nd number:”);
Scanf(“%d”,&b);
Printf(“Enter 1 for + :\n”);
Printf(“Enter 2 for - :\n”);
Printf(“Enter 3 for * :\n”);
Printf(“Enter 4 for / :\n”);
Scanf(“%d”,&op);
switch()
Case 1:
Printf(“%d”,a+b);
break ;
Case 2:
Printf(“%d”,a-b);
break ;
Case 3:
Printf(“%d”,a*b);
Page 11
Programming in ‘C/C++’ Bsc-I (2023-24)
break ;
Case 4:
Printf(“%d”,a/b);
break ;
getch() ;
Output:
Page 12
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 5
Write a program in c/c++ for multiplication of two 3X3 matrix.
Source code :
#include<stdio.h>
#include<conio.h>
void main()
int a[3][3], b[3][3],x[3][3];
int r,c,i;
Clrscr();
Printf(“Enter element of first array \n);
for(r=0;r<3;r++)
for(c=0;c<3;c++)
Printf(“Enter element of : a[%d][%d]”,r,c);
Scanf(“%d”,&a[r][c] );
}}
Printf(“Enter element of second array \n”);
for(r=0;r<3;r++)
for(c=0;c<3;c++)
Printf(“Enter element of : a[%d][%d]”,r,c);
Scanf(“%d”,&b[r][c] );
}Printf((“\nFirst Matrix is \n”);
for(r=0;r<3;r++)
for(c=0;c<3;c++)
Printf(“%d\t”,a[r][c] );
Page 13
Programming in ‘C/C++’ Bsc-I (2023-24)
}
}Printf(“\n Second matrix is \n “);
for(r=0;r<3;r++)
for(c=0;c<3;c++)
x[r][c]=0;
for(i=0;i<3;i++)
x[r][c]=x[r][c]+(a[r][c]*b[i][c] );
}}
Printf(“\n Third Matrix is \n”);
for(r=0;r<3;r++)
for(c=0;c<3;c++)
Printf(“%d\t”, x[r][c] );
getch();
}
Output :-
Page 14
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 6
Write a program in c/c++ to store five books
information using structure
Source code :
#include<stdio.h>
#include<conio.h>
Struct book
{int book id;
char book name[10];
char author[10];
int rate;
};
void main( );
book b[5];
int i;
clrscr( );
printf(“%d\n”,size of(b[0]);
for(i=0;i<5;i++)
printf(“Enter book id:”);
scanf(“%d”,&b[i].book id);
Page 15
Programming in ‘C/C++’ Bsc-I (2023-24)
printf(“Enter book name:”);
scanf(“%s”,&b[i].book name);
printf(“Enter author name:”);
scanf(“%s”,&b[i].author);
printf(“Enter book rate”);
scanf(“%d”,&b[i].rate);
clrscr();
printf(“ID/t NAME/t AUTHOR/t
RATE/n”);
for(i=0;i<5;i++)
printf(“%d\t %s\t %s\t %d\n”,
b[i]. book id , b[i]. book name, b[i].
author, b[i]. rate);
Output:
Page 16
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 7
Write a program in c/c++ to store six employee
information using union.
Source code :
#include<stdio.h>
#include<conio.h>
union emp
int empid();
char empname[10];
char dept[10];
struct Date
{
int dd;
int mm;
int yy;
char sp;
}doj;
int salary;
}employee;
void main( )
{
emp e[6];
int i;
clrscr();
for(i=0;i<6;i++)
{
printf("Enter emp id :");
scanf("%d",e[i].empid);
printf("Emp id = %d",e[i].empid);
printf("Enter emp name :");
scanf("%s",e[i].empname);
printf("Emp id = %s",e[i].empname);
printf("Enter dept :");
scanf("%s",e[i].dept);
printf("Emp id = %s",e[i].dept);
printf("Enter date of Joining dd mm yy :");
scanf("%d%d%d",&e[i].doj.dd,&e[i].doj.mm,&e[i].doj.yy);
e[i].doj.sp='-';
printf("Emp id DOJ = %d%c%d%c%d",e[i].doj.dd, e[i].doj.sp, e[i].doj.mm, e[i].doj.sp, e[i].doj.yy);
Page 17
Programming in ‘C/C++’ Bsc-I (2023-24)
printf("Enter emp Salary :");
scanf("%d",e[i].salary);
printf("Emp Salary = %d",e[i].salary);
}
getch( );
}
Output:
Page 18
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 8
Write a program in c/c++ to calculate simple
interest using call by value and call by
reference method.
. Source code :
#include<iostream.h>
#include<conio.h>
int main()
{
float p,r,t,si;
cout<<"Enter Principle Amount: ";
cin>>p;
cout<<Enter Rate of Interest: ";
cin>.r;
cout<<"Enter Time Period : ";
cin>>t;
si = (p*r*t)/100;
cout<,"Simple Intersr Amount: "<<si;
cout<<endl;
return 0;
}
Output:-
Page 19
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 9
Write a program in c/c++ for swapping of two
numbers using pointer
Source code :
#include <iostream.h>
#include<conio.h>
using namespace std;
int main()
{ int x,y; // Input any two numbers from the user.
cout << "Enter the numbers:\n";
cin>>x>>y;
int *num_1,*num_2,temp; //Declaring pointers
num_1=&x; // Declaring address
num_2=&y;
temp=*num_1; //Swap procedure starts
*num_1=*num_2;
*num_2=temp;
cout<<"Numbers after swapping:";
cout<<"\nfirst number="<<x;
cout<<"\nsecond number="<<y;
return 0;
}
Output:-
Page 20
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 10
Write a program in c/c++ to make a text file using file handling.
Source code :
#include< stdio.h >
#include<conio.h>
int main()
FILE *fp;
char fName[20];
printf("Enter file name to
create :");
scanf("%s",fName);
fp=fopen(fName,"w");
if(fp==NULL)
printf("File does not
created!!!");
printf("File created
successfully.");
Page 21
Programming in ‘C/C++’ Bsc-I (2023-24)
return 0;
Output:-
Page 22
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 11
Write a program to count word,space and lines in a text file.
Source code :
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *fp;
char ch, fname[30];
int no.ofChar=0, no.ofSpace=0, no.ofTab=0, no.ofNewline=0;
printf("Enter file name with extension: ");
gets(fname);
fp = fopen(fname, "r");
while(fp)
{
ch = fgetc(fp);
if(ch==EOF)
break;
no.ofChar++;
if(ch==' ')
no.ofSpace++;
if(ch=='\t')
no.ofTab++;
if(ch=='\n')
no.ofNewline++;
}
fclose(fp);
printf("\nNumber of characters = %d", no.ofChar);
printf("\nNumber of spaces = %d", no.ofSpace);
printf("\nNumber of tabs = %d", no.ofTab);
printf("\nNumber of newline = %d", no.ofNewline);
getch();
return 0;
}
Output:-
Page 23
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 12
Write a program to demonstrate work of calloc( ).
Source code :
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int n, *ptr, *p, i, sum = 0;
printf (" Enter the number of elements: ");
scanf (" %d", &n);
ptr = (int *) calloc (n, sizeof(int));
p = ptr;
if (ptr == NULL)
{
printf (" Memory is not allocated. ");
exit(0);
}
printf (" Enter %d numbers \n", n);
for ( i = 1; i <= n; i++)
{
scanf ( "%d", ptr);
sum = sum + *ptr;
ptr++;
}
printf (" Elements are: ");
for (i = 1; i <= n; i++)
{
printf (" %d", *p);
p++;
}
printf (" \n The addition of the elements is: %d ", sum);
getch();
Page 24
Programming in ‘C/C++’ Bsc-I (2023-24)
}
Output:
Page 25
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 13
Write a program to demonstrate work of malloc(),realloc()
and free().
Source code :
#include <stdio.h>
#include <stdlib.h>
int main()
int* ptr;
int n, i;
printf("Enter number of
elements:");
scanf("%d",&n);
printf("Entered number of
elements: %d\n", n);
ptr = (int*)malloc(n *
sizeof(int));
if (ptr == NULL) {
printf("Memory not
allocated.\n");
exit(0);
else {
printf("Memory successfully
allocated using malloc.\n");
Page 26
Programming in ‘C/C++’ Bsc-I (2023-24)
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
printf("The elements
of the array are: ");
for (i = 0; i < n; ++i)
printf("%d, ",
ptr[i]);
return 0;
Output:
Page 27
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 14
Write a program in C++ to find the sum and average of five
numbers using class and objects.
Source code :
#include<iostream.h>
#include<conio.h>
{
private:
int a[5];
int sum;
float avg;
public:
void get_data( )
{
int i;
for(i=0;i<5;i++)
{
cout<<"Enter a number :";
cin>>a[i];
}
}
void calculate_sum( )
{
int i;
sum=0;
for(i=0;i<5;i++)
{
sum=sum+a[i];
}
cout<<endl<<"Sum = "<<sum;
}
void calculate_avg( )
{
avg=sum/5.0;
cout<<endl<<"Avg = "<<avg;
}
};
void main( )
{
clrscr( );
Num n;
n.get_data( );
n.calculate_sum( );
Page 28
Programming in ‘C/C++’ Bsc-I (2023-24)
n.calculate_avg( );
getch( );
}
Output:-
Page 29
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 15
Write a program in C++ to multiply two
numbers using private and public member
functions.
Source code :
#include <iostream>
#include<conio.h>
class Addition
{
private:
int num1, num2, sum;
public:
void input() {
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
}
void calculate() {
sum = num1 + num2;
}
void output() {
std::cout << "The sum of " << num1 << " and " << num2 << " is " << sum << std::endl;
}
};
int main() {
Addition add;
add.input();
add.calculate();
add.output();
return 0;
}
Output:
Page 30
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 16
Write a program in C++ to print structure like this using scope
resolution operator .
1
12
123
1234
12345
Source code :
#include<iostream.h>
#include<conio.h>
int main()
{
int i,j,a;
cout<<"\nEnter the value:";
cin>>a;
for(i=1;i<=a;i++)
{
cout<<"endl";
for(j=1;j<=i;j++)
{
cout<<j;
}
}
return 0;
}
Output:
Page 31
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 17
Write a program in C++ for constructor and
Destructor.
Source code :
#include <iostream>
#include<conio.h>
class Department
{
public:
Department()
{
cout << "Constructor Invoked for Department class" << endl;
}
~Department()
{
cout << "Destructor Invoked for Department class" << endl;
}
};
class Employee {
public:
Employee()
{
cout << "Constructor Invoked for Employee class" << endl;
}
~Employee()
{
cout << "Destructor Invoked for Employee class" << endl;
}
};
int main(void)
{
.Department d1;
Employee e2;
return 0;
}
Output:
Page 32
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 18
Write a program in C++ for multiple
inheritance.
Source code :
#include<iostream.h>
#include<conio.h>
Class A
{
public:
void show( ){
cout<<"OK BYE";}
};
class B : public A
{};
class C : public B
{};
class D : public C
{};
void main( )
{
clrscr( );
D d1;
d1.show( );
getch( ); }
Output:
Page 33
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 19
Write a program in C++ for operator overloading.
Source code :
#include <iostream>
#include<conio.h>
class Count
{
private:
int value;
public:
Count() : value(5) {}
void operator ++ ()
{
++value;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
++count1;
count1.display();
return 0;
}
Output:
Page 34
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 20(I)
Write a program in C++ for friend class.
Source code :
#include <iostream.h>
#include<conio.h>
class AM {
private:
int private_variable;
protected:
int protected_variable;
public:
AM()
private_variable =10;
protected_variable = 99;
friend class F;
};
class F {
public:
void display(GFG& t)
Page 35
Programming in ‘C/C++’ Bsc-I (2023-24)
{
cout << "The value of Private
Variable = "<< t.private_variable << endl;
cout << "The value of Protected
Variable = "<< t.protected_variable;
};
int main()
AM g;
F fri;
fri.display(g);
return 0;
getch( );
Output:
Page 36
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 20(ii)
Write a program in C++ for friend Function .
Source code :
#include <iostream>
#include<conio.h>
class base {
private:
int private_variable;
protected:
int protected_variable;
public:
base()
{
private_variable = 10;
protected_variable = 99;
}
friend void friendFunction(base& obj);
};
void friendFunction(base& obj)
{
cout << "Private Variable: " << obj.private_variable
<< endl;
cout << "Protected Variable: " << obj.protected_variable;
}
int main()
{
base object1;
friendFunction(object1);
return 0;
}
Output:-
Page 37
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 21(I)
Write a program in C++ for virtual function.
Source code :
#include <iostream>
#include<conio.h>
class Base
{
public:
virtual void print() {
cout << "Base Function" << endl;
}
};
class Derived : public Base {
public:
void print()
{
cout << "Derived Function" << endl;
}
};
int main()
{
Derived derived1;
Base* base1 = &derived1;
base1->print();
return 0;
}
Output:-
Page 38
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 22
Write a program in C++ for Exception
handling.
Source code :
#include <iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr( );
cout << "Enter 1st no: ";
cin >> a;
cout << "Enter 2nd no: ";
cin >> b;
if(b==0)
{
throw runtime_error(Div By Zero\n");
}
try{
c=a/2;
}
catch(runtime_error &e)
{
cout<<"Infinite";
}
Output:-
Page 39
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 23
Write a program in C++ to open and close a
file using file Handling.
Source code :
#include<iostream.h>
#include <fstream.h>
#include <conio.h>>
k
int main()
{
fstream new_file;
new_file.open("new_file",ios::out);
if(!new_file)
{
cout<<"File creation failed";
}
else
{
cout<<"New file created";
new_file.close();
}
return 0;
}
Output:-
Page 40
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 24
Given two ordered arrays of integer, write a
program to merge the two-arrays to get an
ordered array.
Source code :
#include <stdio.h>
int main()
int n1,n2,n3;
int a[100], b[100], c[200];
printf("Enter the number of elements of
First Array: ");
scanf("%d",&n1);
printf("Enter the elements of First
Array:");
for(int i = 0; i < n1; i++)
scanf("%d", &a[i]);
printf("Enter the number of elements
of Second Array: ");
scanf("%d",&n2);
printf("Enter the elements of second
Array: ");
for(int i = 0; i < n2; i++)
scanf("%d", &b[i]);
n3 = n1 + n2;
Page 41
Programming in ‘C/C++’ Bsc-I (2023-24)
for(int i = 0; i < n1; i++)
c[i] = a[i];
for(int i = 0; i < n2; i++)
c[i + n1] = b[i];
printf("Array after merging: ");
for(int i = 0; i < n3; i++)
printf("%d ", c[i]);
return 0;
Output:-
Page 42
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 25(I)
Write a program to display Fibonacci series
(i) using recursion,
(ii) using iteration.
Source code :
#include<stdio.h>
#include<conio.h>
int fibonacci(int);
int main()
{
int n, i;
printf("Enter the number of element you want in series :");
scanf("%d",&n);
printf("fibonacci series is : \n");
for(i=0;i<n;i++) {
printf("%d \n",fibonacci(i));
}
getch();
}
int fibonacci(int i)
{
if(i==0) return 0;
else if(i==1) return 1;
else return (fibonacci(i-1)+fibonacci(i-2));
}
Output:-
Page 43
Programming in ‘C/C++’ Bsc-I (2023-24)
Program- 25(ii)
Source code :
#include <stdio.h>
#include <conio.h>
int main()
int n, first = 0,
second = 1, result, i;
printf("Please give an
input upto you want to
print series :");
scanf("%d", &n);
printf("Fibonacci
Series is: \n");
for (i = 0; i < n; i++)
{ if (i <= 1)
result = i;
else
Page 44
Programming in ‘C/C++’ Bsc-I (2023-24)
result = first +
second;
first = second;
second = result;
printf("%d \n",
result);
return 0;
Output:
Page 45