Friday, 29 September 2017

Types Of For Loop

In the previous post, we understand how for loop works and three parts of for loop.In this post, I will discuss the types of for loop or in which way you will use for loop.
For loops are mainly two types:

  • Infinite for loop
  • Nested for loop
THE INFINITE FOR LOOP:-
A loop becomes infinite loop if the condition never becomes false. For loop required three parts to run initialization, test expression, and update. if you leave the conditions empty that will make the endless loop.

Syntax:-
#include <iostream>
using namespace std;
 
int main () {
   for( ; ; ) {
      cout<<"This loop will run forever.\n";
   }

   return 0;
}
when the conditional expression is absent, it is assumed to be true. you may use initialization and update expression but most of the
programmers prefer "for(; ;)" to construct an infinite for loop.

NOTE:- you will use CTRL+C to terminate the infinite for loop.
use printf("This loop will run forever.\n"); instead of cout<<"This loop will run forever.\n"; do it yourself and see what happened.

NESTED FOR LOOP:-
The placing of one loop inside the body of another loop is called nesting.In nested for loop, a for loop is present inside the another for loop. 

Syntax:-
for ( initialization; test condition; increment ) {

   for ( initialization; tset condition; increment ) //using another variable{
      statement(s);
   }
 
   statement(s);
}

program to print number in the right triangular pattern:-

#include <iostream>
using namespace std;
int main()
{
    int i,j;
    for(i=1;i<=5;i++)
    {
        for(j=1;j<=i;j++)
            cout<<j;
            cout<<"\n"; 
    }
    return 0;
}
Output:-
1
12
123
1234
12345

now take a look how nested loop works? the outer for loop start working,i=1 then it will compare to test condition 1<=5 if the condition is true then it will go to inner for loop and give an increment. In inner for loop j=1, and test condition is 1, i is 1 and j<=i now it will run once and exist from inner for loop and go to outer for loop.Next time i become 2 outer for loop will run once but inner loop will run twice.The process will repeat again and again whenever test condition does not become false.
    
    happy coding....keep learning.

Sunday, 24 September 2017

Iteration Statement- For Loop

Iteration is a process where a set of instruction or statement is executed repeatedly for a specified number of time or until a condition is met.Iteration statements are commonly known as loops.There are three types of looping statements in C++:

A looping is consist of three parts:- initialization, test expression, increment/decrement or update value. A loop will run whenever the test condition is not met if the test condition is met then the loop will break and show the result on the screen.

For loop is most commonly used looping technique because in for loop all three parts of the loop: initialization, test expression, increment/decrement or update value in the same line.

Syntax:
for(intialization;test expression;increment/decrement)

{
 
 //body of loop
 
}
In the above syntax, you will see three parts of the loop in the same line and the three parts are separated by the semicolon, not by the comma's.

Program to print 1 to 10 number by using for loop:
#include<iostream>
using namespace std;
int main(){
 int i;
 cout<<"numbers from 1 to 10"<<endl;
 for(i=1;i<=10;i++){
  cout<<i<<endl;
  
 }
 return 0;
}
Output:
numbers from 1 to 10
1
2
3
4
5
6
7 
8
9
10

Now, let understand how for loop works? Here we have the initial value of i as 1(initialization).The test condition is i<=10 and update expression is i++.
In the beginning, the control goes to the initial condition and assigns the value 1 to i. Now its check whether 1<=10, if the condition is true then the body of the loop will execute and give an increment to i and assign the value 2 to i. Again its check whether 2<=10, if the condition is true then the body of the loop will execute. The whole process will run whenever the test condition is not met. When i becomes equal to 11, the condition i<=10 becomes false and the loop terminates and the program ends.


Friday, 22 September 2017

Selection Statement- the switch case statement

THE SWITCH CASE STATEMENT:-
A switch statement is used for multiple-way selections that will branch into different code segments based on the value of a variable or expression. This expression or variable must be of integer data type.

syntax:
switch (expression)
{
  case value1:
    code segment1;
    break;
  case value2:
    code segment2;
    break;
.
.
.
  case valueN:
    code segmentN;
    break;
  default:
    default code segment;
}
Above is the representation of the switch case. In switch-case, the program asks you about your choice and that choice will run. now understand its syntax. First, we provide the choice to the switch case and that particular choice will run after that program shows the result, it doesn't go to another part of the program because that particular case terminated by the break. The default choice is there if anyone tries to run the wrong choice it will show the default code segment.

