C++ Basics
Photo
#2
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 3.141592653589793;
int main() {
double radius;
double volume;
cout << "Volume Sphere Calculator" << endl;
cout << "Enter radius: ";
cin >> radius;
if (radius < 0) {
cout << "Error: Radius cannot be negative." << endl;
return 1;
}
volume = (4 * PI * pow(radius, 3)) / 3;
cout << "The Volume Sphere is: " << volume << endl;
return 0;
}
π8π₯°3
C++ Basics
Quiz Scenario: Create two variables that accept name and age, then display them to the user. you should use appropriate data types
#include <iostream>
using namespace std;
int main() {
string name;
int age;
cout << "Please enter your name: " << endl;
cin>>name;
cout << "Please enter your age: " << endl;
cin >> age;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
return 0;
}
π9
Quiz
What's the output of this code and if you got error fix it and explain why it occur?
What's the output of this code and if you got error fix it and explain why it occur?
#include <iostream>
using namespace std;
void add();
int main() {
int age = 21;
add();
return 0;
}
void add() {
cout << "Age: " << age << endl;
}
Forwarded from Dagi
#include <iostream>
using namespace std;
int age = 21;
void add() {
cout << "Age: " << age << endl;
}
int main() {
add();
return 0;
}
Like this
using namespace std;
int age = 21;
void add() {
cout << "Age: " << age << endl;
}
int main() {
add();
return 0;
}
Like this
π4π€2π1
This one also possible
#include <iostream>
using namespace std;
int age = 21;
void add();
int main() {
add();
return 0;
}
void add() {
cout << "Age: " << age << endl;
}
Output :21
#include <iostream>
using namespace std;
int age = 21;
void add();
int main() {
add();
return 0;
}
void add() {
cout << "Age: " << age << endl;
}
Output :21
Ehen ensera ena lezare yebekanal
Write the output of thisπ code?
#include <iostream>
using namespace std;
int main( ) {
int n = 4, k = 2;
cout<< ++n << endl;
cout<< n++ << endl;
cout<< n << endl;
cout<< --n << endl;
cout<< n << endl;
cout<< n--<< endl;
cout<< n + k << endl;
cout<< k << endl;
cout<< " n" << endl;
cout<< "\n" << endl;
cout<< " n * n = ";
cout<< n * n << endl;
cout<< 'n' << endl;
return 0;
}
Write the output of thisπ code?
#include <iostream>
using namespace std;
int main( ) {
int n = 4, k = 2;
cout<< ++n << endl;
cout<< n++ << endl;
cout<< n << endl;
cout<< --n << endl;
cout<< n << endl;
cout<< n--<< endl;
cout<< n + k << endl;
cout<< k << endl;
cout<< " n" << endl;
cout<< "\n" << endl;
cout<< " n * n = ";
cout<< n * n << endl;
cout<< 'n' << endl;
return 0;
}
π8π1
1.
#include <iostream>
using namespace std;
int main() {
int numb1, numb2;
cout << "Please Enter Number 1:" << endl;
cin >> numb1;
cout << "Please Enter Number 2:" << endl;
cin >> numb2;
cout << "X + Y :" << numb1 + numb2 << endl;
if (numb2 != 0) {
cout << "X / Y :" << numb1 / numb2 << endl;
}
cout << "X * Y :" << numb1 * numb2 << endl;
cout << "X - Y :" << numb1 - numb2 << endl;
if (numb1 > numb2) {
cout << "Number " << numb1 << " is Greater than Number " << numb2 << endl;
} else if (numb1 < numb2) {
cout << "Number " << numb1 << " is Greater than Number " << numb2 << endl;
} else {
cout << "They are equal" << endl;
}
return 0;
}
π₯°5π4
C++ Basics
Photo
2.
#include <iostream>
using namespace std;
const double PI = 3.14159265358979323846;
int main() {
double radius;
double circumference;
cout << "Please enter the radius: " << endl;
cin >> radius;
if (radius <= 0) {
cout << "Radius must be a positive number." << endl;
return 1;
}
circumference = 2 * PI * radius;
cout << "The circumference of the circle is: " << circumference << endl;
return 0;
}
π2π₯2
3..
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double root1, root2;
int a, b, c;
double discriminant, square;
cout << "Please Enter coefficient A:" << endl;
cin >> a;
cout << "Please Enter coefficient B:" << endl;
cin >> b;
cout << "Please Enter coefficient C:" << endl;
cin >> c;
cout << a << "x^2+" << b << "x+" << c << endl;
if (a == 0) {
cout << "Division by zero isn't possible" << endl;
return 1;
}
discriminant= pow(b, 2) - (4 * a * c);
if(discriminant>0){
square =sqrt(discriminant);
root1 = (-b + square) / (2 * a);
root2 = (-b - square) / (2 * a);
cout << "Root1 :"<<root1 << endl;
cout << "Root2 :"<<root2<< endl;
}else if(discriminant==0){
square =sqrt(discriminant);
root1=(-b)/(2*a);
cout << "Root: " << root1<< endl;
}else{
cout << "The equation is complex number"<< endl;
}
return 0;
}
π3π₯2
.
Input a string and count the vowels(a,e,I,o,u) . Algorithm,pseudocode and flowchart.please do me this .
#include<iostream>
#include<string>
using namespace std;
int main()
{
string name;
cout << "Enter A text " << endl;
getline(cin,name);
int count=0;
for(int i=0; i<name.length(); i++) {
switch(name[i])
{
case 97:
case 65:
count++;
break;
case 101:
case 69:
count++;
break;
case 105:
case 73:
count++;
break;
case 111:
case 79:
count++;
break;
case 117:
case 85:
count++;
break;
}
}
if(count==0) {
cout << "There is no vowels" << endl;
}
else {
cout << "There are "<<count<<" vowels" << endl;
}
return 0;
}
π5
Function
β What is a Function?
A function is a reusable block of code that performs a specific task.
Think of a vending machine:
- Input (Parameters): You select a snack (e.g., B3 for chips).
- Process (Function Body): The machine processes your request.
- Output (Return Value): It delivers the chips.
β Function Components
a) Declaration (Menu)
- Tells the compiler a function exists.
b) Definition (Machine Internals)
c) Call (Pressing the Button)**
β Types of Functions
-Library Function
-User-Defined
β Parameter Passing
a) Call by Value (Photocopy)
- Like giving a photocopy of your ID β changes donβt affect the original.
b) Call by Reference (Original Document)
- Like signing the original contract β changes are permanent.
Example:
β Why Use Functions?
Reusability: Like a recipe you reuse (e.g.,
Modularity: Break problems into smaller parts (e.g.,
Debugging: Fix one function without breaking others.
β Exam-Style Example :
β Common Mistakes
1.Missing Return Statement:
2.Incorrect Parameters:
β Real-World Coding Example
Scenario: A coffee shop calculator.
Key Insight:
-
β What is a Function?
A function is a reusable block of code that performs a specific task.
Think of a vending machine:
- Input (Parameters): You select a snack (e.g., B3 for chips).
- Process (Function Body): The machine processes your request.
- Output (Return Value): It delivers the chips.
β Function Components
a) Declaration (Menu)
// Like the vending machine's menu
int getDrink(int selection);
- Tells the compiler a function exists.
b) Definition (Machine Internals)
// Like the machine's internal mechanism
int getDrink(int selection) {
if (selection == 1) return COKE;
else return WATER;
}
c) Call (Pressing the Button)**
int myDrink = getDrink(1); // Output: COKE
β Types of Functions
-Library Function
sqrt(25) (from <cmath>) -User-Defined
calculateTax(income) β Parameter Passing
a) Call by Value (Photocopy)
void printReceipt(int amount) {
// Works with a COPY of the original
cout << "Amount: " << amount;
} - Like giving a photocopy of your ID β changes donβt affect the original.
b) Call by Reference (Original Document)
void updateBalance(int &account) {
account -= 100; // Modifies the ORIGINAL
} - Like signing the original contract β changes are permanent.
Example:
int wallet = 500;
updateBalance(wallet); // wallet becomes 400
β Why Use Functions?
Reusability: Like a recipe you reuse (e.g.,
calculateInterest()). Modularity: Break problems into smaller parts (e.g.,
login(), checkout()). Debugging: Fix one function without breaking others.
β Exam-Style Example :
// Function to check even numbers
bool isEven(int num) {
return (num % 2 == 0);
}
β Common Mistakes
1.Missing Return Statement:
int sum(int a, int b) {
a + b; // Error! No return.
}
2.Incorrect Parameters:
void greet(string name) { ... }
greet(); // Error! Missing argument.
β Real-World Coding Example
Scenario: A coffee shop calculator.
#include <iostream>
using namespace std;
// Function to calculate total cost
float calculateTotal(int coffees, float price) {
return coffees * price;
}
int main() {
int cups = 3;
float cost = calculateTotal(cups, 2.5);
cout << "Total: $" << cost; // Output: $7.5
return 0;
}
Key Insight:
-
calculateTotal is like a cashier β you give inputs (cups, price), it returns the result.β€3π₯3π1