Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.vscode
/**/practice
**.exe
21 changes: 21 additions & 0 deletions Learn-CPP/Day-01/hello.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Let's with the basics of CPP

// Import a library 😁

#include <iostream> // It's precompiled code, which includes keywords, namespaces

using namespace std;

// Main function, it's better to write void as an argument
// It makes program more readable
int main(void){

// Print statement, outputs to the console
cout<<"Let's go with g++!!";
// return 0; // Tells the os, that the program execution was succesfull
}

// g++ {programFileName}.cpp -o {outputFileName}.(out | exe)
// Run the file
// ./{outputFile} - Powershell / bash
// call {outputFile} - On CMD
Binary file added Learn-CPP/Day-01/hello.out
Binary file not shown.
26 changes: 26 additions & 0 deletions Learn-CPP/Day-02/alias.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include<iostream>

using namespace std;


// Typedef is just like defining a new keyword with properties or behaviour of existing keyword
typedef int rollno; // rollno has properties of `int` now


// enum is like defining a counter to the given set of items
// By default this counter will start from 0, but we can specify the counter value for specific item, and others will have there counters updated
enum working {mon=1, tue, wed, fri=5, sat, sun};
// 1 2 3 5 6 7

int main(void){

rollno myRollNo = 46; // myRollNo is of type `int` only, but this version of code is much more readable
cout<<"Size of 'myRollNo': "<<sizeof(myRollNo)<<endl<<endl;

// Value wed, will be 3, as it second after the mon(1)
cout<<"Value for wed: "<<working::wed<<endl;
cout<<"Value for sat: "<<working::sat<<endl;
// sat will be 6, as it comes just after fri(5)

return 0;
}
31 changes: 31 additions & 0 deletions Learn-CPP/Day-02/arithmetic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include<iostream>
using namespace std;

int main(void){

char a = 'a';
char b = 'b';

char c = a + b;

cout<<"Printing directly the sum: "<<a+b <<endl; // Output is 195(97 + 98), as internally char are stored as integers(ASCII values)
cout<<"Printing variable declared as `char`: "<<c<<endl; // Outputs ├ because `c` is declared as the `char`, so 195 is converted back to character

// Integers

int Int1 = 5;
int Int2 = 7;

int sum = Int1 + Int2;
int diff = Int1 - Int2;
int product = Int1 * Int2;
int remainder = Int1 % Int2; // It's called a modulo operator

cout<<"Working with Integers";
cout<<"Sum: "<<sum<<endl;
cout<<"Diff: "<<diff<<endl;
cout<<"Product: "<<product<<endl;
cout<<"Remainder: "<<remainder<<endl;

return 0;
}
31 changes: 31 additions & 0 deletions Learn-CPP/Day-02/data_modifiers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include<iostream>

using namespace std;

int main(void){
/*

Primitive Data types: (int, char): Integral, bool, (float, double): Decimal
Data type
signed - Default one , one bit is reserved for the sign
unsigned - All bits are used, minimum number will be 0
short - Almost half the size
long - Almost double the size (depends on compiler)

*/

int var = 5;
signed int svar = 6;
unsigned int uvar = 6;
short int shvar = 6;
long int lvar = 6;


cout<<"Size of int: "<<sizeof(var)<<endl; // 4
cout<<"Size of signed int: "<<sizeof(svar)<<endl; // 4
cout<<"Size of unsigned int: "<<sizeof(uvar)<<endl; // 4
cout<<"Size of short int: "<<sizeof(shvar)<<endl; // 2
cout<<"Size of long int: "<<sizeof(lvar)<<endl; // 8

return 0;
}
12 changes: 12 additions & 0 deletions Learn-CPP/Day-02/uses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Uses of typedef
#include <iostream>

using namespace std;

// Instead of writing long long int, now we have to write only ll, to define a long long int
typedef long long int ll;

int main(void){
ll myLongInt;
cout<<sizeof(ll);
}
11 changes: 11 additions & 0 deletions Learn-CPP/Day-02/variables.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include<iostream>

// Define namespace
using namespace std;

// Main function
int main(void){
int a = 5; // Declare a variable with integer data type
cout<<"Value of a: "<< a; //output above integer
return 0;
}
14 changes: 14 additions & 0 deletions Learn-CPP/Day-03/advanced_conditionals.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include<iostream>

using namespace std;

int main(void){
// Comma(,) Operator returns last output
int a=5, b=6, c=7;
if(a<b, b>c){
cout<<"This is inside if statement";
}else{
cout<<"Though the first expression is true, else is getting processed...";
}
return 0;
}
32 changes: 32 additions & 0 deletions Learn-CPP/Day-03/menu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include<iostream>
using namespace std;

int main(void){
string time, name;
cout<<"Enter your name: ";
cin>>name;

cout<<"What's the section of the day now?(morning, afternoon, eve): ";
cin>>time;

switch(time){ // May throw, switch quantity is not integer, in CPP version<17
case "morning":
cout<<"Good morning "<<name<<". Did you have breakfast yet?\n";
break;
case "afternoon":
cout<<"Good afternoon "<<name<<". Did you have lunch yet?\n";
break;
case "eve":
case "evening":
cout<<"Good evening "<<name<<". Did you have evening yet?\n";
break;
case "night":
cout<<"Good night "<<name<<".🥱\n";
break;

default:
cout<<"Well, maybe you're living in different dimension\a";
}

return 0;
}
42 changes: 42 additions & 0 deletions Learn-CPP/Day-03/relation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Let's start with Relational and Logical operators
#include<iostream>

using namespace std;

int main(void){

/*
Relational Operators
> : Greater than
< : Less than
>= : Greater than or equal to
<= : Greater than or equal to
== : Is equal
!= : Not equal
*/

int a = 8, b = 7;

cout<<"a = 8\nb = 7\n";
cout<<"a > b: "<<(a>b);
cout<<"\na < b: "<<(a<b);
cout<<"\na >= b: "<<(a>=b);
cout<<"\na <= b: "<<(a<=b);
cout<<"\na == b: "<<(a==b);
cout<<"\na != b: "<<(a!=b);


/*
Logical Operators
&& : AND
|| : OR
! : NOT : Unary Operator
*/

int c = 5;
cout<<"\n\na>b and a>c (Is a greatest of them): "<<(a>b) && (a>c);
cout<<"\nb>c and b>a (Is b greatest of them): "<<(b>c) && (b>a);
cout<<"\nc>a and c>b (Is a greatest of them): "<<(c>a) && (c>b);

return 0;
}
25 changes: 25 additions & 0 deletions Learn-CPP/Day-03/switch_case.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include<iostream>
using namespace std;

int main(void){
int choice;
cout<<"Enter your choice: ";
cin>>choice;

// Compare the value of `choice` variable
switch(choice){
case 0:
cout<<"Choice 0"<<endl;
break;
case 1:
cout<<"Choice 1"<<endl;
break;
case 2:
cout<<"Choice 2"<<endl;
break;

default:
cout<<"Not defined case"<<endl;
}
return 0;
}
16 changes: 16 additions & 0 deletions Learn-CPP/Day-03/ternary_max.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Find max of 3 numbers using ternary operators
#include<iostream>
using namespace std;

int main(void){

int a, b, c, _MAX;
cout<<"Type in 3 integers: ";
cin>>a>>b>>c;

// Just use ternary operators to minimise the LOC (lines of code)
_MAX = (a>b && a>c)?a:(b>c?b:c);
cout<<"Maximum is: "<<_MAX;

return 0;
}
14 changes: 14 additions & 0 deletions Learn-CPP/Day-04/factorial.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include<iostream>
using namespace std;

int main(void){
int n;
unsigned long long int fact=1;
cout<<"Enter n: ";
cin>>n;

for(int i=2; i<=n; i++) fact*=i;
cout<<fact;

return 0;
}
24 changes: 24 additions & 0 deletions Learn-CPP/Day-04/hw/detectPrime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Check if the number is prime
#include<iostream>
using namespace std;

int main(void){
int num;
bool isPrime = true;

cout<<"Enter the number to check: ";
cin>>num;


// Check till i*i<=num
for(int i=2; i<num/2+1; i++){
if(num%i==0){
isPrime = false;
break;
}
}

cout<<num<<" is a "<<(isPrime?"Prime":"Composite")<<" number...";

return 0;
}
18 changes: 18 additions & 0 deletions Learn-CPP/Day-04/hw/sumNatural.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Find the sum of n natural numbers
#include<iostream>
using namespace std;

int main(void){
int n, _sum = 0;

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

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

// Alternative: cout<<n*(n+1)/2;

cout<<"Sum of "<<n<<" natural numbers = "<<_sum;

return 0;
}
10 changes: 10 additions & 0 deletions Learn-CPP/Day-04/intro_for.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Introduction to for loop
#include<iostream>
using namespace std;

int main(void){

for(int i=1; i<10; i*=2) cout<<i<<" ";

return 0;
}
16 changes: 16 additions & 0 deletions Learn-CPP/Day-04/max_function.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Use of max function to get max of 3 variables
#include<iostream>
using namespace std;

int main(void){
int a, b, c, _max;

cout<<"Type in 3 integers: ";
cin>>a>>b>>c;

// Only takes to arguments
_max = max(a, max(b, c));
cout<<"Greatest of these numbers: "<<_max;

return 0;
}
27 changes: 27 additions & 0 deletions Learn-CPP/Day-04/perfectNumEff.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Trying to find efficient algorithm to find if the number is a Perfect Number
#include<iostream>
#include<assert.h>
using namespace std;

typedef long int l;

bool isPerfect(int num){
l sum = 1;
for (int i = 2; i < num/2; i++){
if(num%i == 0){
sum += i;
if(num/i >= num/2 && num/i != i){
sum += num/i;
}
}
}
return num==sum;
}

int main(void){
assert(isPerfect(6));
assert(isPerfect(28));
assert(isPerfect(496));
assert(!isPerfect(5));
cout<<"Yupp";
}
Loading