0% found this document useful (0 votes)
10 views19 pages

Yash Kushwaha Pratical PC New

The document contains a series of C and C++ programming exercises authored by Yash Kushwaha for BCA 1st (E) students. Each program aims to demonstrate specific programming concepts such as control statements, arrays, pointers, classes, inheritance, operator overloading, constructors, and destructors. The document includes code snippets along with their expected outputs for each program.

Uploaded by

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

Yash Kushwaha Pratical PC New

The document contains a series of C and C++ programming exercises authored by Yash Kushwaha for BCA 1st (E) students. Each program aims to demonstrate specific programming concepts such as control statements, arrays, pointers, classes, inheritance, operator overloading, constructors, and destructors. The document includes code snippets along with their expected outputs for each program.

Uploaded by

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

Program No.

11
Name: Yash Kushwaha class :BCA 1st (E)
Aim: Write a C Program to understand the use of continue and break statement
BREAK STATEMENT:
The break statement can also be used to jump out of a loop. This example jumps
out of the loop when i is equal to 4:
#include<stdio.h>
#include <conio.h>
void main()
{
int i;
clrscr( );
for (i = 0; i < 10; i++)
{
if (i == 4)
{
break;
}
printf("%d\n", i);
getch( );
}
CONTINUE STATEMENT:
The continue statement breaks one iteration (in the loop), if a specified condition
occurs, and continues with the next iteration in the loop. This example skips the
value of 4
#include<stdio.h>
#include <conio.h>
void main( )
{
int i;
clrscr( );
for (i = 0; i < 10; i++)
{
if (i == 4)
{
continue;
}
printf("%d\n", i);
getch( );
}
Name: Yash Kushwaha class :BCA 1st (E)
PROGRAM NO. 12
Aim: Write a C Program to input values into an array and display them.
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5],i;
clrscr( );
for(i=0; i<5; i++)
{
printf("Enter the value for arr[%d] : ",i);
scanf("%d", &arr[i]);
}
printf("The array elements are : \n");

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


{
printf("%d\t", arr[i]);
printf("\n");
}
getch( );
}
OUTPUT :-
Enter the value for arr[0] : 5
Enter the value for arr[1] : 3
Enter the value for arr[2] : 8
Enter the value for arr[3] : 6
Enter the value for arr[4] : 1
The array elements are: 5 3 8 6 1
Name: Yash Kushwaha class :BCA 1st (E)
PROGRAM NO. 13
Aim: Write a C Program to print address and value using pointer
Program
#include <stdio.h>
int main()
{
int* pc, c;
c = 22;
printf("Address of c: %p \n", &c);
printf("Value of c: %d \n\n", c); // 22
pc = &c;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 22
return 0;
}
Output:
Address of c : 0x7ffd46f0a69c
Value of c : 22
Address of pointer pc : 0x7ffd46f0a69c
Content of pointer pc : 22

Name: Yash Kushwaha class :BCA 1st (E)


