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

OOPS Practical File - Nilesh-1

The document contains summaries of 12 programming practicals involving basic C++ concepts like conditional statements, loops, functions, pointers, call by value vs call by reference. The practicals include programs to check even/odd numbers, find the greatest of three numbers, calculate sum of digits, generate Fibonacci series, print multiplication tables, calculate factorials, perform math operations using switch statement, check prime numbers, print patterns, and demonstrate call by value and call by reference using function pointers.

Uploaded by

mallmridul3
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)
19 views19 pages

OOPS Practical File - Nilesh-1

The document contains summaries of 12 programming practicals involving basic C++ concepts like conditional statements, loops, functions, pointers, call by value vs call by reference. The practicals include programs to check even/odd numbers, find the greatest of three numbers, calculate sum of digits, generate Fibonacci series, print multiplication tables, calculate factorials, perform math operations using switch statement, check prime numbers, print patterns, and demonstrate call by value and call by reference using function pointers.

Uploaded by

mallmridul3
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/ 19

1|Page Object-Oriented Programming using C++

Practical 1: Write a program to check whether a given number is ‘Even’ or


‘Odd’.

Syntax:

#include<iostream>
using namespace std;

int main(){

int num;

// Input of Value
cout<<"Enter The Value: ";
cin>>num;

// Checking either the Value is 'Odd' or 'Even'


if(num%2==0){
cout<<"The Value '"<<num<<"' is Even";
}
else{
cout<<"The Value '"<<num<<"' is Odd";
}

return 0;
}

Output:

Submitted by:- Nilesh Tamboli (3rd Sem) Submitted to:- Mrs. Pooja Rathore
2|Page Object-Oriented Programming using C++

Practical 2: Write a program to find the greatest of the three numbers using
conditional operator.

Syntax:

#include<iostream>
using namespace std;

int main(){

int a, b, c;

// Input The Values


cout<<"Enter The First Value: ";
cin>>a;
cout<<"Enter The Second Value: ";
cin>>b;
cout<<"Enter The Third Value: ";
cin>>c;

// Checking The Values for the Greatest


if(a>b && a>c)
cout<<"The First Value is Bigger i.e., "<<a<<endl;
else if(b>a && b>c)
cout<<"The Second Value is Bigger i.e., "<<b<<endl;
else
cout<<"The Third Value is Bigger i.e., "<<c<<endl;

return 0;
}

Output:

Submitted by:- Nilesh Tamboli (3rd Sem) Submitted to:- Mrs. Pooja Rathore
3|Page Object-Oriented Programming using C++

Practical 3: Write a Program to Find the Sum of Digits of a Number.

Syntax:

#include<iostream>
using namespace std;

int main(){

int n, m=0, num=0;

// Input The Value


cout<<"Enter A Number: ";
cin>>n;

// Using While Loop to calculate the value using reminders and


removing last digit every cycle
while(n>0){
m=n%10;
num+=m;
n=n/10;
}

// Output The Total


cout<<"The Sum of Digits in number is "<<num;

return 0;
}

Output:

Submitted by:- Nilesh Tamboli (3rd Sem) Submitted to:- Mrs. Pooja Rathore
4|Page Object-Oriented Programming using C++

Practical 4: Write a program to generate Fibonacci series of First ‘n’ numbers

Syntax:

#include <iostream>
using namespace std;

int main(){

int n, n1=0, n2=1, nt=0;

// Input The Range


cout<<"Enter the number of terms: ";
cin>>n;

cout<<"Fibonacci Series: ";

for(int i=1; i<=n; i++){

// Prints the first two terms.


if(i==1){
cout<<n1<<", ";
continue;
}
if(i==2){
cout<<n2<<", ";
continue;
}

// Calculating The Next Values


nt=n1+n2;
n1=n2;
n2=nt;

// Printing The Latest Values Every Cycle


cout<<nt<< ", ";
}

return 0;
}

Output:

Submitted by:- Nilesh Tamboli (3rd Sem) Submitted to:- Mrs. Pooja Rathore
5|Page Object-Oriented Programming using C++

Practical 5: Write a program to print the multiplication table of a given


Number

Syntax:

#include<iostream>
using namespace std;

int main(){

int n;

// Input of The Value for Multiplication Table


cout<<"Enter The Number For The Table: ";
cin>>n;

cout<<"The Table of "<<n<<" is"<<endl;

// Loop Cycles all the 10 Values of 'n' in an ascending order and


print them
for(int i=1; i<=10; i++){
cout<<n<<" X "<<i<<" = "<<n*i<<endl;
}

return 0;
}

Output:

Submitted by:- Nilesh Tamboli (3rd Sem) Submitted to:- Mrs. Pooja Rathore
6|Page Object-Oriented Programming using C++

Practical 6: Write a Program to calculate the factorial of a Number.

Syntax:

#include<iostream>
using namespace std;

