#include<iostream>
using namespace std;
int main(){
// Your code logic goes here
return 0;
}
#include<iostream>
using namespace std;
int main(){
cout<<"Hello World!";
return 0;
}
Given marks of a student in 3 subjects - Physics, Chemistry and Maths - print their average.
Â
Sample Input
Physics = 90
Maths = 75
Chemistry = 68
Â
Sample Output
77.66
#include
directive tells the compiler to include the header file in the source code.
Â
Â
#include<iostream>
#incldue<algorithm>
#include<stack>
int main(){
....
}
#define
directive tells the compiler to create symbolic constants. The symbolic constant is called a macro.
Â
All subsequent occurrences of macro in that source code will be replaced by its replacement text before the program is compiled.
#define PI 3.14
int main(){
//Area of Circle
int r = 5;
float area = PI*r*r;
return 0;
}
#define
directive tells the compiler to create symbolic constants. The symbolic constant is called a macro.
Â
All subsequent occurrences of macro in that source code will be replaced by its replacement text before the program is compiled.
#define PI 3.14
#define AREA(l,b) (l*b)
int main(){
//Area of Rectangle
int area = AREA(10,5);
return 0;
}
Â
The identifier is only used to identify an entity uniquely in a program at the time of execution whereas, a variable is a name given to a memory location, that is used to hold a value.​
float calculateMarks(int p,int c, int m){
float average = (p+c+m)/3;
return average;
}
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
32 Keywords common in C++ and C
Â
asm dynamic_cast namespace reinterpret_cast
bool explicit new static_cast
catch false operator template
class friend private this
const_cast inline public throw
delete mutable protected true
try typeid typename using
using virtual wchar_t
Some new Keywords in C++
int factorial(int n){
if(n<0){
cout << "Invalid Input";
return -1;
}
int ans = 1;
for(int i=1;i<=n;i++){
ans = ans*i;
}
return ans;
}
int main(){
//Execution stars from here
// Your Work
return 0;
}
int main(){
//logic
return 0;
}
int main(int argc,char *argv[]){
//logic
return 0;
}
using namespace cv;
Â
 To avoid data structure and function name conflicts with other libraries, OpenCV has its own namespace: cv.
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
cout<<"Welcome to Image Editing App";
Mat image = imread("Bob.jpg");
return 0;
}
Â
Â
Â
Variable name: A label for a memory location
Value: The something that would be
stored in a variable
Storage: A place where data can be stored
Declaration: Announcing a variable (usually) at the beginning of a program
Naming convention: A set of rules about the names of variables
Assignment: Giving (setting) a variable a value
Â
Â
Â
//Valid Names
double simple_interest;
int student_age;
float Student_percentile;
int prateek123;
//Invalid Names
int 123_age;
Â
//Initialisation of Variable
float a = 10;
//Declaration
int b;
//Assignment
b = 20;
Boolean - boolean
Character - char
Integer – int
Floating Point – float
Double Floating Point – double
Several of the basic types can be modified using one or more of these type modifiers
Â
signed
unsigned
short
long
int marks;
unsigned int roll_number;
unsigned long long int large_factorial;
short int age;
Constants are variables or values in programming language which cannot be modified once they are defined.
const float pi = 3.14
const int loan_period = 10
//Don't do this, Initialisation is Must for constants
const int x;
x = 5;
// Assignment is not allowed here
Â
Using a const keyword
Using preprocessor directive
#define PI 3.14
#define LOAN_PERIOD 10
// Single If
int marks = 90;
if (marks > 80) {
cout << “Let's Party!”;
}
If-else block
// If-Else Block
int marks = 70;
if (marks > 80) {
cout << “Let's Party”;
}
else{
cout<< “Work hard next time“;
}
If-else-if-else block
// If-Else Block
int marks = 70;
if (marks > 80) {
cout << “Let's Party”;
}
else if(marks>60){
cout<<"Good Job";
}
else{
cout<<"Work hard next time";
}
Challenge 🔥Â
Electricity Bill Calculator : Given total consumption of aÂ
household in units, write a program to estimate the total bill amount as per the table.
Units | Charges |
---|---|
1 to 100 units | Free |
100 to 200 units | Rs. 5/unit |
200 to 300 units | Rs.10/unit |
300+ units | Rs.12/unit |
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
//Init
while(..condition is true ..){
//execute some stuff
//update
}
for(init;stopping_condition;update_statement){
//execute some stuff
}
for(int calories=0;calories<100;calories = calories+1){
//execute some stuff
cout<<"Run 1 step";
}
Â
Given N, Print following pattern (For Example N = 5)
Â
  *
  ***
 *****
 *******
Â
Given N, Print following pattern (For Example N = 5)
Â
ABCDEEDCBA
ABCDDCBA
ABCCBA
ABBA
AA
Â
Â
Â
Â
Given N, Print all Prime Numbers upto N.
Â
For Example - N = 10
Â
Output - [2,3,5,7]
//init
do(){
//execute some stuff
}
while(condition);
//init
do(){
//execute some stuff
}
while(condition);
//Exit Controlled Loop
//init
//Entry Controlled Loop
while(condition){
//execute some stuff
}
int calories = 0;
while(calories<20){
if(calories==15){
cout<<"Stop the Workout";
break;
}
cout<<calories<<" ";
calories = calories + 1;
}
cout<<"Complete";
int calories = 0;
while(calories<20){
if(calories==15){
cout<<"Stop the Workout";
calories = calories + 1;
continue;
}
cout<<calories<<" ";
calories = calories + 1;
}
cout<<"Complete";
int calories = 0;
while(calories<20){
if(calories==15){
cout<<"Stop the Workout";
break;
}
cout<<calories<<" ";
calories = calories + 1;
}
cout<<"Complete";
Stop the loop when executed.
int calories = 0;
while(calories<20){
if(calories==15){
cout<<"Go to the next Step";
calories = calories + 1;
continue;
}
cout<<calories<<" ";
calories = calories + 1;
}
cout<<"Complete";
Control jumps to the beginning of the loop for next iteration
Â
Â
    1
   232
  34543
 4567654
567898765