PROGRAM NO.14
Aim: Write a C Program to display Fibonacci Series.
#include <stdio.h>
#include <conio.h>
void main()
{
int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
printf("Enter the number of terms: ");
scanf("%d", &n);
// print the first two terms t1 and t2
printf("Fibonacci Series: %d, %d, ", t1, t2);
// print 3rd to nth terms
for (i = 3; i <= n; ++i)
{
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
getch ( );
}

OUTPUT :-
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Name: Yash Kushwaha class :BCA 1st (E)


PROGRAM NO. 15
Aim: Write a C Program to sort an array
#include <stdio.h>
void main ()
{
int num[20];
int i, j, a, n;
printf ("enter number of elements in an array\n");
scanf ("%d", &n);
printf ("Enter the elements\n");
for (i = 0; i < n; ++i)
scanf("%d", &num[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (num[i] > num[j])
{
a = num[i];
num[i] = num[j];
num[j] = a;
}
}
}
printf ("The numbers in ascending order is:\n");
for (i = 0; i < n; ++i)
{
printf("%d\n", num[i]);
}
getch();
}
Output:
enter number of elements in an array
5
Enter the elements
12
23
89
11
22
The numbers in ascending order is:
11
12
22
23
89
Name: Yash Kushwaha class :BCA 1st (E)
PROGRAM NO.16
Aim: Write a C++ Program to Print Number Entered by User
Program:
#include <iostream>
using namespace std;
void main()
{
int number;
cout << "Enter an integer: ";
cin >> number;
cout << "You entered " << number;
getch();
}
Output:
Enter an integer: 32
You entered 32
Name: Yash Kushwaha class :BCA 1st (E) PROGRAM NO . 17
Aim: Write a C++ Program to create a simple class and object.
Program:
#include<iostream>
using namespace std;
class Hello
{
public:
void sayHello()
{
cout << "Hello World" << endl;
}
};
int main()
{
Hello h;
h.sayHello();
return 0;
}
Output:
Hello World

Name: Yash Kushwaha class :BCA 1st (E) PROGRAM NO. 18


Aim: Write a C++ Program using access modifiers.
#include <iostream>
using namespace std;
class Sample
{
public:
int age;
void displayAge() {
cout << "Age = " << age << endl;
}
};
int main()
{
Sample obj1;
cout << "Enter your age: ";
cin >> obj1.age;
obj1.displayAge();
return 0;
}
Output
Enter your age: 20
Age = 20
Name: Yash Kushwaha class :BCA 1st (E) PROGRAM NO. 19
Aim: Write a C++ Program to Implement Single level Inheritance.
C++ Program
#include <iostream>
using namespace std;
class Account
{
public:
float salary = 60000;
};
class Programmer: public Account
{
public:
float bonus = 5000;
};
void main( )
{
Programmer p1;
cout<<" Salary: "<< p1.salary << endl;
cout<<" Bonus: "<< p1.bonus << endl;
getch ( );
}
OUTPUT :-

Salary: 60000
Bonus: 5000
Name: Yash Kushwaha class :BCA 1st (E)
PROGRAM NO. 20
Aim: Write a C++ Program to Implement Multi level Inheritance.
C++ Multilevel Inheritance
In C++ programming, not only you can derive a class from the base class but you
can
also derive a class from the derived class. This form of inheritance is known as
multilevel
inheritance.
class A {
... .. ...
};
class B: public A {
... .. ...
};
class C: public B {
... ... ...
};
Here, class B is derived from the base class A and the class C is derived from the
derived
class B.
C++ Program
#include <iostream>
using namespace std;
class A
{
public:
void display()
{
cout<< " Base class content.";
}
};
class B : public A
{
};
class C : public B
{
};
void main( )
{
C obj;
obj.display( );
getch( );
}
OUTPUT :-
Base class content.

Name: Yash Kushwaha class :BCA 1st (E) PROGRAM NO. 21


Aim: Write a C++ Program to implement Unary Operator overloading.
class Count {
private:
int value;
public:
// Constructor to initialize count to 5
Count() : value(5) {}
// Overload ++ when used as prefix
void operator ++ () {
++value;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
// Call the "void operator ++ ()" function
++count1;
count1.display();
return 0;
}
Output:
Count: 6

Name: Yash Kushwaha class :BCA 1st (E)


PROGRAM NO. 22
Aim: Write a C++ Program to implement a Constructor.
Program:
// C++ program to demonstrate the use of default constructor
#include <iostream>
using namespace std;
// declare a class
class Wall {
private:
double length;
public:
// default constructor to initialize variable
Wall() {
length = 5.5;
cout << "Creating a wall." << endl;
cout << "Length = " << length << endl;
}
};
int main() {
Wall wall1;
return 0;
}
Output:
Creating a Wall

Length = 5.5

Name: Yash Kushwaha class :BCA 1st (E)


PROGRAM NO. 23
Aim: Write a C++ Program to implement a Destructor.
Program:
#include <iostream>
using namespace std;
class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor declaration
~Line(); // This is the destructor: declaration
private:
double length;
};
// Member functions definitions including constructor
Line::Line(void) {
cout << "Object is being created" << endl;
}
Line::~Line(void) {
cout << "Object is being deleted" << endl;
}
void Line::setLength( double len ) {
length = len;
}
double Line::getLength( void ) {
return length;
}
// Main function for the program
void main()
{
Line line;
// set line length
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
getch();
}
Output:
Object is being created
Length of line : 6
Object is being delete

You might also like