0% found this document useful (0 votes)
1 views7 pages

Sample Programs

The document contains multiple C++ programs demonstrating various programming concepts including constants, control structures, user-defined data types, memory management, and classes. Each program illustrates specific features such as arithmetic operations, prime number checking, simple calculators, and number manipulation techniques. The programs are structured to showcase both procedural and object-oriented programming approaches.

Uploaded by

ranjijoshu07
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)
1 views7 pages

Sample Programs

The document contains multiple C++ programs demonstrating various programming concepts including constants, control structures, user-defined data types, memory management, and classes. Each program illustrates specific features such as arithmetic operations, prime number checking, simple calculators, and number manipulation techniques. The programs are structured to showcase both procedural and object-oriented programming approaches.

Uploaded by

ranjijoshu07
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/ 7

Program 1: Constants, Keywords, Identifiers, Operators, Expression Types

#include <iostream>
using namespace std;

int main() {
const int x = 10; // Constant
int y = 5; // Identifier
float z = x + y * 2.5; // Expression (arithmetic,
precedence)

cout << "Result: " << z << endl; // Output

return 0; // Keyword
}

Program 2: User-defined Data Type (struct), Derived Data Type (array),


Control Structures
#include <iostream>
using namespace std;

struct Student { // User-defined data type


int id;
char name[20]; // Derived data type (array)
float marks;
};

int main() {
Student s = {1, "Amit", 85.5};

if (s.marks > 50) { // Control structure


cout << s.name << " passed!" << endl;
} else {
cout << s.name << " failed!" << endl;
}

return 0;
}

Program 3: Class, Scope Resolution Operator, Operator Precedence


#include <iostream>
using namespace std;

class Math {
public:
int a, b;

void set(int x, int y) {


a = x;
b = y;
}

int add();
};

// Scope resolution operator used outside class


int Math::add() {
return a + b * 2; // Operator precedence
}

int main() {
Math m;
m.set(5, 3);
cout << "Result = " << m.add() << endl;
return 0;
}

Program 4: Memory Management Operators (new, delete), Pointers, Control


Structures
#include <iostream>
using namespace std;

int main() {
int* p = new int; // Memory allocation
*p = 20;

if (*p > 10) {


cout << "Value is greater than 10" << endl;
}

delete p; // Memory deallocation


return 0;
}

Program 5: Enum (User-defined), Switch (Control Structure), Derived Type


#include <iostream>
using namespace std;

enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat }; // User-defined type

int main() {
Day today = Wed; // Identifier

switch (today) { // Control structure


case Sun: cout << "Sunday" << endl; break;
case Wed: cout << "Wednesday" << endl; break;
default: cout << "Another day" << endl;
}

return 0;
}

Program 6: Find Prime Numbers in a Range (using for and if)


#include <iostream>
using namespace std;

int main() {
int start, end;
cout << "Enter start and end values: ";
cin >> start >> end;
cout << "Prime numbers between " << start << " and " << end << " are:
";
for (int num = start; num <= end; num++) {
bool isPrime = true;
if (num <= 1) continue;

for (int i = 2; i * i <= num; i++) {


if (num % i == 0) {
isPrime = false;
break;
}
}

if (isPrime) {
cout << num << " ";
}
}

cout << endl;


return 0;
}

Program 7: Simple Calculator Using switch


#include <iostream>
using namespace std;

int main() {
char op;
double a, b;
cout << "Enter operator (+, -, *, /): ";
cin >> op;
cout << "Enter two numbers: ";
cin >> a >> b;

switch (op) {
case '+': cout << "Result = " << a + b; break;
case '-': cout << "Result = " << a - b; break;
case '*': cout << "Result = " << a * b; break;
case '/':
if (b != 0) cout << "Result = " << a / b;
else cout << "Division by zero!";
break;
default: cout << "Invalid operator!";
}

cout << endl;


return 0;
}

Program 8: Reverse a Number (using while)


#include <iostream>
using namespace std;

int main() {
int num, reverse = 0;
cout << "Enter a number: ";
cin >> num;

while (num != 0) {
int digit = num % 10;
reverse = reverse * 10 + digit;
num /= 10;
}

cout << "Reversed number = " << reverse << endl;


return 0;
}

Program 9: Multiplication Table using do-while


#include <iostream>
using namespace std;

int main() {
int n, i = 1;
cout << "Enter a number to print its multiplication table: ";
cin >> n;

do {
cout << n << " x " << i << " = " << n * i << endl;
i++;
} while (i <= 10);

return 0;
}

Program 10: Sum of Digits Using Loop and Condition


#include <iostream>
using namespace std;

int main() {
int num, sum = 0;
cout << "Enter a number: ";
cin >> num;

while (num != 0) {
sum += num % 10;
num /= 10;
}

cout << "Sum of digits = " << sum << endl;


return 0;
}
Programs rewritten using C++ classes
Program 1: Find Prime Numbers in a Range (using class)
#include <iostream>
using namespace std;

class PrimeChecker {
public:
void findPrimes(int start, int end) {
cout << "Prime numbers between " << start << " and " << end << ":
";
for (int num = start; num <= end; num++) {
if (isPrime(num)) {
cout << num << " ";
}
}
cout << endl;
}

private:
bool isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return false;
}
return true;
}
};

int main() {
PrimeChecker pc;
int start, end;
cout << "Enter start and end values: ";
cin >> start >> end;
pc.findPrimes(start, end);
return 0;
}

Program 2: Simple Calculator (using class and switch)


#include <iostream>
using namespace std;

class Calculator {
public:
void calculate(double a, double b, char op) {
switch (op) {
case '+': cout << "Result = " << a + b; break;
case '-': cout << "Result = " << a - b; break;
case '*': cout << "Result = " << a * b; break;
case '/':
if (b != 0) cout << "Result = " << a / b;
else cout << "Division by zero!";
break;
default: cout << "Invalid operator!";
}
cout << endl;
}
};

int main() {
Calculator calc;
double a, b;
char op;
cout << "Enter operator (+, -, *, /): ";
cin >> op;
cout << "Enter two numbers: ";
cin >> a >> b;
calc.calculate(a, b, op);
return 0;
}

Program 3: Reverse a Number (using class)


#include <iostream>
using namespace std;

class Reverser {
public:
int reverseNumber(int num) {
int rev = 0;
while (num != 0) {
rev = rev * 10 + (num % 10);
num /= 10;
}
return rev;
}
};

int main() {
Reverser r;
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Reversed number = " << r.reverseNumber(num) << endl;
return 0;
}

Program 4: Multiplication Table using do-while in Class


#include <iostream>
using namespace std;

class TablePrinter {
public:
void printTable(int n) {
int i = 1;
do {
cout << n << " x " << i << " = " << n * i << endl;
i++;
} while (i <= 10);
}
};
int main() {
TablePrinter tp;
int n;
cout << "Enter a number: ";
cin >> n;
tp.printTable(n);
return 0;
}

Program 5: Sum of Digits Using Class


#include <iostream>
using namespace std;

class DigitSummer {
public:
int sumOfDigits(int num) {
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
};

int main() {
DigitSummer ds;
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Sum of digits = " << ds.sumOfDigits(num) << endl;
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy