├── C++ ├── 1. Adding colors in C++ │ └── adding colors in c++.cpp ├── 10. Interview's Questions │ ├── swaping using three variables.cpp │ ├── swapping using two variables method_1.cpp │ └── swapping using two variables method_2.cpp ├── 2. Basics to if-else │ ├── 'fixed' Manipulator.cpp │ ├── 'setfill' manipulator.cpp │ ├── 'setprecision' manipulator.cpp │ ├── 'setw' manipulator.cpp │ ├── Any character teller.cpp │ ├── Armstrong Num or not.cpp │ ├── C++ Basic with Hello.cpp │ ├── CONSTANTS.cpp │ ├── D bw post and prefix decrement operator.cpp │ ├── D bw post and prefix increment operator.cpp │ ├── OPERATORS IN C++.cpp │ ├── PRECEDENCE IN ARITHMETIC OPERATORS.cpp │ ├── PREPOCESSOR DIRECTIVES.cpp │ ├── Palindrome num or not.cpp │ ├── Perfect num or not.cpp │ ├── THE main() FUNCTION.cpp │ ├── The C++ header files.cpp │ ├── area finder.cpp │ ├── area of square.cpp │ ├── area of triangle.cpp │ ├── area using define directive.cpp │ ├── arithmetic calculator.cpp │ ├── average marks.cpp │ ├── avg using if.cpp │ ├── bill calculator.cpp │ ├── character into ASCII.cpp │ ├── check all even or odd if-else.cpp │ ├── check character or number.cpp │ ├── check divisibility.cpp │ ├── check even or odd using if-else.cpp │ ├── check even or odd without remainder operator.cpp │ ├── check positive or negative number.cpp │ ├── check uppercase or lowercase character.cpp │ ├── choose area n circumference.cpp │ ├── comparing integers.cpp │ ├── composite num or prime num.cpp │ ├── conditional operator.cpp │ ├── define directive.cpp │ ├── eligible or not.cpp │ ├── find quotient and remainder.cpp │ ├── find square n cube.cpp │ ├── geometry calculator.cpp │ ├── getline and cin ignore used.cpp │ ├── grades calculaor 1100.cpp │ ├── grades calculator.cpp │ ├── greater among 3 nested if.cpp │ ├── greatest among 3 nested if.cpp │ ├── greatest among 3 using if.cpp │ ├── hello in C++.cpp │ ├── if statement.cpp │ ├── inches into mm and vice versa.cpp │ ├── integer overflow and underflow.cpp │ ├── introducing variables.cpp │ ├── larger age of two using if-else.cpp │ ├── leap year or not.cpp │ ├── max or min of 5 num.cpp │ ├── middle number btw two numbers.cpp │ ├── miles into km and vice versa.cpp │ ├── miles to km.cpp │ ├── multiple and compound assignment operator and expression..cpp │ ├── multiple or not.cpp │ ├── nested if-1.cpp │ ├── nested if-2.cpp │ ├── nested if-3.cpp │ ├── numbers are equal or not.cpp │ ├── print the number entered by the user.cpp │ ├── quick history of C++.cpp │ ├── reverse 3 digit.cpp │ ├── reverse a number.cpp │ ├── salary bonus.cpp │ ├── separate digits.cpp │ ├── size of operator.cpp │ ├── square of first or not.cpp │ ├── square of other or not.cpp │ ├── status teller on gpa.cpp │ ├── sum of floating point numbers.cpp │ ├── sum of two numbers.cpp │ ├── swaping using three variables.cpp │ ├── swaping using two variables.cpp │ ├── swapping using two variables method_1.cpp │ ├── swapping using two variables method_2.cpp │ ├── tax in abras.cpp │ ├── temperature conversion.cpp │ ├── time in HH MM SS.cpp │ ├── type casting.cpp │ ├── using all arithmetic operators.cpp │ ├── using conditional operator.cpp │ ├── variable and data types.cpp │ ├── volume finder.cpp │ └── vowel teller.cpp ├── 3. switch │ ├── operator switch.cpp │ ├── show day switch.cpp │ ├── switch structure.cpp │ ├── vowel teller switch.cpp │ └── weight on other planets switch.cpp ├── 4. continue │ ├── continue basics.cpp │ ├── input num continue.cpp │ └── sum of series continue.cpp ├── 5. goto │ ├── goto statement.cpp │ └── sum till enters 0 goto.cpp ├── 6. Loops │ ├── .DS_Store │ ├── do-while │ │ ├── Guess Game Character guess A to Z do while.cpp │ │ ├── Palindrome num or not.cpp │ │ ├── do while basics.cpp │ │ ├── print num 10 to 1 do while.cpp │ │ ├── print odd num given limit do while.cpp │ │ └── sum till enters 0.cpp │ ├── for │ │ ├── Perfect num or not.cpp │ │ ├── a^n.cpp │ │ ├── basics for.cpp │ │ ├── check prime num or not.cpp │ │ ├── check prime or not with another condition.cpp │ │ ├── composite num or prime num.cpp │ │ ├── highest and lowest num in a digit.cpp │ │ ├── print num 1 to 5 for.cpp │ │ ├── product of odd for.cpp │ │ ├── sum of square from 1 to n for.cpp │ │ ├── sum till enters 0 for.cpp │ │ └── table of num for.cpp │ ├── nested loops │ │ ├── pattern 3.cpp │ │ ├── pattern 4.cpp │ │ ├── pattern pyramid 1.cpp │ │ ├── pattern pyramid 2.cpp │ │ ├── pyramid pattern 5.cpp │ │ ├── shape asteric 2.cpp │ │ ├── shape asteric.cpp │ │ └── triangle 1.cpp │ └── while │ │ ├── 1 to 100 while.cpp │ │ ├── Armstrong Num or not.cpp │ │ ├── Print series 2 4 6 10....cpp │ │ ├── even n odd sum while.cpp │ │ ├── factorial of num while.cpp │ │ ├── input integers while n is positive.cpp │ │ ├── show entered num while.cpp │ │ ├── show even num while.cpp │ │ ├── square 1 to 5 while.cpp │ │ ├── sum 1 to 10 while.cpp │ │ ├── sum of given series while.cpp │ │ ├── sum till enters 0 while.cpp │ │ ├── table of num while.cpp │ │ └── while loop basics.cpp ├── 7. Array │ ├── .DS_Store │ ├── 1-D Array │ │ ├── Array Basic.cpp │ │ ├── ages between 50 and 60 array.cpp │ │ ├── binary search basics.cpp │ │ ├── days in year array.cpp │ │ ├── find sum n avg in array.cpp │ │ ├── max in array.cpp │ │ ├── min in array.cpp │ │ ├── num,squre,cube,sum Arrays.cpp │ │ ├── print integers array basic.cpp │ │ ├── print integers array using loops.cpp │ │ ├── reverse num in array.cpp │ │ └── search num in array and show its index.cpp │ ├── Multi-Dimensional Array │ │ ├── Sorting Arrays.cpp │ │ ├── binary search array.cpp │ │ ├── bubble sort in arrays.cpp │ │ ├── display in 2-d array.cpp │ │ ├── max n min in 2-d array.cpp │ │ ├── search num in array.cpp │ │ ├── sorting in arrays.cpp │ │ └── store integer in 2-D array.cpp │ └── bubble sort │ │ └── sorting using bubble sort.cpp ├── 8. Functions │ ├── GCD_using_functions.cpp │ ├── add two num functions..cpp │ ├── addition using default parameters.cpp │ ├── basics function..cpp │ ├── d bw Call by value and call by reference part 1.cpp │ ├── default parameters with example.cpp │ ├── even num in arrays using functions.cpp │ ├── even numbers in array using functions.cpp │ ├── factorial of a num functions.cpp │ ├── function overloading with example.cpp │ ├── function with parameter and return type.cpp │ ├── function with parameters.cpp │ ├── function without parameter and return type.cpp │ ├── functions and array with example.cpp │ ├── functions and array.cpp │ ├── functions and structures with example.cpp │ ├── functions overloading.cpp │ ├── global identifier functions.cpp │ ├── grade teller functions.cpp │ ├── inline functions with example.cpp │ ├── max num functions.cpp │ ├── multiple or not functions.cpp │ ├── pass by value functions.cpp │ ├── passing 2-D array to function.cpp │ ├── passing individual array element to function with example.cpp │ ├── passing individual array element to function.cpp │ ├── prime num in given range.cpp │ ├── recursions with factorial example.cpp │ ├── returning structure from function with example.cpp │ ├── static variable functions.cpp │ ├── static variables with example.cpp │ ├── sum and cube functions.cpp │ ├── sum of +ve n -ve integers using defualt parameters.cpp │ ├── table of num functions.cpp │ ├── use of prototype in function basics.cpp │ └── using all arithmetic operators functions..cpp ├── 9. Structures and unions │ ├── Assigning one structure variable to other.cpp │ ├── Nested Structure example 2.cpp │ ├── Nested Structure.cpp │ ├── Unions with example.cpp │ ├── array as member of structure.cpp │ ├── array of structures.cpp │ ├── enumerations with example.cpp │ ├── structure book b1,b2.cpp │ ├── structure data d.cpp │ ├── sturcture date d.cpp │ └── sturcture employee emp.cpp └── Pointers │ ├── Basics.cpp │ ├── DifferencesArrayPointers │ ├── Introduction.cpp │ ├── VectorStorage.cpp │ ├── operations.cpp │ └── sizeof.cpp ├── CONTRIBUTING.md ├── JAVA └── 1. Basics │ ├── 1-intro.md │ ├── 2-Basic Syntax.md │ └── 3-Hello-World.md ├── LEARN.md ├── LICENSE.md └── README.md /C++/1. Adding colors in C++/adding colors in c++.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | We can add colors to our text and background in C++: 3 | 4 | 0=black 5 | 1=blue 6 | 2=green 7 | 3=aqua 8 | 4=red 9 | 5=purple 10 | 6=yellow 11 | 7=white 12 | 8=gray 13 | 9=light blue 14 | A=Light Green 15 | B=Light Aqua 16 | C=Light Red 17 | D=Light Purple 18 | E=Light Yellow 19 | F=Bright White 20 | 21 | */ 22 | // Program to Add colors. 23 | 24 | 25 | #include 26 | #include 27 | using namespace std; 28 | 29 | int main() 30 | { 31 | //For adding colors we just need to add a single command that is:--> 32 | 33 | system("color 6f"); 34 | // 6 is for yellow and 'f' is for bright white. 35 | 36 | 37 | cout<<"\t\t\t I like adding colors to C++"<<"\n"; 38 | 39 | 40 | 41 | return 0; 42 | 43 | //See the magic in output. 44 | } 45 | -------------------------------------------------------------------------------- /C++/10. Interview's Questions/swaping using three variables.cpp: -------------------------------------------------------------------------------- 1 | // this program is to swap two numbers by using three variables. 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int a,b,temp; 7 | /* here we declare three variables in which 'temp' is assigned to 'a', 8 | and the value of 'a' is assigned to 'b', and the value of 'b' is assigned to 'temp'. */ 9 | cout<<"enter first number"; 10 | cin>>a; 11 | cout<<"enter second number"; 12 | cin>>b; 13 | cout<<"before swaping"<>a>>b; 19 | cout<<"Before Swapping:"<>a>>b; 12 | cout<<"Before Swapping:"< 7 | #include 8 | using namespace std; 9 | int main() 10 | { 11 | 12 | char ch; 13 | 14 | cout<<"Enter any Key\n"; 15 | cin>>ch; 16 | /* Now we are applying the condition for uppercase letters that is if character is Greater than or equal to A and less than or equal to Z.Then 17 | the character is a uppercase letter */ 18 | if(ch>='A' && ch<='Z') 19 | { 20 | cout<<"You Entered a Uppercase Letter\n"; 21 | } 22 | else 23 | /* Similarly we here apply the condition for lowercase letters that is if character is greater than or equal to 'a' and less than or equal to 'z'.Then 24 | the character is a lowercase letter. */ 25 | if(ch>='a' && ch<='z') 26 | { 27 | cout<<"You Entered a Lowercase Letter\n"; 28 | } 29 | else 30 | /* Now here we apply the condition for numeric digits that is if character is greater than or equal to '0' and less than or equal to '9'.Then 31 | the character is a digit. */ 32 | if(ch>='0' && ch<='9') 33 | { 34 | cout<<"You Entered a Digit\n"; 35 | } 36 | else 37 | { 38 | cout<<"You Entered a Symbol\n"; 39 | } 40 | 41 | getch(); 42 | } 43 | -------------------------------------------------------------------------------- /C++/2. Basics to if-else/Armstrong Num or not.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that inputs a number and checks whether it is an Armstrong number or not.A number is an Armstrong number if the sum of the cubes of its 3 | digits is equal to the number itself.For example, 371 is an armstrong number since 3^3+7^3+1^3=371. 4 | */ 5 | 6 | #include"iostream" 7 | using namespace std; 8 | int main() 9 | { 10 | int num; 11 | int r,n; 12 | int sum=0; 13 | 14 | cout<<"Enter Number:"; 15 | cin>>num; 16 | n=num; 17 | while(n!=0) 18 | { 19 | r=n%10; 20 | sum=sum+(r*r*r); 21 | n=n/10; 22 | } 23 | if(sum==num) 24 | cout<<"Number is an Armstrong Number\n"; 25 | else 26 | cout<<"Number is not an Armstrong Number\n"; 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /C++/2. Basics to if-else/CONSTANTS.cpp: -------------------------------------------------------------------------------- 1 | CONSTANTS: 2 | The quantities that cannot change their values during the execution of program are called constants. 3 | Constants are divided into four types: 4 | 5 | 1- STRING CONSTANT: 6 | 7 | A sequence of characters surrounded by double quotation marks is known as a string constant or a string literal. 8 | 9 | 2- FLOATING-POINT CONSTANT: 10 | 11 | The numeric values that have decimal part also,are known as float-point constants.The number 1.609 is an example 12 | of floating-point constant. 13 | 14 | 3- INTEGER CONSTANT: 15 | 16 | The numeric values that have no decimal part i.e;the whole values are known as integer constants.e.g;0,16,420. 17 | 18 | 4- CHARACTER CONSTANTS: 19 | 20 | A single character or digit or special character written between single quotation marks is called character 21 | constant.e.g; x,y,+,a,9 are its examples. 22 | 23 | 24 | THE 'const' QUALIFIER: 25 | You can also declare an identifier as constant.The 'const' qualifier is used to mark a variable as constant. 26 | The 'const' is also referred to as access modifier.The value of variable cannot be changed after the value has 27 | been assigned to the variable at the time of its declaration.e.g; to declare a variable PI and to assign value 28 | 3.417 by using 'const' qualifier we write it as; 29 | const float PI=3.417; 30 | 31 | 32 | -------------------------------------------------------------------------------- /C++/2. Basics to if-else/D bw post and prefix decrement operator.cpp: -------------------------------------------------------------------------------- 1 | //Program to differentiate between postfix and prefix decrement operator. remember postfix is a++/a-- and prefix is ++a/--a. 2 | 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int x,a; 8 | cout<<"Enter any value"<>a; 10 | cout<<"Enter 1 for postfix decrement operator OR"<>x; 13 | if(x=1) 14 | { 15 | a--; //In this case first the value of variable is used in the operation and then variable is decremented by 1. 16 | cout< 4 | using namespace std; 5 | int main() 6 | { 7 | int x,a; 8 | cout<<"Enter any value"<>a; 10 | cout<<"Enter 1 for postfix increment operator OR"<>x; 13 | if(x=1) 14 | { 15 | a++; //In this case first the value of variable is used in the operation and then variable is incremented by 1. 16 | cout<' 21 | 22 | 2- EQUAL TO '==' 23 | 24 | 3- LESS THAN '<' 25 | 26 | 4- GREATER THAN OR EQUAL TO '>=' 27 | 28 | 5- LESS THAN OR EQUAL TO '<=' 29 | 30 | 6- NOT EQUAL TO '!=' -------------------------------------------------------------------------------- /C++/2. Basics to if-else/PRECEDENCE IN ARITHMETIC OPERATORS.cpp: -------------------------------------------------------------------------------- 1 | PRECEDENCE IN ARITHMETIC OPERATORS: 2 | 3 | 1- The operators in the parenthesis () are evaluated first.If there are nested parentheses then the inner 4 | most is evaluated first. 5 | 6 | 2- The expressions are always evaluated from left to right. 7 | 8 | 3- The operators *,/,% have the highest precedence after parenthesis. 9 | 10 | 4- Lowest precedence is of + and - 11 | 12 | 5- If there are * and / operators is an expression(both have the same precedence) then the operatorswhich occurs 13 | first from left to right will be evaluated first and then the next, except you force any operator to evaluate 14 | by putting parentheses around it. -------------------------------------------------------------------------------- /C++/2. Basics to if-else/PREPOCESSOR DIRECTIVES.cpp: -------------------------------------------------------------------------------- 1 | PREPOCESSSOR DIRECTIVES: 2 | The lines beginning with # sign are directives(instructions) to the C++ compiler. 3 | The instructions that are given to the compiler before the beginning of the source code are called PREPOCESSOR 4 | DIRECTIVES.The prepocessor directives begin with a # sign followed by a word 'include' or 'define'. The part 5 | of the C++ compiler that takes action on the prepocessor directives is called prepocessor. 6 | In C++ "#include" prepocessor directive is used to include the header file into source program. -------------------------------------------------------------------------------- /C++/2. Basics to if-else/Palindrome num or not.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | write a program that inputs a number and checks whether it is a palindrome or not.A palindrome is a number that reads the same backwards as forwards 3 | such as 62526 and 4994. 4 | */ 5 | #include"iostream" 6 | using namespace std; 7 | int main() 8 | { 9 | int num; 10 | int n,rev,digit; 11 | rev=0; 12 | cout<<"Enter Number:"; 13 | cin>>num; 14 | n=num; 15 | do 16 | { 17 | digit=n%10; 18 | rev=(rev*10)+digit; 19 | n=n/10; 20 | } 21 | while(n!=0); 22 | cout<<"The reverse of the number is:"<>num; 16 | for(int i=1;i 2 | using namespace std; 3 | int main() 4 | { 5 | float length,width,area; 6 | cout<<"Write length"<>length; 8 | cout<<"Write width"<>width; 10 | area=length*width; 11 | cout<<"Area is="< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | 10 | float height,width,area; //Here we declare float data type because height and width might be floating point numbers. 11 | 12 | cout<<"Enter height and width of Square\n"; 13 | 14 | cin>>height>>width; // Remember C++ is a case-sensitive language.So, Area and area are two different variables. 15 | 16 | area=height*width; // Formula for finding the area of a square is Height*width. 17 | 18 | cout<<"Area of square="< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | 9 | { 10 | 11 | float base,height,area; 12 | //we declare float data type because base and height and area might be floating point numbers. 13 | 14 | cout<<"Enter base and height of a triangle\n"; 15 | cin>>base>>height; 16 | 17 | area=0.5*base*height; 18 | //formula for calculating area of triangle is 0.5*base*height. 19 | 20 | cout<<"Area of triangle="< 3 | #define PI 3.1417 4 | using namespace std; 5 | int main() 6 | { 7 | float r,area; 8 | cout<<"please enter radius of circle"; 9 | cin>>r; 10 | area=PI*r*r; // formula for finding area of a circle. 11 | cout<<"Area of a circle="< 3 | using namespace std; 4 | int main() 5 | { 6 | float a,b,sum,mul,div,sub,avg,square; 7 | cout<<"Enter first number"<>a; 9 | cout<<"enter second number"<>b; 11 | sum=a+b; // formula for sum. 12 | sub=a-b; // formula for subtract. 13 | mul=a*b; // formula for multiplication. 14 | div=a/b; // formula for division. 15 | avg=sum/2; // formula for average. 16 | square=a*a; // formula for square. 17 | cout<<"sum is="< 3 | using namespace std; 4 | int main() 5 | { 6 | int a,b,c,d,e; 7 | float avg; 8 | cout<<"Enter marks of your 5 subjects upto 100"<>a>>b>>c>>d>>e; 10 | avg=(a+b+c+d+e)/5; 11 | // formula for average 12 | cout<<"Average marks are:"< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int sub1,sub2,sub3,avg; 9 | 10 | cout<<"Enter Marks of three subjects\n"; 11 | cin>>sub1>>sub2>>sub3; 12 | 13 | avg=(sub1+sub2+sub3)/3; 14 | 15 | if(avg>80) 16 | { 17 | cout<<"You are above standard.\n"<<"Admission Granted!"; 18 | } 19 | else 20 | { 21 | cout<<"You are below standard.\n"<<"Admission not granted!"; 22 | } 23 | getch(); 24 | } 25 | -------------------------------------------------------------------------------- /C++/2. Basics to if-else/bill calculator.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that calculates the electricity bill.The rates of electricity per unit are as follow: 3 | If the units consumed are <=300 then the cost is RS.2 per unit. 4 | If the units consumed are >300 and <=500 then the cost is RS.5 per unit. 5 | If the units consumed exceeds 500 then the cost per unit is RS.7 per unit. 6 | A line rent of Rs.150 is also added to the total bill and a surcharge of 5% extra if the bill 7 | exceeds Rs.2000.Calculate the total bill with all the conditions given above. 8 | */ 9 | 10 | #include 11 | #include 12 | using namespace std; 13 | int main() 14 | { 15 | 16 | int units; 17 | float bill; 18 | 19 | cout<<"Please Enter the units consumed\n"; 20 | cin>>units; 21 | /* 22 | it is given that if units are less than or equal to 300 then cost is Rs.2 per unit.so in this case we have to multiply the units with 2 23 | to get the total bill. 24 | */ 25 | if(units<=300) 26 | { 27 | bill=units*2; 28 | } 29 | if(units>300 && units<=500) 30 | { 31 | bill=units*5; 32 | } 33 | if(units>500) 34 | { 35 | bill=units*7; 36 | } 37 | if(units>2000) 38 | { 39 | bill=units*7; 40 | /* 41 | It is given that if units are greater than 500 then cost is Rs.7 per unit. 42 | here the bill is greater than 2000 so the condition is also applicable there.*/ 43 | 44 | 45 | bill=bill+150+(5/100); 46 | /* 47 | It is given in the question if bill is greater than 2000 then line rent of Rs.150 and a surcharge of 5% extra is also 48 | added. so we added it in total bill. */ 49 | } 50 | cout<<"Total Bill="< 3 | using namespace std; 4 | int main() 5 | { 6 | char a; 7 | cout<<"Enter ASCII value="; 8 | cin>>a; 9 | cout<<"The value of"< 2 | using namespace std; 3 | int main() 4 | { 5 | int a,b; 6 | cout<<"Enter first number"<>a; 8 | cout<<"Enter second number"<>b; 10 | if(a%2==0 && b%2==0) 11 | { 12 | cout<<"Both numbers are EVEN!!"< 3 | using namespace std; 4 | int main() 5 | { 6 | char a; 7 | cout<<"Please enter any Alphabet or Number"; 8 | cin>>a; 9 | if(a>=65 && a<=122) 10 | /* 11 | This condition means that the value of Alphabets both upper and lowercase starts from 65 and ends on 122. 12 | So if the user entered lowercase 'z' the program checks the condition and the condition will be true because the value of 13 | lowercase 'z' in ASCII is 122.So it gives message you entered an alphabet. 14 | */ 15 | { 16 | cout<<"You entered a Alphabet"; 17 | } 18 | else 19 | //This condition apply when upper condition is false. Then it gives message you entered a digit. 20 | { 21 | cout<<"You entered a digit"; 22 | } 23 | system("pause"); 24 | } 25 | -------------------------------------------------------------------------------- /C++/2. Basics to if-else/check divisibility.cpp: -------------------------------------------------------------------------------- 1 | // This is a program to check the divisibilty. 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int a,b; 7 | cout<<"Enter a number"<>a; 9 | cout<<"Enter another number"<>b; 11 | if(a%b==0) 12 | { 13 | cout<<"a is dvisible by b."< 15 | using namespace std; 16 | int main() 17 | { 18 | int number; 19 | cout<<"Please enter the number"<>number; 21 | if(number%2==0) 22 | { 23 | cout<<"Number is even"< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | 9 | { 10 | int number; 11 | 12 | cout<<"Enter any number\n"; 13 | cin>>number; 14 | 15 | if(number!=0) 16 | { 17 | 18 | if(2*(number/2)==number)/* As we know that integer division truncates.So if user input 4 then 4/2=2 and 2*2=4 gives us the same number which is entered by user 19 | so when this happen the number is 20 | even but when user input 5 then 5/2=2 and 2*2=4 it didn't gave the same number which is entered by user so it means that the number is odd.*/ 21 | { 22 | 23 | cout<<"The number is even"; 24 | } 25 | 26 | if(2*(number/2)!=number) 27 | { 28 | 29 | cout<<"The number is odd"; 30 | 31 | } 32 | } 33 | 34 | else 35 | { 36 | cout<<"Invalid Input!!"; 37 | } 38 | 39 | return 0; 40 | } -------------------------------------------------------------------------------- /C++/2. Basics to if-else/check positive or negative number.cpp: -------------------------------------------------------------------------------- 1 | // this is a program to check the number is positive or negative using if-else statements. 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int num; 7 | cout<<"Please enter the number"<>num; 9 | if(num>=0) 10 | { 11 | cout<<"Number is positive"< 3 | using namespace std; 4 | int main() 5 | { 6 | char a; 7 | cout<<"Enter Any Character"; 8 | cin>>a; 9 | if(a>=97 && a<=122) 10 | /* 11 | here we write a>=97 && a<=122 because the ASCII value of lowercase alphabets starts from 97 and ends on 122. 12 | It means that if the user entered lowercase 'a'.Then the program check the condition a>97 && a<=122.The condition will be true 13 | because the ASCII value of lowercase 'a' is 97. so it gives message you entered a lowercase alphabet. 14 | */ 15 | { 16 | cout<<"You entered a lowercase letter."; 17 | } 18 | else 19 | { 20 | if(a>=65 && a<=90) 21 | /* 22 | here we write a>=65 && a>=90 because the ASCII value of uppercase alphabets starts from 65 and ends on 90. 23 | It means that if the user entered uppercase 'A'. Then the program checks the condition a>=65 && a<=90.The condition will be true 24 | because the ASCII value of Uppercase 'A' is 65. so it gives message you entered a uppercase alphabet. 25 | */ 26 | { 27 | cout<<"You entered a uppercase letter."; 28 | } 29 | else 30 | // This is because if user enters special characters it gives message INVALID CHARACTER. 31 | { 32 | cout<<"Invalid Character"; 33 | } 34 | } 35 | system("pause"); 36 | } 37 | -------------------------------------------------------------------------------- /C++/2. Basics to if-else/choose area n circumference.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Program that inputs radius and user's choice.It calculates area of a circle if user choose 1 and it calculates circumference of circle if user choose 2. 3 | */ 4 | 5 | #include 6 | #include 7 | #define Pi 3.14 8 | 9 | using namespace std; 10 | int main() 11 | { 12 | float radius,area,circumference; 13 | int choice; 14 | 15 | cout<<"Please Enter Radius\n"; 16 | cin>>radius; 17 | 18 | cout<<"Enter 1 for calculating Area Or Enter 2 for calculating Circumference \n"; 19 | cin>>choice; 20 | 21 | if(choice==1) 22 | { 23 | area=Pi*radius*radius; 24 | cout<<"Area="< 4 | using namespace std; 5 | int main() 6 | { 7 | int number1,number2; 8 | cout<<"Enter two integers to compare"; 9 | cin>>number1>>number2; 10 | if(number1==number2) 11 | { 12 | cout<number2) 23 | { 24 | cout<"<="<>num; 13 | for(int i=1;i<=num/2;i++) 14 | { 15 | if(num%i==0) 16 | count++; 17 | } 18 | if(count==1) 19 | cout< 14 | #include 15 | using namespace std; 16 | int main() 17 | { 18 | 19 | int n; 20 | 21 | cout<<"Enter your marks\n"; 22 | cin>>n; 23 | 24 | cout<<"Result is "<<(n>=33 ? "pass":"fail"); 25 | 26 | getch(); 27 | 28 | 29 | } 30 | -------------------------------------------------------------------------------- /C++/2. Basics to if-else/define directive.cpp: -------------------------------------------------------------------------------- 1 | THE 'define' DIRECTIVE: 2 | THE 'define' is another preprocessor directive used to define an identifier and to assign a value to it, 3 | which remains constant during the execution of program.Like other preprocessor directives,the 'define' 4 | directive is also used at the beginning of the program.The purpose of 'define' directive is similar to that 5 | of 'const' qualifier but difference between these two is: 6 | 1- 'const' is used as statement. 7 | 2- 'define' is used as prepocessor directive. 8 | 9 | syntax of 'define' directive is #define identifier value. where 10 | identifier specifies the name of identifier to which the value is to be assigned.This name cannot be used 11 | again in programs as variable name or function name. 12 | 13 | value specifies the value to be assigned to the identifier.e.g; to assign a value to a constant identifier 14 | 'PI' the preprocessor directive is written as; #define PI 3.1417 15 | -------------------------------------------------------------------------------- /C++/2. Basics to if-else/eligible or not.cpp: -------------------------------------------------------------------------------- 1 | //Program to Input age and prints message "eligible" if the age is valid enough to have a driving license otherwise print "not eligible". 2 | 3 | #include"iostream" 4 | using namespace std; 5 | int main() 6 | { 7 | float age; 8 | 9 | cout<<"Enter your age\n"; 10 | cin>>age; 11 | 12 | if(age==18 || age>18) 13 | cout<<"You are Eligible for having Driving License.\n"; 14 | else 15 | cout<<"You are not eligible for having driving license.\n"; 16 | 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /C++/2. Basics to if-else/find quotient and remainder.cpp: -------------------------------------------------------------------------------- 1 | // this program is to find the quotient and remainder. 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int dividend,divisor,quotient,remainder; 7 | // variables can also be declared by this method instead of declaring them one by one. 8 | cout<<"enter dividend"; 9 | cin>>dividend; 10 | cout<<"enter divisor"; 11 | cin>>divisor; 12 | quotient=dividend/divisor; 13 | // formula for finding quotient. 14 | // this ' / ' is used to divide a number by another number.this is arithmetic operator. 15 | remainder=dividend%divisor; 16 | // formula for finding remainder. 17 | // this ' % ' is used to find remainder.this is also arithmetic operator. 18 | cout<<"quotient ="< 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() 9 | 10 | 11 | { 12 | 13 | int a,square,cube; 14 | 15 | cout<<"Enter a number"; 16 | cin>>a; 17 | 18 | square=a*a; //formula for calculating sum. 19 | 20 | cube=a*a*a; //formula for calculating cube. 21 | 22 | cout<<"Square="<=80% A+ 8 | Between 70 and 80 A 9 | Between 60 and 70 B 10 | Between 50 and 60 C 11 | Between 40 and 50 D 12 | Between 33 and 40 E 13 | Below 33 F 14 | 15 | */ 16 | 17 | #include 18 | #include 19 | 20 | using namespace std; 21 | 22 | int main() 23 | 24 | { 25 | int marks,percentage; 26 | 27 | cout<<"Enter Your Marks Out OF 1100\n"; 28 | cin>>marks; 29 | 30 | 31 | percentage=(marks*100)/1100; 32 | 33 | if(percentage>=80) 34 | { 35 | cout<<"Your Grade is A+"; 36 | } 37 | 38 | if(percentage>=70 && percentage<80) 39 | { 40 | cout<<"Your Grade is A"; 41 | } 42 | 43 | if(percentage>=60 && percentage <70) 44 | { 45 | cout<<"Your Grade is B"; 46 | } 47 | 48 | if(percentage>=50 && percentage<60) 49 | { 50 | cout<<"Your Grade is C"; 51 | } 52 | 53 | if(percentage>=40 && percentage<50) 54 | { 55 | cout<<"Your Grade is D"; 56 | } 57 | 58 | if(percentage>=33 && percentage<40) 59 | { 60 | cout<<"Your Grade is E"; 61 | } 62 | 63 | if(percentage<33) 64 | { 65 | cout<<"Your Grade is F\n"; 66 | cout<<"You are Fail"; 67 | } 68 | getch(); 69 | } 70 | -------------------------------------------------------------------------------- /C++/2. Basics to if-else/grades calculator.cpp: -------------------------------------------------------------------------------- 1 | // This is a program for grade calculator. 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int a; 7 | cout <<"Enter marks"<>a; 9 | if(a>=90 && a<=100) 10 | { 11 | cout<<"You have A+ grade."<=80 && a<=89) 15 | { 16 | cout<<"You have A- grade."<=70 && a<=79) 21 | { 22 | cout<<"You have A grade."<=60 && a<=69) 27 | { 28 | cout<<"You have B grade."<=50 && a<=59) 33 | { 34 | cout<<"you have C grade"<=33 && a<=49) 39 | { 40 | cout<<"you have D grade"; 41 | } 42 | else 43 | { 44 | cout<<"you are fail"<>num_1>>num_2>>num_3; 10 | 11 | if(num_1==num_2 && num_2==num_3) 12 | { 13 | cout<<"All the numbers are equal.\nYou must have to enter three different numbers to get the greatest number.\n"; 14 | } 15 | else 16 | if(num_1==num_2 || num_1==num_3 || num_2==num_3) 17 | { 18 | cout<<"Two of the entered numbers are equal.\nYou must have to enter three different numbers to get the greatest number."; 19 | } 20 | else 21 | if(num_1>num_2) 22 | { 23 | if(num_1>num_3) 24 | { 25 | cout<num_3) 33 | { 34 | cout< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int a,b,c; 9 | 10 | cout<<"Enter three numbers\n"; 11 | cin>>a>>b>>c; 12 | 13 | if(a>b) 14 | { 15 | if(a>c) 16 | { 17 | cout<<"'a' is greater\n"<a) 21 | { 22 | if(b>c) 23 | { 24 | cout<<"'b' is greater\n"<a) 28 | { 29 | if(c>b) 30 | { 31 | cout<<"'c' is greater\n"< 3 | using namespace std; 4 | int main() 5 | { 6 | float n1,n2,n3; 7 | // Float is a data type. It is used for decimal numbers/real numbers. It is also used for both integers and decimals numbers. 8 | cout<<"enter three numbers"<>n1>>n2>>n3; 10 | if(n1>n2 && n1>n3) 11 | { 12 | cout<<"n1 is greater"<n1 && n2>n3) 15 | { 16 | cout<<"n2 is greater"<n1 && n3>n2) 19 | { 20 | cout<<"n3 is greater"< 2 | /*header files contain definitions of functions and variables, which is imported or used into any c/c++ program by 3 | using the pre processor #include statement. Header file have an extension ".h" which contains c/c++ 4 | function declaration and macro definition.*/ 5 | /*iostream provides basic input and output services for c++ programs. 6 | iostream uses objects cin,cout,cerr,and clog for sending data to and from the standard streams input,output,error(unbuffered), 7 | and log(buffered) respectively.*/ 8 | using namespace std; 9 | /* std is the standard namespace. 10 | cin,cout and a lot of things are defined in it. 11 | this means that one way to call them is by using std::cout and std::cin. 12 | */ 13 | int main() 14 | /* main() is a function which is called by the '_start' function. 15 | In C, default return value is 'int' so when we write main() it is equivalent to 'int main()' where main is a function which takes nothing 16 | as an argument and returns integer. 17 | This is because the main function in C++ returns a int value at exit. 18 | */ 19 | { 20 | cout<<"hello"< 10 | using namespace std; 11 | int main() 12 | { 13 | int x; 14 | cout<<"enter the age"; 15 | cin>>x; 16 | if(x>20) 17 | // this' > ' is a relational operator.so here it means x is greater than 20. 18 | { 19 | cout<<"you are elder"< 3 | using namespace std; 4 | int main() 5 | { 6 | float inches,mm,x,y,z; 7 | cout<<"Enter Inches and Millimeters"; 8 | cin>>x>>y; 9 | mm=x/25.4; 10 | inches=y*25.4; 11 | cout<<"Enter 1 to convert inches into mm or Enter 2 to convert mm into inches"; 12 | cin>>z; 13 | if(z==1) 14 | { 15 | cout<<"Inches in mm are="< 3 | using namespace std; 4 | int main() 5 | { 6 | int age1,age2; 7 | cout<<"Enter Amir's age"<>age1; 9 | cout<<"Enter the age of Tania"<>age2; 11 | if(age1>age2) 12 | { 13 | cout<<"Amir is elder than Tania"< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int year; 9 | 10 | cout<<"Enter a year\n"; 11 | cin>>year; 12 | 13 | /* 14 | Each leap year has 366 days instead of the usual 365, 15 | by extending February to 29 days rather than the common 28. 16 | These extra days occur in years which are multiples of four. 17 | Therefore we apply condition if year%4==0 means if years module 4 18 | gives remainder 0 then it is a leap year. 19 | */ 20 | 21 | if(year%4==0) 22 | 23 | cout<<"Year is a leap year\n"; 24 | 25 | else 26 | cout<<"Year is not a leap year"; 27 | 28 | getch(); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /C++/2. Basics to if-else/max or min of 5 num.cpp: -------------------------------------------------------------------------------- 1 | //Program that inputs five integers and find the smallest and the largest integer. 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int a,b,c,d,e,min,max; 9 | 10 | cout<<"Enter Five numbers\n"; 11 | cin>>a>>b>>c>>d>>e; 12 | 13 | max=min=a; /*Here we declare minimum and maximum number is equal to 'a'. So when it checks the condition (bmax) it is checking actually 28 | if b>a; so if the condition is true then maximum is now 'b'. Now it checks the next condition (c>max) it is checking actually 29 | c>b if condition is true then maximum is now 'c'.It checks the other conditions similarly. If one or more of the condition is 30 | false then it skips that condition and goes to next condition and so on and then finds the maximum and minimum number. */ 31 | 32 | 33 | 34 | if(b>max) 35 | max=b; 36 | if(c>max) 37 | max=c; 38 | if(d>max) 39 | max=d; 40 | if(e>max) 41 | max=e; 42 | 43 | cout<<"Maximum Number="<>num_1>>num_2>>num_3; 11 | 12 | if(num_1==num_2 || num_1==num_3 || num_2==num_3) 13 | cout<<"Two numbers are equal if you want to find middle number you must have to enter three numbers."; 14 | else if(num_1>num_2 && num_1num_3 && num_1 3 | using namespace std; 4 | int main() 5 | { 6 | float miles,km,x,y,z; 7 | cout<<"Please Enter Miles and Kilometers"<>x>>y; 9 | miles=x*1.609; //formula for converting miles into km. 10 | km=y*0.6213; //formula for converting km into miles. 11 | cout<<"Enter 1 for converting miles into kilometers or Enter 2 for converting kilometers into miles"; 12 | cin>>z; 13 | if(z==1) 14 | { 15 | cout<<"Miles in km are="< 3 | using namespace std; 4 | int main() 5 | { 6 | float miles,km; 7 | cout<<"Please Enter Miles"; 8 | cin>>miles; 9 | km=miles*1.609; //Formula for converting miles into km. 10 | cout<<"Miles in Km are="< 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() 9 | 10 | { 11 | int a,b; 12 | 13 | cout<<"Enter two numbers\n"; 14 | cin>>a>>b; 15 | 16 | if(b%a==0) 17 | /* 18 | Let user enter first integer 6 and second integer 36. Then condition is applied that is 36%6 which is equal to 0. 19 | So, the condition is true so first integer is a multiple of second integer because 6*6=36. 20 | */ 21 | 22 | { 23 | cout<<"First number is a multiple of second"; 24 | } 25 | else 26 | { 27 | cout<<"First number is not multiple of second number"; 28 | } 29 | 30 | getch(); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /C++/2. Basics to if-else/nested if-1.cpp: -------------------------------------------------------------------------------- 1 | // now we are starting nested if statement. 2 | /* syntax of nested if statement is 3 | if(condition-1) 4 | { 5 | //executes only if condition-1 one is true. 6 | if(condition-2) 7 | { 8 | //executes only if condition-2 is true. 9 | statements; 10 | } 11 | } 12 | . 13 | */ 14 | //this is a program for nested if statements. 15 | #include 16 | using namespace std; 17 | int main() 18 | { 19 | int a=5,b=3,c=9,d=6; 20 | 21 | /*here first of all it checks the statement a>b. as a>b so it goes inside the if. then it checks the statement c>d. which is 22 | also true . then it prints the message good job. 23 | if the statement a>b is false then it didn't go inside and give no output. */ 24 | if(a>b) 25 | { 26 | if(c>d) 27 | { 28 | cout<<"good job"< 14 | using namespace std; 15 | int main() 16 | { 17 | int a=5,b=3,c=9,d=6; 18 | 19 | /*here first of all it checks the statement a>b. as a>b so it goes inside the if. then it checks the statement c>d. which is 20 | also true . then it prints the message good job. 21 | if the statement a>b is false then it didn't go inside and give no output. */ 22 | if(a>b) 23 | { 24 | if(cb. so it comes inside and check cd which is true so it prints perfect. */ 26 | { 27 | cout<<"good job"<d) 30 | { 31 | cout<<"perfect"; 32 | } 33 | } 34 | 35 | system("pause"); 36 | } 37 | -------------------------------------------------------------------------------- /C++/2. Basics to if-else/nested if-3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main () { 5 | // local variable declaration: 6 | int a = 100; 7 | int b = 200; 8 | 9 | // check the condition 10 | if( a == 100 ) 11 | { 12 | // if condition is true then check the following 13 | if( b == 200 ) 14 | { 15 | // if condition is true then print the following 16 | cout << "Value of a is 100 and b is 200" << endl; 17 | } 18 | } 19 | system("pause"); 20 | } 21 | -------------------------------------------------------------------------------- /C++/2. Basics to if-else/numbers are equal or not.cpp: -------------------------------------------------------------------------------- 1 | //Program that input 2 numbers from the user and find either both are equal or not. 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | 9 | { 10 | float a,b; 11 | 12 | cout<<"Enter two numbers\n"; 13 | cin>>a>>b; 14 | 15 | if(a==b) 16 | { 17 | cout<<"Both the numbers are equal"; 18 | } 19 | else 20 | { 21 | cout<<"Numbers are not equal"< 3 | using namespace std; 4 | int main() 5 | { 6 | int number; 7 | cout<<"enter the number"; 8 | cin>>number; 9 | // cin is used to store a value in variable. 10 | cout<<"you entered"< 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main() 9 | 10 | { 11 | int n,a,b; 12 | 13 | cout<<"Enter a number\n"; 14 | cin>>n; 15 | 16 | a=n/100; 17 | 18 | n=n%100; 19 | 20 | b=n/10; 21 | 22 | n=n%10; 23 | 24 | cout<<"Number in reverse order is="< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | 9 | { 10 | int number,a,b,c,d; 11 | 12 | cout<<"Enter a five digit number\n"; 13 | cin>>number; 14 | 15 | a=number/10000; 16 | //let user enters a number that is 12345 when 12345/10000 we get 1.2345 but only 1 is stored in 'a' and decimal part is truncated in C++. 17 | number=number%10000; 18 | //when 12345%1000 we get the remainder 2345 so now number=2345 19 | 20 | b=number/1000; 21 | //now when 2345/1000 we get 2.345 but only 2 is stored in 'b' and decimal part is truncated.Because C++ truncated decimal points. 22 | number=number%1000; 23 | //now when 2345%1000 we get remainder 345 so now number=345 24 | 25 | c=number/100; 26 | //now when 345/100 we get 3.45 but only 3 is stored in 'c' and decimal part is truncated. 27 | number=number%100; 28 | //now when 345%100 we get remainder 45 so now number=45. 29 | 30 | d=number/10; 31 | //now when 45/10 we get 4.5 but only 4 is stored in 'd' and decimal part is truncated. 32 | number=number%10; 33 | //now when 45%10 we get remainder 5 so now number=5. 34 | 35 | cout<<"Number in reverse order is\n"< 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() 10 | 11 | { 12 | int salary,grade,bonus; 13 | 14 | cout<<"Please Enter your Salary\n"; 15 | cin>>salary; 16 | 17 | cout<<"Please Enter your Grade\n"; 18 | cin>>grade; 19 | 20 | if(grade>15) 21 | { 22 | bonus=salary*50/100; // Because 50% = 50/100. 23 | } 24 | else 25 | { 26 | bonus=salary*25/100; //Because 25%= 25/100. 27 | } 28 | 29 | salary=salary+bonus; 30 | 31 | cout<<"Your Total Salary with Bonus="< 4 | using namespace std; 5 | int main() 6 | { 7 | int a,b,c,d,n; 8 | cout<<"enter a 4 digit number"<>n; 10 | a=n/1000; 11 | n=n%1000; 12 | b=n/100; 13 | n=n%100; 14 | c=n/10; 15 | d=n%10; 16 | cout< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int a,b; 9 | cout<<"Enter two integers\n"; 10 | cin>>a>>b; 11 | 12 | if(a*a==b) 13 | { 14 | cout<<"Second number is square of first"; 15 | } 16 | else 17 | { 18 | cout<<"Second number is not square of first"; 19 | } 20 | getch(); 21 | } 22 | -------------------------------------------------------------------------------- /C++/2. Basics to if-else/square of other or not.cpp: -------------------------------------------------------------------------------- 1 | //program that inputs two numbers and find if second number is square of first or not. 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | 9 | { 10 | 11 | int a,b; 12 | 13 | cout<<"Enter two numbers\n"; 14 | cin>>a>>b; 15 | 16 | if(a*a==b) 17 | { 18 | cout<<"second number is square of first"; 19 | } 20 | else 21 | { 22 | cout<<"second number is not square of first"; 23 | } 24 | 25 | system("pause"); 26 | } 27 | -------------------------------------------------------------------------------- /C++/2. Basics to if-else/status teller on gpa.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Program to Determine status of student (dropped/probation/safe).Student is dropped if gpa is less than 1.7, on probation if gpa is greater than or equal to 1.7 but less than 3 | 2.0,otherwise student is safe. 4 | */ 5 | 6 | #include"iostream" 7 | using namespace std; 8 | int main() 9 | { 10 | float gpa; 11 | 12 | cout<<"Please enter your GPA\n"; 13 | cin>>gpa; 14 | 15 | if(gpa>=0.0 && gpa<=4.0) 16 | { 17 | if(gpa<1.7) 18 | cout<<"You are Dropped\n"; 19 | else if(gpa>=1.7 && gpa<2.0) 20 | cout<<"You are on Probation\n"; 21 | else 22 | cout<<"You are Safe\n"; 23 | } 24 | else 25 | { 26 | cout<<"Invalid GPA!!\n"; 27 | } 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /C++/2. Basics to if-else/sum of floating point numbers.cpp: -------------------------------------------------------------------------------- 1 | //Program that adds two floating point numbers. 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | float x,y,sum; //we declare float data type because we are adding floating point numbers. 7 | 8 | cout<<"Enter two floating point numbers\n"; // " \n " is used for a new line and is always used in double quotation. 9 | cin>>x>>y; 10 | sum=x+y; //formula for calculating sum. 11 | cout<<"Sum of floating point numbers="< 3 | using namespace std; 4 | // this " ; " is a statement terminator.It is used to end a statement. 5 | int main() 6 | { 7 | int a; 8 | int b; 9 | // two variables 'a' and 'b' are declared of type int. 10 | int sum; 11 | // sum is declared to calculate the sum. 12 | cout<<"enter first number"; 13 | cin>>a; 14 | // first integer is stored in 'a' 15 | cout<<"enter second number"; 16 | cin>>b; 17 | // second integer is stored in 'b' 18 | sum=a+b; 19 | // this is the formula to calculate the sum. 20 | cout<<"sum is"< 3 | using namespace std; 4 | int main() 5 | { 6 | int a,b,temp; 7 | /* here we declare three variables in which 'temp' is assigned to 'a', 8 | and the value of 'a' is assigned to 'b', and the value of 'b' is assigned to 'temp'. */ 9 | cout<<"enter first number"; 10 | cin>>a; 11 | cout<<"enter second number"; 12 | cin>>b; 13 | cout<<"before swaping"< 3 | using namespace std; 4 | int main() 5 | { 6 | int a,b; 7 | // we declare two variables. 8 | cout<<"Enter first value"<>a; 10 | cout<<"Enter second value"<>b; 12 | cout<<"before swaping"<>a>>b; 19 | cout<<"Before Swapping:"<>a>>b; 12 | cout<<"Before Swapping:"<>income; 22 | 23 | tax1=5000*0.0; 24 | tax2=10000*0.10; 25 | tax3=20000*0.15; 26 | 27 | if(income>35000) 28 | { 29 | tax=income-35000; 30 | tax4=tax*0.20; 31 | } 32 | 33 | if(income<=5000) 34 | totalTax=tax1; 35 | 36 | if(income<=15000 && income>5000) 37 | totalTax=tax1+tax2; 38 | 39 | if(income<=35000 && income>15000) 40 | totalTax=tax1+tax2+tax3; 41 | 42 | if(income>35000) 43 | totalTax=tax1+tax2+tax3+tax4; 44 | 45 | cout<<"Your Total Tax on "< 3 | using namespace std; 4 | int main() 5 | { 6 | float F,temp; 7 | cout<<"Enter temperature in celcius"<>temp; 9 | F=((temp*9/5)+32); 10 | // Formula for converting temperature celcius to fahrenheit. 11 | cout<<"Temperature in Farenhite is"< 5 | 6 | 7 | #include 8 | /* This header file means "console input output". 9 | we use this header file because we use getch(); in this program and to use getch(); we must include its header file.*/ 10 | 11 | using namespace std; 12 | 13 | int main() 14 | 15 | { 16 | 17 | int sec,hour,min,seconds; // 'seconds' variable is used to store the input entered by the user. 18 | 19 | cout<<"Enter time in Seconds\n"; 20 | cin>>seconds; 21 | 22 | 23 | hour=seconds/3600; 24 | /* Let user enters 5300 seconds. then to calculate hour 5300/3600 and we get 1 because decimal part is truncated in c++. It stores 1 in 25 | hour which means that hour=1. we divided seconds by 3600 because 1hour=3600s */ 26 | 27 | 28 | seconds=seconds%3600; 29 | /* Now 5300%3600 and we get remainder 1700. So now seconds=1700. Actually we are using remainder theorem. */ 30 | 31 | 32 | min=seconds/60; 33 | /* Now 1700/60 and we get 28 and because decimal part is truncated.So min=28. we divided min by 60 because in 1hour there is 60mins. */ 34 | 35 | 36 | sec=seconds%60; 37 | /* now 1700%60 and we get 20 in remainder so sec=20. */ 38 | cout<<"Time in format HH:MM:SS="< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int a,b,sum,sub,mul,div,rem; 10 | 11 | cout<<"Enter two numbers"; 12 | 13 | cin>>a>>b; 14 | 15 | sum=a+b; 16 | 17 | sub=a-b; 18 | 19 | mul=a*b; 20 | 21 | div=a/b; 22 | 23 | rem=a%b; 24 | 25 | cout<<"sum="< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int n; 9 | 10 | cout<<"Enter a number\n"; 11 | cin>>n; 12 | 13 | if(n!=0) 14 | (n%3==0 ? cout<<"Divisible by 3\n" : cout<<"Not Devisible by 3\n"); 15 | 16 | else 17 | cout<<"Not Possible"; 18 | getch(); 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /C++/2. Basics to if-else/volume finder.cpp: -------------------------------------------------------------------------------- 1 | //This is a program to calculate the volume. As we know V= Length*Width*Height. 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | float l,w,h,volume; 7 | cout<<"Enter Length="; 8 | cin>>l; 9 | cout<<"Enter width="; 10 | cin>>w; 11 | cout<<"Enter Height="; 12 | cin>>h; 13 | volume=l*w*h; 14 | //Formula for calculating volume. 15 | cout<<"Volume="< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | char ch; 9 | 10 | cout<<"Enter a Character\n"; 11 | cin>>ch; 12 | 13 | if(ch=='A' || ch=='a' || ch=='E' || ch=='e' || ch=='I' || ch=='i' || ch=='O' || ch=='o' || ch=='U' || ch=='u') 14 | 15 | /* 16 | As we know that vowels are (A,E,I,O,U || a,e,i,o,u). So apply the condition that if the typed character is equal to uppercase 'A' or lowercae 17 | 'a' or similarly uppercase 'E' or lowercase 'e' or so on then the character is vowel else it is not a vowel. 18 | */ 19 | 20 | { 21 | cout<<"You entered a Vowel="< 8 | #include 9 | using namespace std; 10 | int main() 11 | { 12 | float a,b; 13 | char op; 14 | 15 | cout<<"Enter a Floating point number\n"; 16 | cin>>a; 17 | 18 | cout<<"Enter an operator\n"; 19 | cin>>op; 20 | 21 | cout<<"Enter a Floating point number\n"; 22 | cin>>b; 23 | 24 | switch(op) 25 | { 26 | { 27 | case '+' : 28 | cout< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | 9 | int n; 10 | cout<<"Enter a number\n"; 11 | cin>>n; 12 | 13 | switch(n)/* In this step we write within the parenthesis that variable to which we are applying switch statement.As we are applying switch 14 | statement to 'n' therefore we write it in parenthesis after switch. */ 15 | { 16 | case 1: 17 | /* 18 | we write here 1 after case because it represent the input if user enters 1 so this case is applicable and it prints Monday on the screen. 19 | similarly other cases. 20 | */ 21 | { 22 | cout<<"Monday"; 23 | break;//break; is used to exit from switch statement.if break is not used,all case blocks that come after the matching case will also executed. 24 | } 25 | case 7: 26 | { 27 | cout<<"Sunday"; 28 | break; 29 | } 30 | default: 31 | /* 32 | the default label appears at the end of all case blocks.It is executed only when the result of expression does not match with any case label. 33 | its use is optional.Its position is not fixed. It may be placed before the first case statement or after the last one. 34 | */ 35 | { 36 | cout<<"Invalid input"; 37 | } 38 | 39 | } 40 | getch(); 41 | 42 | } 43 | -------------------------------------------------------------------------------- /C++/3. switch/switch structure.cpp: -------------------------------------------------------------------------------- 1 | 'switch' Structure: 2 | 3 | The switch statement is another conditional structure.It is a good alternative of nested if-else. 4 | It can be easily used when there are many choices available and only one should be executed. 5 | 6 | Syntax: 7 | 8 | switch(expression) 9 | { 10 | case constant 1; 11 | statement(s); 12 | break; 13 | 14 | case constant 2; 15 | statement(s); 16 | break; 17 | : 18 | : 19 | : 20 | case constant N; 21 | statement(s); 22 | break; 23 | 24 | default; 25 | statement(s); 26 | } 27 | 28 | "break;" causes exit from case body. -------------------------------------------------------------------------------- /C++/3. switch/vowel teller switch.cpp: -------------------------------------------------------------------------------- 1 | //Program that inputs a character from the user and checks whether it is a vowel or consonant using switch. 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | char ch; //we declare char data type because characters are stored in char. 9 | 10 | cout<<"Enter an Alphabet\n"; 11 | cin>>ch; 12 | 13 | 14 | switch(ch) 15 | { 16 | { //here we apply the cases for vowels that are A,E,I,O,U/a,e,i,o,u so if user enter any of these it prints ita a vowel. 17 | case 'A' : 18 | case 'a' : 19 | cout<<"You Entered Vowel\n"; 20 | break; 21 | } 22 | { 23 | case 'E' : 24 | case 'e' : 25 | cout<<"You Entered an Vowel\n"; 26 | break; 27 | } 28 | { 29 | case 'I' : 30 | case 'i' : 31 | cout<<"You Entered a Vowel\n"; 32 | break; 33 | } 34 | { 35 | case 'O' : 36 | case 'o' : 37 | cout<<"You Entered a Vowel\n"; 38 | break; 39 | } 40 | { 41 | case 'U' : 42 | case 'u' : 43 | cout<<"You Entered a Vowel\n"; 44 | break; 45 | } 46 | { 47 | default: //if any of the cases didn't match then default shows the message its not a vowel. 48 | cout<<"Not Vowel\n"; 49 | } 50 | } 51 | 52 | getch(); 53 | } 54 | -------------------------------------------------------------------------------- /C++/4. continue/continue basics.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 'continue' statement: 3 | The Continue statement is used in the body of the loop.It is used to move the control to the start of the loop body.when this statement is 4 | executed in the loop body,the remaining statements of current iteration are not executed.The control directly moves to the next iteration. 5 | */ 6 | //Example 7 | //program to print HELLOW! five times using for loop use continue statement inside the loop body. 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | int main() 13 | { 14 | int x; 15 | 16 | for(x=1;x<=5;x++) 17 | { 18 | 19 | cout<<"HELLO!\n"< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int a, n; 9 | 10 | for(a=1;a<=5;a++) 11 | { 12 | cout<<"Enter a Number\n"; 13 | cin>>n; 14 | 15 | if(n<=0) 16 | { 17 | continue;//if this condition is true then it take input from the user and didn't show message you entered that number. 18 | } 19 | cout<<"You Entered\n"< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int i,sum=0; 9 | 10 | for(i=1;i<=100;i++) 11 | { 12 | if(i%2==0)//Here we apply the condition that if number is even then it didn't go to next step.It goes up to loop and again executed till the if condition is false. 13 | { 14 | continue; 15 | } 16 | sum=sum+i;//when if condition is false then it comes here and calculates the sum.And after calculating sum it goes up to loop till the condition in loop is true. 17 | } 18 | cout<<"Sum="< 13 | #include 14 | using namespace std; 15 | int main() 16 | { 17 | 18 | int n=1; 19 | 20 | comehere: //It is the label.The control is directly move to this location of program using goto statement.label is followed by a colon(:). 21 | cout<C++\n"< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int n,c,sum; 9 | c=1; 10 | sum=0; 11 | 12 | addition: 13 | cout<<"Enter a Number\n"; 14 | cin>>n; 15 | 16 | sum+=n;//It means that sum=sum+n; 17 | 18 | if(n!=0)//The loop executes till the user enters 0. 19 | 20 | goto addition; 21 | 22 | cout<<"Total Sum="< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | 7 | { 8 | char guess; 9 | int tryNum=1; 10 | 11 | cout<<" Its a Guess Game.\n\n\nI have a character for you that is between A to Z.\nYou have to Guess that Character.\nYou will be given three chances.\n "; 12 | 13 | cout<<"\n\n\n LET'S PLAY\n\n\n"; 14 | 15 | 16 | do 17 | { 18 | cout<<"PLease Enter your Guess by pressing a Character from A to Z.\n"; 19 | cin>>guess; 20 | 21 | 22 | if(guess=='U' || guess=='u' || guess=='S' || guess=='s' || guess=='C' || guess=='c' ) 23 | { 24 | 25 | cout<<"\n\n\n\n CONGRATULATIONS !!! \n\n\n"; 26 | 27 | cout<<" You Guess the right Character \n\n\n\n "; 28 | 29 | tryNum=4; 30 | } 31 | 32 | else 33 | { 34 | 35 | cout<<"\n\n Wrong Guess! \n\n "; 36 | 37 | tryNum+=1; 38 | 39 | } 40 | 41 | 42 | 43 | } 44 | 45 | while(tryNum<=3); 46 | 47 | 48 | return 0; 49 | } -------------------------------------------------------------------------------- /C++/6. Loops/do-while/Palindrome num or not.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | write a program that inputs a number and checks whether it is a palindrome or not.A palindrome is a number that reads the same backwards as forwards 3 | such as 62526 and 4994. 4 | */ 5 | #include"iostream" 6 | using namespace std; 7 | int main() 8 | { 9 | int num; 10 | int n,rev,digit; 11 | rev=0; 12 | cout<<"Enter Number:"; 13 | cin>>num; 14 | n=num; 15 | do 16 | { 17 | digit=n%10; 18 | rev=(rev*10)+digit; 19 | n=n/10; 20 | } 21 | while(n!=0); 22 | cout<<"The reverse of the number is:"<In while loop,condition comes before the body of the loop. 19 | In do-while loop,condition comes after the body of the loop. 20 | 21 | 22 | --> If condition is false in the beginning,while loop is never executed. 23 | do-while is executed at least once even if condition is false in the beginning. 24 | */ -------------------------------------------------------------------------------- /C++/6. Loops/do-while/print num 10 to 1 do while.cpp: -------------------------------------------------------------------------------- 1 | //Program that displays back-counting from 10 to 1 using do-while loop. 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | 9 | int n; 10 | n=10; 11 | 12 | do 13 | { 14 | cout<=1); //condition is applied n>=1 the loop is executed till the condition is true.It is terminated when condition is false. 21 | 22 | getch(); 23 | 24 | } 25 | -------------------------------------------------------------------------------- /C++/6. Loops/do-while/print odd num given limit do while.cpp: -------------------------------------------------------------------------------- 1 | //Program that gets starting and ending point from the user and displays all odd numbers in the given range using do-while loop. 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int s,e,c; 9 | 10 | cout<<"Enter starting number\n"; 11 | cin>>s; 12 | 13 | cout<<"Enter ending number\n"; 14 | cin>>e; 15 | 16 | c=s; 17 | do 18 | { 19 | if(c%2!=0)//The numbers which do not give remainder 0 when divided with 2 are odd numbers.So here we applied the condition for odd numbers. 20 | cout< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int n,c,sum; 9 | c=1; 10 | sum=0; 11 | 12 | do 13 | { 14 | cout<<"Enter a Number\n"; 15 | cin>>n; 16 | 17 | sum+=n;//It means that sum=sum+n; 18 | } 19 | while(n!=0);//The loop executes till the user enters 0. 20 | 21 | cout<<"Total Sum="<>num; 16 | for(int i=1;i> base; 12 | 13 | cout<<"Enter exponent\n"; 14 | cin>>exponent; 15 | 16 | for(int count=1; count<=exponent; count++) 17 | { 18 | result= result*base; 19 | } 20 | 21 | cout< 4 | using namespace std; 5 | int main() 6 | { 7 | int num; 8 | int count=0; 9 | 10 | cout<<"Enter Number\n"; 11 | cin>>num; 12 | 13 | for(int counter=1;counter<=num/2;counter++) 14 | { 15 | if(num%counter==0) 16 | { 17 | count=count+1; 18 | } 19 | } 20 | 21 | if(count==1) 22 | { 23 | cout<<"Number is prime\n"; 24 | } 25 | else 26 | { 27 | cout<<"Number is not prime\n"; 28 | } 29 | 30 | return 0; 31 | 32 | 33 | } 34 | 35 | -------------------------------------------------------------------------------- /C++/6. Loops/for/check prime or not with another condition.cpp: -------------------------------------------------------------------------------- 1 | //Program to check the number is prime or not. 2 | 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | int num; 8 | int count=0; 9 | 10 | cout<<"Enter Number\n"; 11 | cin>>num; 12 | 13 | for(int counter=2;counter<=num;counter++) 14 | { 15 | if(num%counter==0) 16 | { 17 | count=count+1; 18 | } 19 | } 20 | 21 | if(count==1) 22 | { 23 | cout<<"Number is prime\n"; 24 | } 25 | else 26 | { 27 | cout<<"Number is not prime\n"; 28 | } 29 | 30 | return 0; 31 | 32 | 33 | } 34 | 35 | -------------------------------------------------------------------------------- /C++/6. Loops/for/composite num or prime num.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | write a program that inputs an integer and prints if it is prime or composite.A number is prime if it has factors 1 and itself,otherwise it is a 3 | composite number. 4 | */ 5 | #include"iostream" 6 | using namespace std; 7 | int main() 8 | { 9 | int num; 10 | int count=0; 11 | cout<<"Enter Number:"; 12 | cin>>num; 13 | for(int i=1;i<=num/2;i++) 14 | { 15 | if(num%i==0) 16 | count++; 17 | } 18 | if(count==1) 19 | cout<>num; 14 | int n; 15 | n=num; 16 | high=n%10; 17 | low=n%10; 18 | n=n/10; 19 | for(int i=n;i>1;i=i/10) 20 | { 21 | rem=i%10; 22 | if(rem>high) 23 | high=rem; 24 | if(rem 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int n; 9 | 10 | for(n=1;n<=5;n=n+1) 11 | { 12 | cout< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int product,num; 9 | product=1; 10 | 11 | for(num=1;num<=10;num=num+2) 12 | /* 13 | here we use num=num+2 because we are given to find the product of only odd numbers as we initialized num=1 so num=1+2 implies that num=3 and 14 | so on in this way it always give a odd number. 15 | */ 16 | { 17 | product=product*num; 18 | } 19 | cout<<"Result is\n"< 5 | #include 6 | using namespace std; 7 | int main() 8 | { 9 | int n,c,sum=0; 10 | 11 | cout<<"Enter a number\n"; 12 | cin>>n; 13 | 14 | for(c=1;c<=n;c++) 15 | { 16 | sum=sum+(c*c);//c*c finds the square of number that is stored in c and then added it into sum. 17 | } 18 | cout<<"sum="< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int n,c,sum; 9 | sum=0; 10 | 11 | 12 | for(n=1;n!=0;) 13 | 14 | { 15 | cout<<"Enter a Number\n"; 16 | cin>>n; 17 | 18 | sum+=n;//It means that sum=sum+n; 19 | } 20 | 21 | 22 | cout<<"Total Sum="< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int table,length,c; 9 | 10 | cout<<"Enter number for table\n"; 11 | cin>>table; 12 | 13 | cout<<"Enter Length of table\n"; 14 | cin>>length; 15 | 16 | for(c=1;c<=length;c++) 17 | { 18 | cout< 13 | #include 14 | using namespace std; 15 | int main() 16 | { 17 | 18 | /* 19 | In nested for loop the outer loop is responsible for number of rows, the first inner loop is responsible for spaces, and the second inner loop is 20 | responsible for printing(what you desire).So, here we declare variables, for rows we use 'r' for spaces we use 'sp' for printing we use 'p', and 21 | for input a number we use 'no'. 22 | */ 23 | 24 | int r,sp,p,no,n; 25 | 26 | cout<<"Enter a number for which you wanted to print pattern\n"; 27 | cin>>no; 28 | 29 | n=no; 30 | 31 | for(r=1;r<=no;r++)//As we need rows which is equal to number that user input.so we initialize r=1 and apply condition r<=no and r++. 32 | { 33 | 34 | for(sp=1;sp<=n;sp++)/*As we need spaces that are equal to number input by user.Let suppose user enters 5 so it prints 5 spaces first and went 35 | out of loop and execute n-- it subtract 1 from the number and we get 4 spaces next time and so on. */ 36 | { 37 | 38 | cout<<" "; 39 | 40 | } 41 | n--; 42 | 43 | 44 | for(p=1;p<=r;p++)/*in this we initialized p=1 and p<=r so it prints number that is the row number .As we have to print '*' therefore 45 | we just cout<<"*"; 46 | Remember we also have to add space after printing the number see pattern there is a space after every number.*/ 47 | { 48 | cout<<"*"<<" " ; 49 | } 50 | 51 | cout< 12 | #include 13 | using namespace std; 14 | int main() 15 | { 16 | 17 | /* 18 | In nested for loop the outer loop is responsible for number of rows, the first inner loop is responsible for spaces, and the second inner loop is 19 | responsible for printing(what you desire).So, here we declare variables, for rows we use 'r' for spaces we use 'sp' for printing we use 'p'. 20 | */ 21 | 22 | int r,sp,p; 23 | 24 | for(r=1;r<=5;r++)//As we need 5 rows in our pattern there we initialize r=1 and apply condition r<=5 and r++. 25 | { 26 | 27 | for(sp=5;sp>=r;sp--)/*As we need 5 spaces in first row,4 in next row similarly,3,2,1 spaces required.There we initialize sp=5(as we need 5 28 | spaces in first row) and sp>=r means that it is greater than or equal to number of rows and sp--(it subtracts 1 from 5) so we will get 4 spaces 29 | in next line and so on. */ 30 | { 31 | 32 | cout<<" "; 33 | } 34 | 35 | for(p=1;p<=r;p++)/*in this we initialized p=1 and p<=r so it prints number that is the row number means when r=1 it prints 1 when r=2 it prints 36 | 2 for two times and so on. Remember we also have to add space after printing the number see pattern there is a space after every number.*/ 37 | { 38 | cout< 3 | #include 4 | using namespace std; 5 | int main() 6 | { 7 | 8 | /* 9 | In nested for loop the outer loop is responsible for number of rows, the first inner loop is responsible for spaces, and the second inner loop is 10 | responsible for printing(what you desire).So, here we declare variables, for rows we use 'r' for spaces we use 'sp' for printing we use 'p', and 11 | for input a number we use 'no'. 12 | */ 13 | 14 | int r,sp,p,no,n; 15 | 16 | cout<<"Enter a number for which you wanted to print pattern\n"; 17 | cin>>no; 18 | 19 | n=no; 20 | 21 | for(r=1;r<=no;r++)//As we need rows which is equal to number that user input.so we initialize r=1 and apply condition r<=no and r++. 22 | { 23 | 24 | for(sp=1;sp<=n;sp++)/*As we need spaces that are equal to number input by user.Let suppose user enters 5 so it prints 5 spaces first and went 25 | out of loop and execute n-- it subtract 1 from the number and we get 4 spaces next time and so on. */ 26 | { 27 | 28 | cout<<" "; 29 | 30 | } 31 | n--; 32 | 33 | 34 | for(p=1;p<=r;p++)/*in this we initialized p=1 and p<=r so it prints number that is the row number means when r=1 it prints 1 when r=2 it prints 35 | 2 for two times and so on. Remember we also have to add space after printing the number see pattern there is a space after every number.*/ 36 | { 37 | cout< 11 | #include 12 | using namespace std; 13 | int main() 14 | { 15 | int i,j; 16 | 17 | for(i=1;i<=5;i++)//It is used to specify the number of rows.So we apply condition here <=5 so it obtains five rows. 18 | { 19 | 20 | for(j=1;j<=5;j++)//This loop is used for specify the spaces and for printing message on screen. 21 | 22 | { 23 | 24 | cout<<"*"<<" "; 25 | 26 | } 27 | 28 | cout< 15 | #include 16 | using namespace std; 17 | int main() 18 | { 19 | int i,j,k; 20 | 21 | for(i=1;i<=7;i++) 22 | { 23 | j=i; 24 | /*In this step j=i means j=1 so it goes to while and checks the condition that is true and it prints '*' it comes again at while and checks the 25 | condition that is true and print '*' and do the same five more times because the condition is true.When it becomes false it comes outside the loop 26 | and goes up and check the condition of for in this step now i=2 as condition is true so j=2,it comes inside and checks the while condition that is 27 | also true so it prints the '*' goes outside and again come in while and checks the condition that is true because j<=7 now so it prints '*' five more 28 | times and repeat the all procedure till the given condition is true. */ 29 | while(j<=7) 30 | { 31 | cout<<"*"<<" "; 32 | j++; 33 | } 34 | cout<<"\n"; 35 | } 36 | getch(); 37 | } 38 | -------------------------------------------------------------------------------- /C++/6. Loops/nested loops/triangle 1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | program that inputs the height of triangle and displays a triangle of characters.For example,if user enters 5,it displays the following: 3 | A 4 | B C 5 | D E F 6 | G H I J 7 | K L M N O 8 | */ 9 | 10 | #include 11 | #include 12 | using namespace std; 13 | int main() 14 | { 15 | int r,p,n; 16 | char ch='A'; 17 | 18 | cout<<"Enter the height of triangle\n"; 19 | cin>>n; 20 | 21 | for(r=1;r<=n;r++)//this loop is responsible for number of rows so rows are <= number entered by user. 22 | { 23 | for(p=1;p<=r;p++)//This is responsible for spacing/printing . 24 | { 25 | cout< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | 9 | int n=1; 10 | //we declare the int type varible n=1. 11 | 12 | while(n<=100) 13 | 14 | { 15 | /* 16 | here it first checks the condition n<=100 as n=1 so condition is true so it goes inside the loop body and first display 'n' that is 1 then it adds 17 | 1 to n so now n=2 then it goes up and checks the condition that is true and display n that is 2 and then add 1 to n and repeats the process till 18 | n<=100 when this condition is false the loop is terminated. 19 | */ 20 | cout<>num; 16 | n=num; 17 | while(n!=0) 18 | { 19 | r=n%10; 20 | sum=sum+(r*r*r); 21 | n=n/10; 22 | } 23 | if(sum==num) 24 | cout<<"Number is an Armstrong Number\n"; 25 | else 26 | cout<<"Number is not an Armstrong Number\n"; 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /C++/6. Loops/while/Print series 2 4 6 10....cpp: -------------------------------------------------------------------------------- 1 | //write a program to print the following series: 2 | // 2 4 6 10 16 26 42 68 110 178 288 3 | 4 | #include 5 | using namespace std; 6 | 7 | int main(){ 8 | int n1=2; 9 | 10 | int n2=4; 11 | 12 | int sum=0; 13 | 14 | cout< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | 9 | int n,c,f; 10 | c=1; 11 | f=1; 12 | 13 | cout<<"Enter a number\n"; 14 | cin>>n; 15 | /* 16 | let user enters 3 which is stored in n. Now it comes in the loop and checks the condition c<=n as c=1 and n=3 so 1<3 the condition is true it goes 17 | inside the loop body. Now f=f*c implies that f=1*1 because we declare f=1 and c=1 so here f=1 then it goes to next step that is 18 | c=c+1 implies that c=1+1 hence c=2 here. Now it goes up and checks the condition as 2<3 so the condition is true it goes inside the loop body.And 19 | execute the step f=f*c which implies that f=1*2 because f and c were 1 and 2 respectively which gives f=2. now it executes next step that is c=c+1 20 | so c=2+1 implies that c=3.Now it goes up again and checks the condition c<=n as 3=3 so condition is true it comes inside and execute the step f=f*c 21 | that is f=2*3 because the value of f and c was 2 and 3 respectively which gives f=6. nOw it execute the next step that is c=c+1 implies that c=3+1 22 | so c=4. now it comes up and checks the condition that is c<=n that is false so loop is terminated and gives the value of f that is 6. 23 | */ 24 | while(c<=n) 25 | { 26 | 27 | f=f*c; 28 | 29 | c=c+1; 30 | 31 | } 32 | 33 | cout<<"factorial of number\n"< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int num,i=1,negativeflag=0; 9 | 10 | while(i<=20 && negativeflag==0)//Here we set the condition that i<=20 and negativeflag==0.The loop runs until both the conditions are true. 11 | { 12 | 13 | cout<<"Enter an integer\n"; 14 | cin>>num; 15 | 16 | if(num<1)//If user enters a negative value then it assign 1 to negativeflag.So when number<0 the loop is terminated.Because the condition in while becomes false. 17 | { 18 | negativeflag=1; 19 | } 20 | i++;//if number is not less than 0 then it increment in the value of i. 21 | } 22 | if(num<1)//If number is negative then it shows the following message. 23 | { 24 | cout<<"You entered a negative number loop is terminated!\n"; 25 | } 26 | else//else when number is not negative it prints the following message. 27 | { 28 | cout<<"You Entered 20 integers\n"; 29 | } 30 | //In this program actually negativeflag is the indicator that the value must be greater than zero. 31 | 32 | getch(); 33 | } 34 | -------------------------------------------------------------------------------- /C++/6. Loops/while/show entered num while.cpp: -------------------------------------------------------------------------------- 1 | //Program that uses a while loop to enter number from the user and then display it.The loop is terminated when the user enters -1. 2 | 3 | 4 | #include 5 | #include 6 | using namespace std; 7 | int main() 8 | { 9 | int n; 10 | n=1; 11 | while(n!=-1)//If this condition is true it goes inside the loop input a number and print it and do so until users enters -1. 12 | { 13 | 14 | cout<<"Enter a number\n"; 15 | cin>>n; 16 | 17 | cout<<"You Entered\n"< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | 9 | int n,s,e; 10 | 11 | cout<<"Enter starting number\n"; 12 | cin>>s; 13 | 14 | cout<<"Enter Ending number\n"; 15 | cin>>e; 16 | 17 | n=s; 18 | 19 | 20 | while(n<=e) 21 | 22 | { 23 | if(n%2==0)//If this condition is true means that number is even so it prints the number.If this condition is false then it execute n=n+1;a 24 | { 25 | cout<<"Even number="< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | 9 | int n=1; 10 | 11 | while(n<=5) 12 | 13 | { 14 | /* 15 | Here first it checks the condition n<=5 so condition is true as n=1 it comes inside the loop body and first prints n which is 1 and then apply 16 | the formula of square that is n*n and display the square and then it adds 1 to n that is n=n+1 implies that n=1+1 so n=2 so it goes up and checks the 17 | condition and execute the loop till n<=5 when condition is false the loop is terminated.a 18 | */ 19 | cout< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int n=1; 9 | int sum=0; 10 | //here we declare int type variable n=1 and sum=0. 11 | 12 | while(n<=10) 13 | 14 | { 15 | /* 16 | here it first checks the condition n<=10 as n=1 so condition is true it goes inside the loop body and first prints n that is n=1 then it calculates 17 | sum=sum+n which is equal to sum=0+1 because we declared sum=0 and value of n here is 1 so sum=1 here and then it add 1 to n so after adding 1 18 | the value of n=2 now . Then it goes up and again checks the condition n<=10 which is true so it comes inside and first prints n which is n=2 and 19 | then calculates sum=sum+1 which is equal to sum=1+2 implies that sum=3 now.It repeats the same procedure till condition is true when condition becomes 20 | false the loop is terminated and it prints the sum of ten numbers. 21 | */ 22 | cout<>n; 18 | 19 | sum+=n;//It means that sum=sum+n; 20 | } 21 | 22 | 23 | cout<<"Total Sum="< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int n, a=1; 9 | 10 | cout<<"Enter a number="; 11 | cin>>n; 12 | 13 | while(a<=10) 14 | 15 | { 16 | 17 | cout< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int age[200],no_of_persons,i; 9 | int count=0; 10 | 11 | cout<<"Enter the number of persons required\n"; 12 | cin>>no_of_persons; 13 | 14 | for(i=0;i>age[i]; 18 | 19 | if(age[i]>50 && age[i]<60) 20 | { 21 | count+=1; 22 | } 23 | } 24 | 25 | cout<<"Persons between 50 and 60 are\n"< 6 | using namespace std; 7 | int main() 8 | { 9 | int arr[10]={10,20,30,40,50,60,70,80,90,100}; 10 | 11 | int start,mid,end; 12 | int loc=-1; 13 | int num; 14 | 15 | start=0; 16 | end=9; 17 | 18 | while(start<=end) 19 | { 20 | mid=(start+end)/2; 21 | 22 | if(arr[mid]==num) 23 | { 24 | loc=mid; 25 | break; 26 | } 27 | else if(num 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() 10 | 11 | { 12 | int day,month,total; 13 | int days_Per_Month[12]={31,28,31,30,31,30,31,31,30,31,30,31};//Number days in a month. 14 | 15 | cout<<"Enter the Day Number\n"; 16 | cin>>day; 17 | 18 | cout<<"Enter the month number\n"; 19 | cin>>month; 20 | 21 | total=day; 22 | 23 | for(int i=0;i 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int arr[5],i,sum=0; 9 | float avg=0; 10 | 11 | for(i=0;i<5;i++)//Array index start from 0 therefore we initialize i=0; and the next condition is i<5 because it has to store 5 value. 12 | 13 | { 14 | cout<<"Enter an integer\n"; 15 | cin>>arr[i]; 16 | 17 | sum=sum+arr[i]; 18 | } 19 | 20 | avg=sum/5; //formula for finding average. 21 | 22 | cout<<"Sum="< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int arr[5],i; 9 | 10 | for(i=0;i<5;i++) 11 | { 12 | cout<<"Enter Number\n"; 13 | cin>>arr[i]; 14 | } 15 | 16 | int maximum; 17 | maximum=arr[0];//This is because array starts from 0. 18 | 19 | for(i=0;i<5;i++) 20 | { 21 | if(maximum>arr[i]; 14 | } 15 | 16 | int minimum; 17 | minimum=arr[0];//This is because array starts from 0. 18 | 19 | for(i=0;i<5;i++) 20 | { 21 | if(minimum>arr[i]) 22 | { 23 | minimum=arr[i]; 24 | } 25 | } 26 | 27 | cout<<"Minimum="< 8 | #include 9 | using namespace std; 10 | int main() 11 | { 12 | int numbers[5]; 13 | int squares[5]; 14 | int cubes[5]; 15 | int sum[5]; 16 | int total=0; 17 | 18 | //Now store numbers in the arrays. 19 | 20 | for(int i=0;i<5;i++) 21 | { 22 | 23 | numbers[i]=i; 24 | squares[i]=i*i; 25 | cubes[i]=i*i*i; 26 | sum[i]=numbers[i]+squares[i]+cubes[i]; 27 | 28 | } 29 | //Output all the arrays and then add up all the sums. 30 | 31 | cout<<"numbers:\t"; 32 | for(int i=0;i<5;i++) 33 | cout< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | 9 | int arr[5];//Here we declare an array of length 5. 10 | 11 | cout<<"Enter five integers\n"<>arr[0]; //Array always starts from 0.The value of first integer is stored in this array and so on. 13 | cin>>arr[1]; 14 | cin>>arr[2]; 15 | cin>>arr[3]; 16 | cin>>arr[4]; 17 | 18 | cout<<"\nThe Values in Array are:\n"< 17 | #include 18 | using namespace std; 19 | int main() 20 | { 21 | int arr[5],i;//Here we declare an array of integer type of length 5. 22 | 23 | for(i=0;i<5;i++) 24 | { 25 | cout<<"Enter an Integer\n"<>arr[i];//it stores the valuees in arr[i]. 27 | } 28 | 29 | cout<<"The values in array are\n"< 4 | #include 5 | using namespace std; 6 | int main() 7 | { 8 | int number[5],i; 9 | 10 | for(i=0;i<5;i++) 11 | { 12 | cout<<"Enter Number\n"; 13 | cin>>number[i]; 14 | } 15 | 16 | cout<<"Number in actual order:\n"; 17 | for(i=0;i<5;i++) 18 | { 19 | cout<=0;i--) 24 | { 25 | cout<>search[i]; //ten numbers will be stored in this array. 13 | } 14 | 15 | int key=7; 16 | /* 17 | Here we declare a variable 'key' and assigned it value which is '7'.Because we have to search 7 in this array. If '7' is found then we have to show its 18 | index. So, in the next step the variable 'key' is compared with array named 'search'.If value is found then it will show its index. 19 | */ 20 | 21 | for(int j=0;j<10;j++) 22 | { 23 | if(search[j]==key) 24 | { 25 | cout<<"Value found at index "< 7 | using namespace std; 8 | int main() 9 | { 10 | int arr[10]={1,2,3,4,5,6,7,8,9,10}; 11 | 12 | int i,n,mid,start,end; 13 | 14 | start=0; 15 | end=9; 16 | int loc=-1; 17 | 18 | cout<<"Enter any number to find\n"; 19 | cin>>n; 20 | 21 | while(start<=end) 22 | { 23 | 24 | mid=(start+end)/2; 25 | 26 | if(arr[mid]==n) 27 | { 28 | 29 | loc=mid; 30 | 31 | break; 32 | } 33 | 34 | else 35 | if(n 11 | using namespace std; 12 | int main() 13 | { 14 | int arr[5],i,j,temp; 15 | 16 | for(i=0;i<5;i++) 17 | { 18 | cout<<"Enter Value\n"; 19 | cin>>arr[i]; 20 | } 21 | cout<<"Original values in array:\n"; 22 | for(i=0;i<5;i++) 23 | { 24 | cout<arr[j+1]) 32 | { 33 | temp=arr[j]; 34 | arr[j]=arr[j+1]; 35 | arr[j+1]=temp; 36 | } 37 | } 38 | } 39 | 40 | cout<<"\nThe sorted array:\n"; 41 | for(i=0;i<5;i++) 42 | { 43 | cout< 4 | using namespace std; 5 | int main() 6 | { 7 | int arr[2][3]={1,2,3,4,5,6}; //this array can store 6 element as 2*3 =6. 8 | int i,j; 9 | 10 | for(i=0;i<2;i++) 11 | { 12 | for(j=0;j<3;j++) 13 | { 14 | cout<<"arr["< 7 | using namespace std; 8 | int main() 9 | { 10 | int i,j,max,min; 11 | 12 | int arr[2][4]={{15,50,20,45},{25,65,80,70}}; 13 | 14 | max=min=arr[0][0];//because array index starts from 0. 15 | 16 | for(i=0;i<2;i++) 17 | { 18 | for(j=0;j<4;j++) 19 | { 20 | if(arr[i][j]>max) 21 | { 22 | max=arr[i][j]; 23 | } 24 | if(arr[i][j]>n; 14 | 15 | for(i=0;i<10;i++) 16 | 17 | if(arr[i]==n) 18 | loc=i; 19 | 20 | 21 | if(loc==-1) 22 | cout<<"Value not found in array\n"; 23 | 24 | else 25 | cout<<"Value found at index\n"< 4 | using namespace std; 5 | int main() 6 | { 7 | 8 | int arr[5],i,j,min,temp; 9 | 10 | for(i=0;i<5;i++) 11 | { 12 | cout<<"Enter value\n"; 13 | cin>>arr[i]; 14 | } 15 | 16 | cout<<"Original values in array:\n"; 17 | for(i=0;i<5;i++) 18 | { 19 | cout< 15 | using namespace std; 16 | int main() 17 | { 18 | int arr[2][4],i,j; 19 | 20 | for(i=0;i<2;i++) 21 | { 22 | for(j=0;j<4;j++) 23 | { 24 | cout<<"Enter an Integer\n"; 25 | cin>>arr[i][j]; 26 | } 27 | } 28 | 29 | for(i=0;i<2;i++) 30 | { 31 | for(j=0;j<4;j++) 32 | { 33 | cout<>sort[i]; 13 | } 14 | 15 | for(i=0;i<5;i++) 16 | { 17 | for(j=0;j<4;j++) 18 | { 19 | if(sort[j]>sort[j+1]) 20 | { 21 | int temp; //for swapping. 22 | temp=sort[j]; 23 | sort[j]=sort[j+1]; 24 | sort[j+1]=temp; 25 | } 26 | } 27 | } 28 | cout<<"The sorted array is\n"; 29 | for(i=0;i<5;i++) 30 | cout<>num1>>num2; 17 | 18 | cout<<"Greatest Common Divisor is "< 4 | using namespace std; 5 | 6 | int sumOfTwo(int num1,int num2) //function definition. 7 | 8 | { 9 | int sum; 10 | 11 | sum=num1+num2; 12 | 13 | return sum; 14 | } 15 | 16 | int main() 17 | 18 | { 19 | int a,b; 20 | 21 | cout<<"Please Enter two numbers to add\n"; 22 | cin>>a>>b; 23 | 24 | 25 | cout<<"Sum of entered two numbers="< /*it is always on the top.*/ 4 | using namespace std; 5 | 6 | 7 | 8 | void showOnScreen() /* 9 | Function. 10 | --> void is the return type.void means it didn't return any value. 11 | --> showOnScreen is the function name. 12 | --> () inside this we write parameters like (int a,int b) 13 | */ 14 | { 15 | cout<<"Programming makes life interesting.\n\n"; 16 | } 17 | 18 | 19 | 20 | int main() /* This is also a function */ 21 | { 22 | 23 | 24 | showOnScreen(); //Function Call. 25 | 26 | showOnScreen(); //Function Call. 27 | 28 | showOnScreen(); //Function Call. 29 | 30 | showOnScreen(); //Function Call. 31 | /* 32 | Here we call the function that prints on screen "programming makes life interesting".And we call it four times so it prints on screen four times. 33 | */ 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /C++/8. Functions/d bw Call by value and call by reference part 1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Program to differentiate between call by value and call by reference. 3 | */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | void swapTwoNum(int num1,int num2) 9 | { 10 | int temporary; 11 | 12 | cout<<"Before Swapping\n A="<>num1>>num2; 29 | 30 | swapTwoNum(num1,num2); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /C++/8. Functions/default parameters with example.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Default Parameters: 3 | The parameters initialized during function declaration are called default parameters.The default parameters allow user to call a function without 4 | giving required parameters.The initialized values of default parameters are used if the user does not specify any value for the parameters in function 5 | call.Default values can be constants,global variables or function calls. 6 | */ 7 | //write a program that used default parameters. 8 | #include"iostream" 9 | using namespace std; 10 | void show(int n=50);//defaul parameter 11 | int main() 12 | { 13 | show();//here the value of default parameter will be used because user did not specify any value here. 14 | show(60); 15 | show(70); 16 | return 0; 17 | } 18 | void show(int n) 19 | { 20 | cout<<"n="<>arr[i]; 17 | 18 | cout<<"The array contains "<>num[i]; 18 | } 19 | 20 | cout<<"The arrays contains "< 6 | using namespace std; 7 | 8 | void factorial(int num); 9 | 10 | int main() 11 | { 12 | int num; 13 | 14 | cout<<"Enter a number\n"; 15 | cin>>num; 16 | 17 | factorial(num); 18 | 19 | return 0; 20 | } 21 | void factorial(int num) 22 | { 23 | int counter; 24 | int factorial=1; 25 | 26 | for(counter=1;counter<=num;counter++) 27 | { 28 | factorial=factorial*counter; 29 | } 30 | 31 | cout<<"Factorial of a number is\n"<>a>>b; 11 | 12 | c=sum(a,b); 13 | cout<<"Sum="<>a>>b; 12 | 13 | sum(a,b); 14 | 15 | return 0; 16 | } 17 | 18 | void sum(int a,int b) //Inside PARENTHESIS are the function parameters.Here a,b are the function parameters. 19 | { 20 | cout<<"Sum="<>a>>b; 18 | sum=a+b; 19 | cout<<"Sum="<>num[i]; 23 | } 24 | test(num); 25 | 26 | return 0; 27 | } 28 | void test(int arr[]) 29 | { 30 | cout<<"The values in array:"<>num[i]; 30 | } 31 | 32 | show(num); 33 | 34 | } 35 | 36 | void show(int arr[]) 37 | { 38 | int j; 39 | cout<<"The values in array:\n"; 40 | for(j=0;j<5;j++) 41 | { 42 | cout<>d.marks; 29 | cout<<"Enter grade:"; 30 | cin>>d.grade; 31 | test(d); 32 | 33 | return 0; 34 | } 35 | void test(data d) 36 | { 37 | cout<<"You entered the following data:"<>marks; 23 | 24 | cout<<"Your grade is "<=80) 33 | return 'A'; 34 | else if(marks>=60 && marks<=79) 35 | return 'B'; 36 | else if(marks>=40 && marks<=59) 37 | return 'C'; 38 | else 39 | return 'F'; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /C++/8. Functions/inline functions with example.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Inline Functions: 3 | The function declared with keyword inline is known as inline function.The inline function is declared before main function.Only very small functions 4 | are declared as inline functions.The code of function body is inserted at each function call if the function is declared as inline function.It allows 5 | the program to execute the function statements without shifting control from calling function.It saves time and increases program's efficiency. 6 | The code of function body is repeated at each function call.It takes more memory than normal function call.That is why the inline functions must only 7 | be used if the function body contains a small number of statements. 8 | */ 9 | 10 | //write a program that uses inline function. 11 | 12 | #include"iostream" 13 | using namespace std; 14 | 15 | inline int square(int n) 16 | { 17 | return n*n; 18 | } 19 | 20 | int main() 21 | { 22 | cout< 6 | using namespace std; 7 | 8 | void maxNum(int num1,int num2);/* We declare function here because it is compulsory because we define function after main() function. */ 9 | 10 | int main() 11 | { 12 | int num1,num2; 13 | 14 | cout<<"Enter two numbers\n"; 15 | cin>>num1>>num2; 16 | 17 | maxNum(num1,num2); 18 | 19 | return 0; 20 | } 21 | 22 | void maxNum(int num1,int num2) 23 | { 24 | if(num1>num2) 25 | cout<<"Maximum="<>num1>>num2; 21 | if(num1==0 || num2==0) 22 | cout<<"Invalid Input!!"; 23 | else{ 24 | 25 | ans=mul(num1,num2); 26 | 27 | if(ans==1) 28 | cout<>n[i][j]; 29 | } 30 | } 31 | maximum=max(n); 32 | cout<<"Maximum value is:"<m) 45 | m=arr[i][j]; 46 | } 47 | } 48 | 49 | return m; 50 | } -------------------------------------------------------------------------------- /C++/8. Functions/passing individual array element to function with example.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Passing Individual Array Element to Function: 3 | An individual element of array can also be passed to function.The data type of array and the data type of function parameter must be same.The 4 | individual element is passed to a function like a simple variable.Suppose a function is defined to accept an integer parameter.It can accept 5 | an indiviadual element of an integer array in same way as it can accept a simple integer variable. 6 | */ 7 | /* 8 | write a program that inputs five integers in an array.It passes all elements of the array to a function one by one.The function displays the actual 9 | value of the element and its square. 10 | */ 11 | 12 | #include"iostream" 13 | using namespace std; 14 | 15 | void square(int n); 16 | 17 | int main() 18 | { 19 | int arr[5]; 20 | cout<<"Enter five integers:"<>arr[i]; 23 | 24 | cout<<"The square of the numbers:"<>num[i]; 26 | } 27 | 28 | cout<<"Calling the Function..."<<"\n"; 29 | for(i=0;i<5;i++) 30 | { 31 | square(num[i]); 32 | } 33 | 34 | return 0; 35 | } 36 | 37 | void square(int n) 38 | { 39 | cout<>min>>max; 15 | 16 | 17 | cout<<"Prime numbers in the range are..."<<"\n"; 18 | for( int i=min;i<=max;i++) 19 | { 20 | prime_checker(i); 21 | } 22 | 23 | } 24 | 25 | void prime_checker(int min) 26 | { 27 | int flag=0; 28 | for(int i=2; i<=min ; i++) 29 | { 30 | if(min%i==0) 31 | flag=flag+1; 32 | } 33 | if(flag==1) 34 | cout<>num; 24 | cout<<"Factorial of a Number is:"<>a.id>>a.pages>>a.price; 30 | cout<<"Enter book ID,Pages and Price:"<>b.id>>b.pages>>b.price; 32 | r=check(a,b); 33 | cout<<"The more costly Book is:"<y.price) 42 | return x; 43 | else 44 | return y; 45 | } 46 | -------------------------------------------------------------------------------- /C++/8. Functions/static variable functions.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Static Variable: 3 | A local variable declared with keyword static is called static variable. 4 | The keyword static is used to increase the lifetime of local variable. 5 | The scope of static variable is similiar to the scope of local variable.Static variable can be used only in the function in which it is 6 | decalred. 7 | */ 8 | 9 | /* 10 | Write a program that calls a function for five times using loop.The function uses a static variable initialized to 0.Each time the function is called, 11 | the value of static variable is incremented by 1 and is displayed on the screen. 12 | */ 13 | 14 | #include"iostream" 15 | using namespace std; 16 | 17 | void fun(); 18 | 19 | int main() 20 | { 21 | int i; 22 | for(i=1;i<=5;i++) 23 | { 24 | fun(); 25 | } 26 | 27 | return 0; 28 | } 29 | 30 | void fun() 31 | { 32 | static int n=0; 33 | n++; 34 | cout<<"Value of n="<>num1; 17 | cout<<"Please enter a number\n"; 18 | cin>>num2; 19 | 20 | ans=square(num1)+cube(num2); 21 | 22 | cout<0) 17 | { 18 | sum=sum+arr[i]; 19 | } 20 | } 21 | else 22 | { 23 | if(arr[i]<0) 24 | { 25 | sum=sum+arr[i]; 26 | } 27 | } 28 | } 29 | 30 | return sum; 31 | } 32 | 33 | int main() 34 | { 35 | int size; 36 | cout<<"Enter size of array:"; 37 | cin>>size; 38 | int arr[size]; 39 | for(int i=0;i>arr[i]; 43 | } 44 | int sum_p; 45 | sum_p=check(arr,size); 46 | int sum_n; 47 | sum_n=check(arr,size,'-'); 48 | cout< 6 | using namespace std; 7 | 8 | int table(int num); 9 | 10 | int main() 11 | { 12 | int num; 13 | 14 | cout<<"Enter a Number\n"; 15 | cin>>num; 16 | 17 | table(num); 18 | 19 | return 0; 20 | } 21 | 22 | int table(int num) 23 | { 24 | int counter; 25 | int table; 26 | 27 | for(counter=1;counter<=10;counter++) 28 | { 29 | cout< 4 | using namespace std; 5 | 6 | void makeCoffee(); /* Prototype of function.We have to write it earlier here when functions declaration and definition is after int main().Otherwise compiler shows error.*/ 7 | 8 | 9 | 10 | 11 | int main() 12 | { 13 | makeCoffee(); 14 | 15 | return 0; 16 | } 17 | 18 | 19 | 20 | 21 | 22 | void makeCoffee() /*Function declaration*/ 23 | { 24 | 25 | 26 | /* A set of statements that explains what a function does is called function definition.it can be written before main() , after main() 27 | and in a separate file.Function declaration is compulsary if function definition is written after main() function.*/ 28 | 29 | 30 | 31 | 32 | cout<<"Take water.\n"; 33 | cout<<"Boil water.\n"; 34 | cout<<"Add coffee in water after boiling.\n"; 35 | cout<<"Shake Well.\n"; 36 | cout<<"Enjoy Coffee!! \n"; 37 | 38 | 39 | 40 | } -------------------------------------------------------------------------------- /C++/8. Functions/using all arithmetic operators functions..cpp: -------------------------------------------------------------------------------- 1 | //Program that uses all arithmetic operators on two numbers using functions. 2 | 3 | #include 4 | using namespace std; 5 | 6 | int add(int num1,int num2) 7 | { 8 | 9 | int addition; 10 | 11 | addition=num1+num2; 12 | 13 | return addition; 14 | 15 | } 16 | 17 | int subtract(int num1,int num2) 18 | { 19 | int subtract; 20 | 21 | subtract=num1-num2; 22 | 23 | return subtract; 24 | } 25 | 26 | int multiply(int num1,int num2) 27 | { 28 | 29 | int multiply; 30 | 31 | multiply=num1*num2; 32 | 33 | return multiply; 34 | } 35 | 36 | int division(int num1,int num2) 37 | { 38 | 39 | int division; 40 | 41 | division=num1/num2; 42 | 43 | return division; 44 | } 45 | 46 | int remainder(int num1,int num2) 47 | { 48 | 49 | int remainder; 50 | 51 | remainder=num1%num2; 52 | 53 | return remainder; 54 | } 55 | 56 | int main() 57 | { 58 | int a,b; 59 | 60 | cout<<"Please Enter two numbers\n"; 61 | cin>>a>>b; 62 | 63 | if(b!=0) 64 | { 65 | cout<<"Division of Numbers="<>s.marks; 29 | cout<<"Enter Grade:"; 30 | cin>>s.grade; 31 | a=s; 32 | cout<<"The first record is as follows:"<>p.fname; 29 | cout<<"Enter second Name:"; 30 | cin>>p.sname; 31 | cout<<"Enter city:"; 32 | cin>>p.city; 33 | cout<<"Enter Phone Number without dashes:"; 34 | cin>>p.telephone; 35 | cout<<"Enter date of Birth in format (DD-MM-YY):\n"; 36 | cin>>p.birthday.day>>p.birthday.month>>p.birthday.year; 37 | cout<>rec.rollNo; 32 | cout<<"Enter Marks:"; 33 | cin>>rec.r.marks; 34 | cout<<"Enter Grade:"; 35 | cin>>rec.r.grade; 36 | cout<>e.salary; 25 | cout<<"Salary is:"<>e.grade; 28 | cout<<"Grade is:"<>e.salary; 36 | cout<<"Enter Grade:"; 37 | cin>>e.grade; 38 | cout<<"Salary is:"<>a.rollNo; 27 | cout<<"Enter marks of five Subjects:\n"; 28 | for(int i=0;i<5;i++) 29 | { 30 | cin>>a.marks[i]; 31 | sum=sum+a.marks[i]; 32 | } 33 | avg=sum/5; 34 | 35 | cout<<"Roll Number:"<>r[i].id; 26 | cout<<"Pages:"; 27 | cin>>r[i].pages; 28 | cout<<"Price:"; 29 | cin>>r[i].price; 30 | } 31 | max=r[0].price; 32 | m=0; 33 | for(int i=0;i<5;i++) 34 | { 35 | if(r[i].price>max) 36 | { 37 | max=r[i].price; 38 | m=i; 39 | } 40 | } 41 | 42 | cout<<"\nThe record of most costly book:\n"; 43 | cout<<"Book ID:"<>b1.bookId>>b1.pages>>b1.price; 21 | cout<<"Enter BookID,Pages and Price of Book 2:\n"; 22 | cin>>b2.bookId>>b2.pages>>b2.price; 23 | 24 | cout<<"The most costly book is:"; 25 | if(b1.price>b2.price) 26 | { 27 | cout<<"Book ID:"<>d.rollNo; 21 | cout<<"Enter Marks:"; 22 | cin>>d.marks; 23 | cout<<"Enter Grade:"; 24 | cin>>d.grade; 25 | cout<<"You entered the following data:\n"; 26 | cout<<"Roll Number:"<>d.day; 21 | cout<<"Enter Month:"; 22 | cin>>d.month; 23 | cout<<"Enter Year:"; 24 | cin>>d.year; 25 | cout<<"Your date of birth is:"< 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | int v[5] = {1,2,3,4,5}; 7 | int w[5] = {5,4,3,2,1}; 8 | int * ptr = v; 9 | ptr++; //compile and valid 10 | cout << *(v+3) << endl; //valid 11 | //v++; //no compile 12 | //v = w; //no compile 13 | ptr = w; //valid, now points to w 14 | *(v+3) = *(w+3); //valid, assigns a value of w to a position of v 15 | cout << (v < ptr) << endl; //valid, check which memory position comes first 16 | cout << v << " " << ptr << " " << w << endl; //prints the memory location of v and ptr which is w 17 | } -------------------------------------------------------------------------------- /C++/Pointers/Introduction.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Pointers are variables that store memory addresses as their values. In C++, a pointer is a variable that holds the memory address of another variable. 3 | Pointers are used to store the addresses of other variables, or to store the addresses of memory locations where data is stored. 4 | 5 | One of the main advantages of pointers is that they allow you to directly manipulate the memory used by your program, 6 | which can be very useful in certain circumstances. For example, you can use pointers to dynamically allocate memory for data structures, 7 | or to pass large blocks of data to functions more efficiently. 8 | 9 | To declare a pointer in C++, you use the "*" operator. For example, the following code declares a pointer to an integer: 10 | Keep in mind you have to initialize pointer to the same data type that you need it to point to. For example in next line we create a pointer that points to a integer data type. 11 | 12 | int *ptr; 13 | 14 | To access the value stored at the memory address pointed to by a pointer, you use the "*" operator also known as the dereference operator. 15 | For example, the following code sets the value of the integer pointed to by "ptr" to 10: 16 | 17 | int val = 10; 18 | ptr = &val; 19 | 20 | Now you can access the values in variable 'val' by dereferencing the pointer ptr; 21 | 22 | *ptr = 10; 23 | 24 | 25 | 26 | There are a few rules that you need to follow when working with pointers in C++. 27 | For example, you cannot assign a value to a pointer directly; you must use the "&" operator to get the address of a variable first. 28 | You also need to make sure that you initialize a pointer before you try to use it, 29 | and that you free any memory that you dynamically allocate using pointers when you are finished with it. 30 | 31 | Overall, pointers are a powerful and useful feature of C++ that allow you to directly manipulate the memory used by your program. With careful use, 32 | they can greatly enhance the efficiency and flexibility of your code. 33 | 34 | */ 35 | -------------------------------------------------------------------------------- /C++/Pointers/VectorStorage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | //Example 1 7 | int v[5] = {1,2,3,4,5}; 8 | int * ptrv = &v[0]; 9 | for(int i = 0; i < 5; i++){ 10 | cout << *(ptrv+i) << " "; 11 | } 12 | cout << endl; 13 | 14 | //Example 2 15 | for(int * ptrit = v; ptrit < v+5; ptrit++){ 16 | cout << *ptrit << " "; 17 | } 18 | cout << endl; 19 | 20 | //Difference between a memory position and the following 21 | cout << ptrv << " " << ptrv+1 << endl; 22 | 23 | //Example 3 24 | ptrv = &v[1]; 25 | int * ptrv2 = &v[3]; 26 | if(ptrv < ptrv2){ 27 | cout << "ptrv points to an index before ptrv2" << endl; 28 | }else{ 29 | cout << "ptrv points to an index after ptrv2" << endl; 30 | } 31 | 32 | //Example 4 33 | int * ptrx; 34 | int * ptry; 35 | ptrx = &v[1]; 36 | ptry = &v[4]; 37 | cout << v[(ptry - ptrx)] << endl; 38 | } -------------------------------------------------------------------------------- /C++/Pointers/operations.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int main(){ 5 | //Assignment of pointer to variable 6 | int * puntero; 7 | int y = 5; 8 | puntero = &y; 9 | cout << "valor of puntero:" << *puntero << endl ; 10 | 11 | //What happens if we do not assign anything to the pointer? 12 | int * ptr; 13 | cout << *ptr << endl; 14 | //Nothing 15 | 16 | /* 17 | float * ptr2=0; //Why would it give error? 18 | cout << *ptr2; 19 | */ 20 | 21 | //Handling a value from a pointer 22 | float *ptrx, x=5.4; 23 | cout << "The old value of x is:" << endl; 24 | cout << x << endl; 25 | ptrx = &x; 26 | *ptrx = 8.2; 27 | cout << "The new value of x is:" << endl; 28 | cout << *ptrx << " " << x << endl; 29 | cout << "The old value of x is (put in input):" << endl; 30 | cin >> *ptrx; 31 | cout << *ptrx << " " << x << endl; 32 | } 33 | -------------------------------------------------------------------------------- /C++/Pointers/sizeof.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(){ 6 | int a=5; bool b=true; float c = 34.5; 7 | double d=43.23; char e = 'a'; 8 | cout << sizeof(a) << endl; 9 | cout << sizeof(b) << endl; 10 | cout << sizeof(c) << endl; 11 | cout << sizeof(d) << endl; 12 | cout << sizeof(e) << endl; 13 | 14 | int *entero = &a; 15 | bool* boolean = &b; 16 | float * flotante = &c; 17 | double * doble = &d; 18 | char * caracter = &e; 19 | 20 | cout << sizeof(entero) << endl; 21 | cout << sizeof(boolean) << endl; 22 | cout << sizeof(flotante) << endl; 23 | cout << sizeof(doble) << endl; 24 | cout << sizeof(caracter) << endl; 25 | 26 | } 27 | -------------------------------------------------------------------------------- /JAVA/1. Basics/1-intro.md: -------------------------------------------------------------------------------- 1 | Java is a programming language and computing platform first released by 2 | Sun Microsystems in 1995.It is used to build a wide range of 3 | applications, including web, mobile, desktop, and backend systems. It is 4 | known for its simplicity, reliability, and security. Java is an 5 | object-oriented language, which means that it is based on the concept of 6 | "objects" that represent real-world entities and their attributes and 7 | behaviors.Java has a large and active developer community, which means 8 | that there are many resources and tools available for learning and using 9 | the language. It is also used in a variety of industries, making it a 10 | useful skill to have in your toolkit as a programmer.To get started with 11 | Java, you will need to install the Java Development Kit (JDK) on your 12 | computer and set up a development environment. There are many IDEs 13 | (Integrated Development Environments) available that make it easy to 14 | write, test, and debug Java code. Some popular IDEs for Java include 15 | Eclipse, IntelliJ, and NetBeans. 16 | -------------------------------------------------------------------------------- /JAVA/1. Basics/3-Hello-World.md: -------------------------------------------------------------------------------- 1 | ## Here is a simple "Hello, World!" program in Java: 2 | ```java 3 | public class Main { 4 | public static void main(String[] args) { 5 | System.out.println("Hello, World!"); 6 | } 7 | } 8 | ``` 9 | **Explanation:** 10 | • The first line `public class Main` defines a class named `Main`. In Java, all code must be contained within a class. 11 | • The next line `public static void main(String[] args)` is the main method of the program. This is the entry point of the program, and is where the code execution begins. 12 | • The next line `System.out.println("Hello, World!");` is a statement that prints the string `"Hello, World!"` to the console. `System.out.println()` is a method that prints a line of text to the standard output (in this case, the console). 13 | ## Run: 14 | To run this program, you will need to have the Java Development Kit (JDK) installed on your computer. You can then compile the program by running the `javac` command followed by the name of the Java file: 15 | ```java 16 | javac Main.java 17 | ``` 18 | This will create a new file named `Main.class`, which contains the compiled version of the program. You can then run the program using the `java` command: 19 | ```java 20 | java Main 21 | ``` 22 | This will execute the code in the `main` method, and you should see the output "Hello, World!" printed to the console. 23 | -------------------------------------------------------------------------------- /LEARN.md: -------------------------------------------------------------------------------- 1 | # Learn Programming Fundamentals 2 | Irrespective of the programming language you choose to learn, the basic concepts of programming are similar across languages. 3 | Some of these concepts include: 4 | - Variable Declaration 5 | - Basic Syntax 6 | - Conditionals 7 | - Loops 8 | - Functions 9 | - etc 10 | 11 | --- 12 | 13 | This is the repository where you can learn the `programming-fundamentals` practically with code examples, specifically in C++. 14 | Start learning by following the links below: 15 | 16 | --- 17 | 18 | ## C++ 19 | 20 | You can use [github.dev](github.dev) for coding. Or you can setup your IDE on your local machine. 21 | You can setup `DevC++` or `CodeBlocks` or `VScode` for running c++ programs. 22 | 23 | 24 | - #### [Basics to if-else](https://github.com/ShehrozIrfan/learn-programming-fundamentals/tree/master/C%2B%2B/2.%20Basics%20to%20if-else) 25 | 26 | 27 | - #### [switch](https://github.com/ShehrozIrfan/learn-programming-fundamentals/tree/master/C%2B%2B/3.%20switch) 28 | 29 | - #### [continue](https://github.com/ShehrozIrfan/learn-programming-fundamentals/tree/master/C%2B%2B/4.%20continue) 30 | 31 | - #### [goto](https://github.com/ShehrozIrfan/learn-programming-fundamentals/tree/master/C%2B%2B/5.%20goto) 32 | 33 | - #### [Loops](https://github.com/ShehrozIrfan/learn-programming-fundamentals/tree/master/C%2B%2B/6.%20Loops) 34 | 35 | - #### [Array](https://github.com/ShehrozIrfan/learn-programming-fundamentals/tree/master/C%2B%2B/7.%20Array) 36 | 37 | - #### [Functions](https://github.com/ShehrozIrfan/learn-programming-fundamentals/tree/master/C%2B%2B/8.%20Functions) 38 | 39 | - #### [Structure and Unions](https://github.com/ShehrozIrfan/learn-programming-fundamentals/tree/master/C%2B%2B/9.%20Structures%20and%20unions) 40 | 41 | - #### [Interview Questions](https://github.com/ShehrozIrfan/learn-programming-fundamentals/tree/master/C%2B%2B/10.%20Interview's%20Questions) 42 | 43 | --- 44 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Shehroz Irfan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Learn Programming Fundamentals 2 | 3 | This is a repository in which you can learn the programming fundamentals practically, specifically in C++. 4 | The repository is divided into multiple folders like: 5 | - Adding colors to C++ Console 6 | - Basics to `if-else` 7 | - `switch` 8 | - `continue` 9 | - `goto` 10 | - Loops 11 | - Arrays 12 | - Functions 13 | - Structures and Unions 14 | - Interview Questions 15 | 16 | You can simply go to any folder, find a program related to that folder topic and learn from that, there are comments added in each program for better understanding. 17 | 18 | ## How to Learn 19 | 20 | Please visit [LEARN.md](https://github.com/ShehrozIrfan/learn-programming-fundamentals/blob/master/LEARN.md) for learning 21 | 22 | ## How to Contribute 23 | 24 | Please visit [CONTRIBUTE.md](https://github.com/ShehrozIrfan/learn-programming-fundamentals/blob/master/CONTRIBUTING.md) for contributions 25 | 26 | ## Hacktoberfest 2023 27 | Hacktoberfest2022 Banner 28 | 29 |

30 | This year marks the 10th anniversary of Hacktoberfest, and we’re calling on your support! Whether it’s your first time participating—or your tenth—it’s almost time to hack out four pristine pull/merge requests as we continue our month of support for open source. 31 |

32 | 33 | #### What is Hacktoberfest? 34 |

35 | Hacktoberfest is a month-long celebration of open source software run by DigitalOcean in partnership with GitHub and Twilio. Hacktoberfest is open to everyone in our global community! Four quality pull requests must be submitted to public GitHub repositories. You can [sign up](https://hacktoberfest.com/) anytime between October 1 and October 31. 36 |

37 | --------------------------------------------------------------------------------