program for basic calculation by using switch case:
#include<iostream>
using namespace std;
int main(){
 int ch,x,y,z;
 cout<<"1.ADDITION"<<endl;
 cout<<"2.SUBTRACTION"<<endl;
 cout<<"3.MULTIPLY"<<endl;
 cout<<"4.DIVISION"<<endl;
 cout<<"5.MODULUS"<<endl;
 cout<<"Enter your choice:";
 cin>>ch;
 switch(ch){
  case 1:
   cout<<"Enter first no:";
   cin>>x;
   cout<<"Enter second no:";
   cin>>y;
   z=x+y;
   cout<<"Addition of two no. is "<<z<<endl;
   break;
  case 2:
   cout<<"Enter first no:";
   cin>>x;
   cout<<"Enter second no:";
   cin>>y;
   z=x-y;
   cout<<"Subtraction of two no. is "<<z<<endl;
   break;
  case 3: 
   cout<<"Enter first no:";
   cin>>x;
   cout<<"Enter second no:";
   cin>>y;
   z=x*y;
   cout<<"Multiplication of two no. is"<<z<<endl;
   break;
  case 4:
      cout<<"Enter first no:";
   cin>>x;
   cout<<"Enter second no:";
   cin>>y;
   z=x/y;
   cout<<"division of two no. is "<<z<<endl;
   break;
  case 5:
   cout<<"Enter first no:";
   cin>>x;
   cout<<"Enter second no:";
   cin>>y;
   z=x%y;
   cout<<"Modulus of two no. is "<<z<<endl;
   break;
  default:
  cout<<"Wrong choice";
 }
 return 0;
}

output:
1.ADDITION
2.SUBTRACTION
3.MULTIPLY
4.DIVISION
5.MODULUS
Enter your choice:1
Enter first no:12
Enter second no:12
Addition of two no. is=24
In the above program, there are five choices if someone wants to choose a particular function just input the choice number and that particular function will run. The break will separate the function if you are not using the break after the break all functions will run. The default will show the wrong choice here.
Note: if you don't know what is the modulus? Please click here

Difference between if-else and switch case:

  • An if-else decision is taken on basis of the input wherein the switch-case decision is taken by the user.
  • if-else is more complex for the lengthy condition than switch-case.
  • if-else can check multiple conditions at a time where switch-case check the only single condition at a time








Monday, 18 September 2017

How to write a program without using semicolon in C++?

As we know every line in C++ ends with the semicolon but we can print a string without using any semicolon in the program. we can print string by using if-else, looping or switch case but here I'm print a string with if-else without using the semicolon.

program to print a string without using any semicolon:-
#include<iostream>
using namespace std;
 main()
{
 if(cout<<"Hello World"){
 }
}
 output:-
Hello World

In the above, you see that there is no semicolon used in the program and also it is most frequently asked interview question.


Thursday, 14 September 2017

Decision Making Statement:- If-else Statement

Control Statements enable us to specify the flow of program control,i.e. the order in which the instructions in a program must be executed. They make it possible to make decisions, to perform tasks repeatedly or to jump from one section of code to another.

There are four types of control statements in C++:
  •  Decision Making Statements
  •  Selection Statements 
  •  Iteration Statements 
  •  Jump Statements 
Decision-Making Statement: the if-else statement

The if-else statement is used to carry out a logical test and then take one of two possible actions depending on the outcome of the test(i.e. whether the outcome is true or false).
                     flowchart of if-else statement


Here, we give a condition to the program if the condition is true then the body inside the if statement is executed and, code inside the if will run, otherwise else statement is executed and, the code inside the if is skipped.

syntax:
if (condition)

{

  statements

}

  else

{

  statements

}
program to check whether a person is adult or not:
#include<iostream>
using namespace std;
int main()
{
int age;
cout<<"Enter the age:";
cin>>age;

if(age<18)
{
 cout<<"juvenile"<<endl; 
}

else 
{
 cout<<"Adult"<<endl;
}

return 0;
}
output:-
Enter the age:16
juvenile
Enter the age:21
Adult

Nested if and if-else statement

It is possible to embed or to nest if-else statements one within the other. Nesting is useful in situations where one or several different courses of action need to be selected.

syntax:
if(condition1)
{
// statement(s);
}
else if(condition2)
{
//statement(s);
}
.
.
.
.
else if (conditionN)
{
//statement(s);
}
else
{
//statement(s);
}

here, the program checks the condition of if  ,if-else and else, from these conditions which are true will be executed rest are skipped by the program.

program to find the greatest of three numbers:-
#include <iostream>
using namespace std;