int main(){

int a, num, factor=1;

// Input of The Value


cout<<"Enter The Number: ";
cin>>num;

a = num;

// While Loop to Get Factorial of The Value


while(num>0){
factor = factor * num;
--num;
}

// Final Output or The Factorial of The Value


cout<<"The Factorial of "<<a<<" is "<<factor;

return 0;
}

Output:

Submitted by:- Nilesh Tamboli (3rd Sem) Submitted to:- Mrs. Pooja Rathore
7|Page Object-Oriented Programming using C++

Practical 7: Write a program to add, subtract, multiply and divide two


numbers using ‘switch’ statement.

Syntax:

#include<iostream>
using namespace std;

int main(){

int n, m;
char syb;

cout<<"Enter The First Value: ";


cin>>n;
cout<<"Enter The Second Value: ";
cin>>m;

cout<<"Confirm The Operation you want to compelete by inputting Them


(i.e., '+' ; '-' ; '*' ; '/') : ";
cin>>syb;

switch(syb){
case '+':
cout<<"The Value After Addition is "<<n+m<<endl;
break;
case '-':
cout<<"The Value After Subtraction is "<<n-m<<endl;
break;
case '*':
cout<<"The Value After Multiplication is "<<n*m<<endl;
break;
case '/':
cout<<"The Value After Division is "<<n/m<<endl;
break;
default:
cout<<"The Operation Confirmed is Inappropriate"<<endl<<"Try
Again";
}

return 0;
}

Output:

Submitted by:- Nilesh Tamboli (3rd Sem) Submitted to:- Mrs. Pooja Rathore
8|Page Object-Oriented Programming using C++

Practical 8: Write a Program to check whether the given number is Even or Not.

Syntax:

#include<iostream>
using namespace std;

int main(){

int n;
bool count=true;

// Input The Value to Check


cout<<"Enter The Number You Want To Check: ";
cin>>n;

// Checking Whether The Value 'n' has any other factor


for(int i=2; i<=n/2; i++){

// If it has a factor 'count' changes to false and the loop breaks


if(n%i==0){
count=false;
break;
}
}

// If the 'count' is false, it is not a Prime No.


if(count==false){
cout<<n<<" is not a Prime Number"<<endl;
}

// if The 'count' is still true, it is a Prime No.


else{
cout<<n<<" is a Prime Number"<<endl;
}

return 0;
}

Output:

Submitted by:- Nilesh Tamboli (3rd Sem) Submitted to:- Mrs. Pooja Rathore
9|Page Object-Oriented Programming using C++

Practical 9: Write a program to Print the given pattern: 1


212
32123
Syntax: 4321234
543212345
#include<iostream>
using namespace std;

int main(){

int num;

// Inputting The Length of The Triangle


cout<<"Enter The Length of The Triangle: ";
cin>>num;

// Processing Rows
for(int n=1; n<=num; n++){

// Processing Spaces before Numerical


for(int m=1; m<=(num-n); m++){
cout<<" ";
}

// Processing Numerical Value in Increasing Order


for(int p=n; p>=2; p--){
cout<<p;
}

// Processing Numerical Value in Decreasing Order


for(int p=1; p<=n; p++){
cout<<p;
}
cout<<endl;
}

return 0;
}

Output:

Submitted by:- Nilesh Tamboli (3rd Sem) Submitted to:- Mrs. Pooja Rathore
10 | P a g e Object-Oriented Programming using C++

Practical 10: Write a program to demonstrate the working of call by value

Syntax:

#include <iostream>
using namespace std;

// function declaration
void swap(int x, int y){

int temp;

temp = x; // save the value at address x


x = y; // put y into x
y = temp; // put x into y

return;
};

int main () {

int a, b;

cout << "Enter The value of a :";


cin>>a;

cout << "Enter The value of b :";


cin>>b;

// calling The 'Swap function' to swap the values using variable


value.
swap(a, b);

// Outputing The Value after Swap


cout << "The Value of 'a' After swap is " << a << endl;
cout << "The value of 'b' After swap is " << b << endl;

return 0;
}

Output:

Submitted by:- Nilesh Tamboli (3rd Sem) Submitted to:- Mrs. Pooja Rathore
11 | P a g e Object-Oriented Programming using C++

Practical 11: Write a program to demonstrate the working of call by reference

Syntax:

#include <iostream>
using namespace std;

// function declaration
void swap(int &x, int &y){

int temp;

temp = x; // save the value at address x


x = y; // put y into x
y = temp; // put x into y

return;
};

int main () {

int a, b;

cout << "Enter The value of a :";


cin>>a;

cout << "Enter The value of b :";


cin>>b;

// calling The 'Swap function' to swap the values using variable


reference.
swap(a, b);

// Outputing The Value after Swap


cout << "The Value of 'a' After swap is " << a << endl;
cout << "The value of 'b' After swap is " << b << endl;

return 0;
}

Output:

Submitted by:- Nilesh Tamboli (3rd Sem) Submitted to:- Mrs. Pooja Rathore
12 | P a g e Object-Oriented Programming using C++

Practical 12: Write a Program to demonstrate the working of Pointer

Syntax:

