0% found this document useful (0 votes)
2 views15 pages

C++ FILE

The document contains multiple C++ code snippets demonstrating basic programming concepts such as input/output, arithmetic operations, class definitions, constructors, and method overloading. Each code snippet illustrates different functionalities including calculating sums, averages, areas, and managing employee and student data. The examples are structured to showcase both procedural and object-oriented programming techniques.

Uploaded by

Harshika Sharma
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)
2 views15 pages

C++ FILE

The document contains multiple C++ code snippets demonstrating basic programming concepts such as input/output, arithmetic operations, class definitions, constructors, and method overloading. Each code snippet illustrates different functionalities including calculating sums, averages, areas, and managing employee and student data. The examples are structured to showcase both procedural and object-oriented programming techniques.

Uploaded by

Harshika Sharma
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/ 15

1.

#include<iostream>

using namespace std;

int main(){

cout<<"my name is harshika"<<endl;

return 0;

2.

#include<iostream>

using namespace std;

int main(){

int a = 5;

int b = 6;

int sum = a+b;

cout<<"sum of a and b is: "<<sum<<endl;

return 0;

}
3.

#include<iostream>

using namespace std;

int main(){

int a = 5;

int b = 10;

int c = 12;

int sum = a+b+c;

cout<<"sum of a , b and c is: "<<sum<<endl;

return 0;

4.

#include<iostream>

using namespace std;

int main(){

float a = 5;

float b = 10;

float c = 12;

float d = 13;

float e = 2;

float sum = a+b+c+d+e;

float average = sum / 5;


cout<<"average is : "<<average<<endl;

return 0;

5.

#include<iostream>

using namespace std;

int main(){

int a,b,c,d,e;

cout<<"enter a : "<<endl;

cin>>a;

cout<<"enter b : "<<endl;

cin>>b;

cout<<"enter c : "<<endl;

cin>>c;

cout<<"enter d : "<<endl;

cin>>d;

cout<<"enter e : "<<endl;

cin>>e;

int sum= a+b+c+d+e;

cout<<"sum is : "<<sum<<endl;

return 0;

}
6.

#include<iostream>

using namespace std;

int main() {

int english, maths, science, sst, hindi;

cout << "Enter marks of English: ";

cin >> english;

cout << "Enter marks of Maths: ";

cin >> maths;

cout << "Enter marks of Science: ";

cin >> science;

cout << "Enter marks of SST: ";

cin >> sst;

cout << "Enter marks of Hindi: ";

cin >> hindi;

int sum = english + maths + science + sst + hindi;

int percentage = sum * 100 / 500;

cout << "Total Marks: " << sum << "/500" << endl;

cout << "Percentage is: " << percentage << "%" << endl;

return 0;

}
7.

#include<iostream>

using namespace std;

int main() {

int num1, num2;

cout << "Enter first number: ";

cin >> num1;

cout << "Enter second number: ";

cin >> num2;

int sum = num1 + num2;

int diWerence = num1 - num2;

int product = num1 * num2;

int modulus = num1 % num2;

cout << "Sum: " << sum << endl;

cout << "DiWerence: " << diWerence << endl;

cout << "Product: " << product << endl;

cout << "Modulus: " << modulus << endl;

return 0;

8.

#include<iostream>

#include<cmath>

using namespace std;

int main() {
float base, height, length, breadth, radius;

float areaTriangle, areaRectangle, areaCircle;

cout << "Enter base and height of the triangle: ";

cin >> base >> height;

areaTriangle = 0.5 * base * height;

cout << "Enter length and breadth of the rectangle: ";

cin >> length >> breadth;

areaRectangle = length * breadth;

cout << "Enter radius of the circle: ";

cin >> radius;

areaCircle = M_PI * radius * radius;

cout << "Area of Triangle: " << areaTriangle << endl;

cout << "Area of Rectangle: " << areaRectangle << endl;

cout << "Area of Circle: " << areaCircle << endl;

return 0;

1.

#include<iostream>

using namespace std;

class Employee {

float basicSalary, da, ta, era, totalSalary;

public:

void input() {
cout << "Enter Basic Salary: ";

cin >> basicSalary;

void calculate() {

da = 0.10 * basicSalary; // 10% of basic

ta = 0.05 * basicSalary; // 5% of basic

era = 0.08 * basicSalary; // 8% of basic

totalSalary = basicSalary + da + ta + era;

void display() {

cout << "Basic Salary: " << basicSalary << endl;

cout << "DA (10%): " << da << endl;

cout << "TA (5%): " << ta << endl;

cout << "ERA (8%): " << era << endl;

cout << "Total Salary: " << totalSalary << endl;

};

int main() {

Employee emp;

emp.input();

emp.calculate();

emp.display();

return 0;

}
2.

#include<iostream>

using namespace std;

class Student {

string name;

int roll;

float marks;

public:

void input() {

cout << "Enter student name: ";

cin >> ws;

getline(cin, name);

cout << "Enter roll number: ";

cin >> roll;

cout << "Enter marks: ";

cin >> marks;

void display() {

cout << "Name: " << name << endl;

cout << "Roll Number: " << roll << endl;

cout << "Marks: " << marks << endl;

};

int main() {

Student s;

s.input();

s.display();

return 0;
}

3. #include <iostream>

using namespace std;

class Student {

private:

string name;

int age;

public:

Student() {

name = "Unknown";

age = 0;

cout << "Default Constructor Called!" << endl;

Student(string n, int a) {

name = n;

age = a;

cout << "Parameterized Constructor Called!" << endl;

Student(const Student& s) {

name = s.name;

age = s.age;

cout << "Copy Constructor Called!" << endl;

void display() {
cout << "Name: " << name << ", Age: " << age << endl;

};

int main() {

Student s1;

s1.display();

Student s2("harshika", 20);

s2.display();

Student s3 = s2;

s3.display();

return 0;

4.

#include <iostream>

using namespace std;

class Rectangle {

private:

int length;

int width;

public:

Rectangle() {

length = 0;

width = 0;

cout << "Default Constructor Called!" << endl;

}
Rectangle(int l, int w) {

length = l;

width = w;

cout << "Parameterized Constructor with 2 arguments Called!" << endl;

Rectangle(int l) {

length = l;

width = l;

cout << "Parameterized Constructor with 1 argument Called!" << endl;

void displayArea() {

cout << "Area: " << length * width << endl;

};

int main() {

Rectangle r1;

r1.displayArea();

Rectangle r2(10, 5);

r2.displayArea();

Rectangle r3(4);

r3.displayArea();

return 0;

5.

#include <iostream>
using namespace std;

class Calculator {

public:

int add(int a, int b) {

return a + b;

int add(int a, int b, int c) {

return a + b + c;

};

int main() {

Calculator calc;

cout << "Sum of 2 numbers: " << calc.add(5, 3) << endl;

cout << "Sum of 3 numbers: " << calc.add(5, 3, 2) << endl;

return 0;

6.

#include <iostream>

using namespace std;

class Calculator {

public:

int add(int a, int b) {

return a + b;

float add(float a, float b) {

return a + b;
}

float add(int a, float b) {

return a + b;

};

int main() {

Calculator calc;

cout << "Sum of two integers: " << calc.add(5, 3) << endl;

cout << "Sum of two floats: " << calc.add(5.5f, 3.2f) << endl;

cout << "Sum of integer and float: " << calc.add(5, 3.2f) << endl;

return 0;

7.

#include <iostream>

using namespace std;

class Calculator {

public:

int add(int a, int b) {

return a + b;

int add(int a, int b, int c) {

return a + b + c;

};

int main() {

Calculator calc;
int a, b, c;

cout << "Enter two integers: ";

cin >> a >> b;

cout << "Sum of 2 numbers: " << calc.add(a, b) << endl;

cout << "Enter three integers: ";

cin >> a >> b >> c;

cout << "Sum of 3 numbers: " << calc.add(a, b, c) << endl;

return 0;

8.

#include <iostream>

using namespace std;

class Calculator {

public:

int add(int a, int b) {

return a + b;

float add(float a, float b) {

return a + b;

float add(int a, float b) {

return a + b;

};

int main() {
Calculator calc;

int int1, int2;

float float1, float2;

cout << "Enter two integers: ";

cin >> int1 >> int2;

cout << "Sum of two integers: " << calc.add(int1, int2) << endl;

cout << "Enter two floats: ";

cin >> float1 >> float2;

cout << "Sum of two floats: " << calc.add(float1, float2) << endl;

cout << "Enter an integer and a float: ";

cin >> int1 >> float1;

cout << "Sum of integer and float: " << calc.add(int1, float1) << 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