int main()
{
    float n1, n2, n3;

    cout << "Enter three numbers: ";
    cin >> n1 >> n2 >> n3;

    if((n1 >= n2) && (n1 >= n3))
        cout << "Largest number: " << n1;
    else if ((n2 >= n1) && (n2 >= n3))
        cout << "Largest number: " << n2;
    else
        cout << "Largest number: " << n3;
    
    return 0;
}
output:-
Enter three numbers:12
34
23
Largest number:34

In the above program, if n1>n2 and n1>n3 then if statement will be executed, if n2>n1 and n2>n3 then if-else statement will be executed, if n3>n1 and n3>n2 then else statement will be executed.


Saturday, 2 September 2017

What is bits/stdc++.h in C++?(master file in C++)

<bits/stdc++.h> is a master file in c++. It is basically a header file which includes every standard library file. you need this file when you don't know which standard file is included in the program. It saves our time and solves the confusion. Let take an example if you want to find square root of a number, you have to include <math.h> file but if you use  <bits/stdc++.h> you don't need any other file.

#include<bits/stdc++.h>
using namespace std;
int main()
{
   float a;
   cout<<"Enter the number:";
   cin>>a;
   cout<<"Square root of the number is:"<<sqrt(a)<<endl;
   return 0;
}

output:-

Enter the number:3
Square root of the number is:1.73205

NOTE: <bits/stdc++.h> is use instead of <iostream> in C++.

ADVANTAGES:-


  1. In the competitive contest, using this file is a good idea, when you want to reduce the time wasted in doing chores, especially when your rank is time sensitive.
  2. It reduces typing in the program and solves the confusion.
  3. you don't have to remember all the  STL of GNU C++ for every function you use.
DISADVANTAGES:-


  1. It is not a standard header file of GNU C++ library. If you use other compilers it might fail. Like there is no such file in MICROSOFT VISUAL STUDIO.
  2. Using it would increase lots of unnecessary stuff and increases compile time.
  3. This header file is not a standard header file of C++ so, non-portable and should be avoided.

Tuesday, 29 August 2017

Topics in C++

Arithmetic Operations in C++(Addition,subtraction,multiplication,division and modulus) With The Help Of Global,Local And Default Variables.

Let understand arithmetic operations with the help of global and local variables.
The arithmetic operation is used for some computation or calculation.
Addition of two variables:-
#include<iostream>
using namespace std;
int x=6; //global variable

int main()
{
   int y=60,z; /* y is declared and initialized,y and z                 are local variable */
 
   z=x+y;
   cout<<"Addition:"<<z;
   return 0;
}


output:-
Addition:66
here x is the global variable(we can use the global variable outside the main function because it is available globally) y and z are local variables(we can use these variables inside the function in which they are declared). y is declared and initialized in the main function but you can give value to y  at the runtime by using cin.

Subtraction of two variables:-

#include<iostream>
using namespace std;
int main()
{
 int x,y,z;
 cout<<"Enter the value of x:";
 cin>>x; // gives value at runtime
 cout<<"Enter the value of y:";
 cin>>y;
 z=x-y;
 cout<<"Subtraction:"<<z;
 return 0;
}
output:-

Enter the value of x:53
Enter the value of y:30
subtraction:23
Multiplication of variables:-

#include<iostream>
using namespace std;
int main()
{
 float2 w=0,x,y,z; //default value for w
 cout<<"Enter the value of x:";
 cin>>x; // gives value at runtime
 cout<<"Enter the value of y:";
 cin>>y;
 z=w*x*y;
 cout<<"multiplication:"<<z;
 return 0;
}


output:

Enter the value of x:2
Enter the value of y:3
multiplication:0


here we have a default value to w that means if we forget to give a value to w it will automatically provide a value to w.
Take an example: if we assign a value to w on runtime say w=2, then answer will be 2*2*3=12 not zero.
do it yourself give the default value of w is zero then assign a value to w by using cin then see what will be the answer and also you can assign a value for w after default value through cin then latest value of w will be include in multiplication.

Division of two variables:-
#include<iostream>
using namespace std;
int main()
{
 float x,y,z; 
 cout<<"Enter the value of x:";
 cin>>x; 
 cout<<"Enter the value of y:";
 cin>>y;
 z=x/y;
 cout<<"Quotient:"<<z;
 return 0;
}

output:-
Enter the value of x:67
Enter the value of y:9
Quotient:7.44444
Modulus of two variables:-
#include<iostream>
using namespace std;
int main()
{
 float x,y,z; 
 cout<<"Enter the value of x:";
 cin>>x; 
 cout<<"Enter the value of y:";
 cin>>y;
 z=x%y;
 cout<<"Remainder:"<<z;
 return 0;
}

output:-
Enter the value of x:45
Enter the value of y:6
Remainder:3
when you divide two variables it gives you quotient where modulus give you the remainder.
if you use int variables is only provide either quotient or remainder, if you use float it will give you perfect value.
Example: divide 7 by 2 if you take 7 and 2 as the integer the quotient is 3 and modulus is 1 where if you take 7 and 2 as float it will give you 3.5 as answer.

                             HAPPY CODING

Wednesday, 23 August 2017

Data Types in C++

while doing programming in any programming language, you need some specific words to tell the compiler. Here I will explain these words.

TOKENS:-it is the smallest unit in a program, these are of five types.


  1. Keywords:-These are the words that convey a special meaning to the language compiler like default, case, break etc.
  2. Identifiers:-It is the name given by the user for a unit of the program.Identifiers can contain letters and digits.example are MYFILE,_DS,z2t0z9.
  3. Literals:-These are data items that never change their value during a program run (often referred to as constants).examples integer-constant,character-constant,floating-constant,string-literal.
  4. Punctuators:-It enhance a program's readability and give proper meaning to statements, expressions etc as per syntax. examples are {},[ ].
  5. Operators:-These are tokens that trigger some computation or action when applied to the variable and other objects in an expression.examples are arithmetic operators, logical operators, relational operators, conditional operators.i will explain it further.
DATA TYPES:-Data types are means to identify the type of data and associated operations for handling it.C++ data types are of two types:-

  1. Fundamental data types: These are those that are not composed of other data types.These are six fundamental data types in C++.
  • Int Data type: Int stores integer i.e. 34,-678 etc.They have no fractional parts.
  • Char Data type: Char stores character.character can store character variable.examples are &,b,% and 3.
  • Float Data type: Float stores floating-point numbers.it stores number having integer plus fractional part.example are  8.98888,7.89.
  • Double Data type: Double stores twice as int and float.
  • Void Data type: The void type specifies an empty set of values.It is used as the return type for functions that do not return a value.
  • Bool Data type: Bool stores Boolean algebra.It stores either true or false.

DATA TYPES MODIFIERS:-Expect type void, the basic data types can be modified by using modifiers such as


  • signed
  • unsigned 
  • long 
  • short
     2.Derived Data Type: These are derived from the fundamental data type.


  • Array: Arrays refer to a named list of a finite number n of similar data elements.Examples are ary[1],ary[2].......ary[n].
  • Function: A function is a named part of a program that can invoke from other parts of the program as often needed.
  • Pointer: A pointer is a variable that holds a memory address(type *ptr).
  • Reference: A reference is an alternative name of an object.The general declaring of reference variable is :type &ref-var=var-name;
  • Constant: The keyword const can be added to the declaration of an object to make that object a constant rather than a variable.The general declaration of constant is -const type name=value;




  • User-Defined Derived Data Types:-There are some derived data types that are defined by the user.
  1. Class:- A class represent a group of similar objects.
  2. Structure: A structure is a collection of variables referenced under one name, providing convenient means of keeping related information together.
  3. Union: A union is a memory location that is shared by two or more different variables, generally of different types at different times.
  4. Enumeration:- An alternative method for naming integer constants is often more convenient than const.
more information will be provided on these topics as we go deeper in C++.



Tuesday, 22 August 2017

What is namespace in C++?

when you use the namespace, your program looks like:-

#include<iostream>
using namespace std;
int main()
{
 cout<<"Hello World";
 return 0;
}
when you don't use the namespace, your program looks like:-

#include<iostream>
int main()
{
 std::cout<<"Hello World";
 return 0;
}
without "using namespace std;" when you write for example "cout<<;", you'd have to put "std::cout<<;",
Another role of the namespace is that it solves name conflicts among files and allow them to use with the same name.Let take an example,
you have a file let say main, and you want to include two files file_one and file_two in main but file_one and file_two both have the same function display when you include these file in the main file and call these file having the same function name it produces a conflict and shows an error.
The error will be the redefinition of function this is because we are not declaring display inside any declarative region such as namespace.C++ think you are redefining the function.To solve this conflicts we use the namespace.we use namespace then file name then we define the function in both file then we use this function in the main file in INT MAIN() { file_one::function}. so namespace avoids collision among same name file and makes them executable.




Thursday, 17 August 2017