#include<iostream>
using namespace std;

int main(){

int n, *p, *q;

cout<<"Enter a Numerical Value: ";


cin>>n;

p=&n;
q=&n;

cout<<"The twice value using Pointer 'p' : "<<*p*2<<endl;


cout<<"The Address of The Value Using Pointer 'q' : "<<q<<endl;

return 0;
}

Output:

Submitted by:- Nilesh Tamboli (3rd Sem) Submitted to:- Mrs. Pooja Rathore
13 | P a g e Object-Oriented Programming using C++

Practical 13: Write a program to print a given pattern: 1


2 2
3 3
Syntax: 4 4
555555555
#include<iostream>
using namespace std;

int main(){

int num;

// Inputting The Length of The Triangle


cout<<"Enter The Length of The Triangle: ";
cin>>num;

// Processing Rows
for(int n=1; n<=num; n++){

// Processing Spaces before Numerical Value


for(int m=1; m<=(num-n); m++){
cout<<" ";
}

// Processing Numerical Value in Increasing Order


for(int p=1; p<=(2*n-1); p++){

// Processing The Slots for Spaces within The Triangle


if(p>1 && p<(2*n-1) && n<num){
cout<<" ";
}
else{
cout<<n;
}
}
cout<<endl;
}

return 0;
}

Output:

Submitted by:- Nilesh Tamboli (3rd Sem) Submitted to:- Mrs. Pooja Rathore
14 | P a g e Object-Oriented Programming using C++

Practical 14: Write a Program to Reverse the series of Numbers

Syntax:

#include<iostream>

using namespace std;

int main(){

int n;

cout<<"Enter The Number: ";


cin>>n;

int rev=0;
while(n1>0){
int m=n%10;
n/=10;
rev = rev*10 + m;
}
cout<<"The Reverse Order of Number is "<<rev<<endl;

return 0;
}

Output:

Submitted by:- Nilesh Tamboli (3rd Sem) Submitted to:- Mrs. Pooja Rathore
15 | P a g e Object-Oriented Programming using C++

Practical 15: Write a program which can have only one object or instance
at a time.

Syntax:

#include<iostream>
using namespace std;

class Singleton {
public:
static Singleton * Instance();

void SetVal(int val);


void PrintVal();

private:
Singleton() {} // Constructor is private.
Singleton(Singleton const&) {} // Copy constructor is private.
// Assignment operator is private.
Singleton& operator=(Singleton const&) { return *this; }

static Singleton * instance_;

int val_;
};

Singleton* Singleton::instance_ = NULL;

Singleton* Singleton::Instance() {
if(!instance_) {
instance_ = new Singleton();
}

return instance_;
}

void Singleton::SetVal(int val) {


val_=val;
}

void Singleton::PrintVal() {
cout<<"val = "<<val_<<endl;
}

int main(){
Singleton *p = Singleton::Instance();
Singleton *q = Singleton::Instance();

Submitted by:- Nilesh Tamboli (3rd Sem) Submitted to:- Mrs. Pooja Rathore
16 | P a g e Object-Oriented Programming using C++

p->SetVal(7);

cout<<"Prtinting using p: "; p->PrintVal();


cout<<"Prtinting using q: "; q->PrintVal();

q->SetVal(9);

cout<<"Prtinting using p: "; p->PrintVal();


cout<<"Prtinting using q: "; q->PrintVal();

return 0;
}

Output:

Submitted by:- Nilesh Tamboli (3rd Sem) Submitted to:- Mrs. Pooja Rathore
17 | P a g e Object-Oriented Programming using C++

Practical 16: Write a Program That reads in a Five-digit integer and


determine whether it is a palindrome.

Syntax:

#include<iostream>

using namespace std;

int main(){

int n, m=0;

// Input The value


cout<<"Enter a 5 Digit Number: ";
cin>>n;

// Making a Copy of Value for checking The length of the Value


int p=n;

// Determining The Lenth of the Value


while(p>0){
p=p/10;
m++;
}

// Checking Whether if the value is long enough


switch(m){
case(5):
/* while the Value is Sufficient the program continue as
Normal */
break;
default:
// in case it isn't long enough, Exiting the program
cout<<"The Value is Not Exactly 5 Digits Long"<<endl;
cout<<"Please Try Again";
exit(0);
}

/* Making a copy of value and reversing the order for further


Verification */
int rev=0, n1=n;
while(n1>0){
int m=n1%10;
n1/=10;
rev = rev*10 + m;
}

Submitted by:- Nilesh Tamboli (3rd Sem) Submitted to:- Mrs. Pooja Rathore
18 | P a g e Object-Oriented Programming using C++

// Checking Whether if The Value is a Palindrome or not


if(rev==n)
cout<<"The Sequence "<<n<<" is a Palindrome"<<endl;
else
cout<<"The Sequence "<<n<<" is not a Palindrome"<<endl;

return 0;
}

Output:

Submitted by:- Nilesh Tamboli (3rd Sem) Submitted to:- Mrs. Pooja Rathore

You might also like