Basic Syntax Of C++(what is main,#include,iostream,namespace and comments in c++)

program to print a string on the screen(DevC++)

The above program is one of the simplest programs that can be written in C++, but it does include basic elements that every C++ programs have. Let us have a  look at these elements one by one:

1. COMMENTS IN C++ PROGRAM 
Comments are pieces of code that the compiler simply does not execute.Comments are written for the explanation of the steps or program.There are two ways to insert comments in C++ programs.

(i) Single line comments with / /:-The comments that begin with //are single line comments.The compiler simply ignores everything following in that same line.In above picture line 1 represents the single line comment, whatever written after // will be discarded.
(ii)multi-line comments with /*.......*/:-The comments begin with /* and end with */.That means, everything that falls between /* and */will be discarded.In above picture line, 8 and 9 represent multi-line comments.

2. #INCLUDE
The statements that begin with the #(hash) sign are directives for the preprocessor(a computer program that modifies data to conform to the input requirements of another program).That means these statements are processed before compilation takes place.INCLUDE statement tells compiler's preprocessor to include the header file in the program.

3. INT MAIN (){.....}
The line indicates the beginning of the main function.Whatever written between the curly brackets {}
will be executed.It is essential to have the main function.INT is data type will be explained further.

4. IOSTREAM(header file)
The header file iostream is included in every C++ program to implement input/output facilities, without this file we cannot input or take the output from the program.

5. COUT(pronounced "see-out")
Cout stands for console output.It helps in printing the values on the screen.

6. CIN(pronounced "see-in")
Cin stands console input.It takes input from input devices.

7. INSERTION OPERATOR("<<")
Insertion is performed by insertion operator"<<".

8.EXTRACTION OPERATOR(">>")
Extraction is performed by the extraction operator">>".

9. SEMICOLON(;)
The semicolon (;) character used to finish every executable statement of a C++ program and it must be included after every executable instruction.

10. RETURN 0
The return instruction makes the main() to finish and it returns a value, in this case, it is returning 0(zero).Returning 0 is the most usual way of telling that program has terminated normally.

                      click here to know what is namespace  

Tuesday, 18 July 2017

How to install DevC++ and use it

Install DevC++ on Windows Operating System


Hello Folks,
Today I am going to share the steps how to download and install DevC++ in your Windows OS and also share how to use it and compile the program on it.

STEP BY STEP PROCEDURE TO INSTALL DEVC++ 

  1. As we know DevC++ IDE is a free portable, fast and simple C/C++ IDE.you can download it from here (open source) DOWNLOAD
  2. After opening the tab now, click on download (which will appear in the green box).
  3. After downloading the setup file double click on the Dev-Cpp 5.11 TDM-GCC 4.9.2 Setup.exe(approx 50MB).
  4. Then press Agree Button.
  5. Press Next Button.
  6. Then comes the next windows which give you the option to choose to install location. If you want to change the location then go to Browse the option and select the desired Destination folder. And Press the Install Button (Note the Destination Folder is the folder where all your Dev-C++ installation file and setup stores).
  7.  It will take some time to Install
    .
  8.  After Installing all the files it will ask the option to choose for whom you want to install the Dev-C++. I mean for all Users or for some specific one.
  9. Now you successfully installed the Dev-C++ IDE. If you want to Run then just click on Finish or Untick the check box option then Finish.
  10. Now you can Configure the software according to your convenient.
    Now Press Next->Next->OK to successfully open the IDE.

  1. Now click on DevC++ icon appears on the desktop.a tab appears on the window.
  2. click on file(in the top most left corner).then File->new->source file or press ctrl+N.

Sunday, 16 July 2017

Introduction To C++

The  C++(pronounced c plus plus) is a middle-level programming language was developed at AT&T Bell Laboratories in the early 1980s by Bjarne Stroustrup.C++ is a combination of  Simula 67(which is an object-oriented programming language )  and C.In early stages it is known as "C with classes".
The name C++ was coined by Rick Mascitti where "++" is the C increment operator.The maturation of the C++ language was attested to by two events:

  • The formation of an ANSI (American National Standard Institute) C++ committee and
  • The publication of the annotated C++ reference manual by Ellis and Stroustrup.
The latest C++ standards document was issued by ANSI/ISO in the year 2011 namely C++11 or formally C++0x.
The major reason behind the success and popularity of C++ is that it supports the object-oriented technology, the latest in the software development and the most near to the real world.C++ runs on the variety of platforms, such as Windows, Mac Os and the various versions of UNIX.