Sunday, 12 July 2015

Solution Of 5th Program

GUESSING  GAME

I hope this is the first guessing game u have ever made with c++


#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    char a;
    int tries=1;
do
{
    cout<<"Enetr Any alphabte from A to Z : ";
    cin>>a;
    if(a=='b'||a=='B')
    {
    cout<<"U win the game ";
    break;
    }
else
    {
    cout<<"U loss.Try again \n";
    tries++;
    }
}while(tries<=3);
getch();
}

Solution Of 4th question

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int i,j,k,num;
    cout<<"Enter Number : ";
    cin>>num;
    for(i=1;i>=num;i++)
    {
    for(j=1;j<=1;j++)
    {
    cout<<" * ";
    }
    for(k=1;k<2*(num-1);k++)
    {
    cout<<" ";
    }
                         
    for(j=1;j<=1;j++)
    {
      if(i==num&&j==num)
      {
           cout<<"";
      }
      else
      {
          cout<<" * ";
      }
      }              
    cout<<endl;
    }
    getch();
}

solution of 2nd question

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int num,i;
    cout<<"Enter The No : ";
    cin>>num;
    for(i=1;i<=10;i++)
    {
        cout<<num<<" * "<<i<<" = "<<num*i<<"\n";
    }
getch();
}

soution of 1st question

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int a=1,b=1,c=1;
    cout<<"Enter number 'a' ";
    cin>>a;
    cout<<"Enter number 'b' ";
    cin>>b;
    /* Swap the value of a to b and b to a */
    c=a;
    a=b;
    b=c;
    cout<<"After swaping a = "<<a<<"\n";
    cout<<"After swaping b = "<<b;
    getch();
}

Solution of 3rd question

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int i,j,num;
    cout<<"Enter Number : ";
    cin>>num;
    for(i=1;i<=num;i++)
    {
    for(j=1;j<=1;j++)
    {
    cout<<j<<" ";
    }
    cout<<endl;
    }
    getch();
}

Friday, 10 July 2015

Loops In C++

Loops

     These are also called as Repetitive control structure.Sometime We want a program or a part of program to repeat again and again then we use Loops.This type of program execution is called as looping.There Are three types of loops These are also called as 
  • While Loop
  • Do-While Loop
  • For Loop

While Loop:

  Structure of While loop.
while(condition)
{
      statement;
}
As from Above structure It is clear that how it work.First we shall Apply a condition.If condition is true then it repeats itself until the condition become false.Here is the example of while loop.I hope It shall Help You very much

Program 

/* 
In This program We shall find Table of the given number by the user with the help of while loop
*/
#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x=1,y=1,z=1;
    cout<<"Enter A number to find table ";
    cin>>x;
    while(z<=10)
{
    cout<<x<<" * "<<y<<" = "<<x*y<<"\n";
    z++;           // Every time z increase by one
    y++;v         // Every time y increase by one
}
getch();
}

    

OuTput

DO-While Loop :

            In Do-while loop the whole program repeats itself again and again until it's condition become false..

STRUCTURE

do
{
    statement;
}while(condition);


Note : That the loop body is always executed at least once. One important difference between the while loop and the do-while loop the relative ordering of the conditional test and loop body execution. In the while loop, the loop repetition test is performed before each execution the loop body; the loop body is not executed at all if the initial test fail. In the do-while loop, the loop termination test is Performed after each execution of the loop body. hence, the loop body is always executed least once.

Program

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x=0,y=0,z=0;
do
{
    cout<<"Enter Number ";
    cin>>x;
    cout<<"Enter 2nd NUmber ";
    cin>>y;
    cout<<"The sum Of the numbers is = "<<x+y<<"\n";
    cout<<"Do U want to repeat The Program Enter '1' to repeat ";
    cin>>z;
}while(z==1);
getch();
}



OUTPUT

You can Repeat it even more than 100000000000000+ times if every time u want to repeat it..

FOR LOOP

Structure of for loop
for (initialization; decision; increment/decrement)
{
   statement(s);
}
In a for loop there are 3 values needed
  • Initialization of loop control variable
  • Testing of loop control variable
  • Update the loop control variable either by incrementing or decrementing.
Operation (i) is used to initialize the value. On the other hand, operation (ii) is used to test whether the condition is true or false. If the condition is true, the program executes the body of the loop and then the value of loop control variable is updated. Again it checks the condition and so on. If the condition is false, it gets out of the loop.

Program


#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x;
    cout<<"Enter Number ";
    cin>>x;
    for(int i=1;i<=10;i++)
    {
        cout<<x<<" * "<<i<<" = "<<x*i<<"\n";
    }
    getch();
}


OUTPUT



Jump Statements

The jump statements unconditionally transfer program control within a function.

  • goto statement
  • break statement
  • continue statement
The goto statement
goto allows to make jump to another point in the program.
goto pqr;
pqr:
 pqr is known as label. It is a user defined identifier. After the execution of goto statement, the control transfers to the line after label pqr.

PROGRAM


#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x;
    abc:
    cout<<"Enter Number ";
    cin>>x;
    goto abc;
    getch();
}

OUTPUT

IT SHALL CONTINUE TO UNLIMITED TIME


The break statement
The break statement, when executed in a switch structure, provides an immediate
exit from the switch structure. Similarly, you can use the break statement in
any of the loop. When the break statement executes in a loop, it immediately exits from the loop.
The continue statement
The continue statement is used in loops and causes a program to skip the rest of the body of the loop.
while (condition)            
{
  Statement 1; 
    If (condition)
      continue;     
    statement; 
}
The continue statement skips rest of the loop body and starts a new iteration.
The exit ( ) function
The execution of a program can be stopped at any point with exit ( ) and a status code can be informed to the calling program. The general format is exit (code) ;
where code is an integer value. The code has a value 0 for correct execution. The value of the code varies depending upon the operating system.

Thursday, 9 July 2015

Use of If and Else In Program

Sometime in a program we want to use a condition in our program to perform a specific action in it.
For example I want to write a program in which if the number is odd the it show the ,massage that Number you entered is ODD otherwise it shows that i is Even then we use if and else in program,So that IF Number is ODD then tell the user that it is ODD and if Number is even it must show the user That it is EVEN.
In any program IF is used to input a condition in program while the else part shows the massage if IF part does not executed.

STRUCTURE OF IF AND ELSE

if(condition)
{
       statement;
}
else
{
 statement;
}

Now I try to explain you with the help of an example

Program

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x;
    cout<<"Plz Enter a Number ";
    cin>>x;
    if(x%2==1)                   // If You Want to Know WHY we use two equal signs Instead of one Click Here
{
    cout<<"The Number Is Odd ";
}
else
{
    cout<<"Number is Even ";
}
getch();
}

OUTPUT

Nested If

If we use another if condition inside an if  then this will called as nested if.

Program


#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x;
    cout<<"Plz Enter a Number ";
    cin>>x;
if(x!=1)                                //  we use  !=  for " Not equal to " sign
{
if(x%2==1)
{    
    cout<<"X is equal to 1 ";
}
}
else
{
    cout<<"Value of x is Unknown ";
}
getch();
}




Use Of if Inside Of an else statement

We can also enter an condition inside an else statement By the use of IF in it..

Program

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x;
    cout<<"Plz Enter a Number ";
    cin>>x;
if(x%2==1)                                //  we use  
{
    cout<<"X is odd ";
}
else if(x%2==0)
{
    cout<<"X is Even ";
}
getch();
}

Output


Difference Between One equal sign And Two Equals in Programming

we use two equal signs(==) to Compare two values as we do with one equal sign in Mathematics And if we use one Equal sign it shall Assign the value to the variable.
To understand that compare the given program below

Program with one Equal..

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x;
    cout<<"Plz Enter a Number ";
    cin>>x;
  x=25;
    cout<<"The value of X is "<<x;
getch();
}

OUTPUT:

When We use one Equal then it Assigns Value to X and shows The Answer equal 25 regardless of what ever Value User enter in it.

Program with = =..

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x;
    cout<<"Plz Enter a Number ";
    cin>>x;
    if(x==1)                                //  we use  
{
    cout<<"X is equal to 1 ";
}
else
{
    cout<<"Value of x is Unknown ";
}
getch();
}

OUTPUT

IF User in put 1 then it shall show the massage that "X is equal to 1 ";
Otherwise it shall show massage that "Value of x is Unknown ";

Wednesday, 8 July 2015

First Program in C++

// This is my first program in C++ 
/* this program will illustrate different components of
a simple program in C++ */
 
#include <iostream>
using namespace std;
 
int main()
{
   cout << "Hello World!";
   return 0;
}

When the above program is compiled, linked and executed, the following output is displayed on the VDU screen.
Hello World!
Various components of this program are discussed below:

Comments

First three lines of the above program are comments and are ignored by the compiler. Comments are included in a program to make it more readable. If a comment is short and can be accommodated in a single line, then it is started with double slash sequence in the first line of the program. However, if there are multiple lines in a comment, it is enclosed between the two symbols /* and */

#include <iostream>

The line in the above program that start with # symbol are called directives and are instructions to the compiler. The word include with '#' tells the compiler to include the file iostream into the file of the above program. File iostream is a header file needed for input/ output requirements of the program. Therefore, this file has been included at the top of the program.
using namespace std;
All the elements of the standard C++ library are declared within std. This line is very frequent in C++ programs that use the standard library.

int main ( )

The word main is a function name. The brackets ( ) with main tells that main ( ) is a function. The word int before main ( ) indicates that integer value is being returned by the function main (). When program is loaded in the memory, the control is handed over to function main ( ) and it is the first function to be executed.

Curly bracket and body of the function main ( )

A C++ program starts with function called main(). The body of the function is enclosed between curly braces. The program statements are written within the brackets. Each statement must end by a semicolon, without which an error message in generated.

cout<<"Hello World!";

This statement prints our "Hello World!" message on the screen. cout understands that anything sent to it via the << operator should be printed on the screen.

return 0;

This is a new type of statement, called a return statement. When a program finishes running, it sends a value to the operating system. This particular return statement returns the value of 0 to the operating system, which means “everything went okay!”.

Printing Multiple Lines of Text with a Single Statement


/* This program illustrates how to print multiple lines of text 
with a single statement */
 
#include <iostream>
using namespace std;
 
int main()
{
   cout << "Welcome\nto\nC++"; 
   return 0;
}

Output:
Welcome
to
C++

The characters print exactly as they appear between the double quotes. However, if we type \n, the characters \n are not printed on the screen. The backslash (\) is called an escape character. It indicates that a "special" character is to be output. When a backslash is encountered in a string of characters, the next character is combined with the backslash to form an escape sequence. The escape sequence \n means newline. It causes the cursor to move to the beginning of the next line on the screen.
These following operators are called Assignment Operator :
#include   --> Pre-Processor Directive
cout<<    --> extreme exertion
cin>>      --> extreme Inserstion
The following table gives a listing of common escape sequences.
Escape SequenceDescription
\nNewline
\tHorizontal tab
\aBell (beep)
\\Backslash
\'Single quote
\''Double quote

Operator And There Use

In a C++ program we use five basic MATHEMATICS OPERATORS 
+     for addition
-     for subtraction
*    for multiplication 
/     for Division 
%   for finding the remainder 

HERE IS Examples OF THESE OPERATORS IN Program

Use of " + " OPERATOR

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x,y;
    x=25;
    y=10;
    cout<<"Plz enter First Number ";
    cin>>x;
    cout<<"Plz Enter second Number ";
    cin>>y;
    cout<<"The sum of both number is = "<<x+y;
    getch();
}

OUTPUT

USE OF " - " OPERATOR

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x,y;
    cout<<"Plz enter First Number ";
    cin>>x;
    cout<<"Plz Enter second Number ";
    cin>>y;
    cout<<"The Answer  of both number by subtracting is = "<<x-y;
    getch();
}

OUTPUT


USE OF " * " OPERATOR

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x,y;
    cout<<"Plz enter First Number ";
    cin>>x;
    cout<<"Plz Enter second Number ";
    cin>>y;
    cout<<"The Answer by Multiplying both number is = "<<x*y;
    getch();
}


OUTPUT

USE OF " / " OPERATOR

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x,y;
    cout<<"Plz enter First Number ";
    cin>>x;
    cout<<"Plz Enter second Number ";
    cin>>y;
    cout<<"The Answer by Dividing both number is = "<<x/y;
    getch();
}

OUTPUT

USE OF " % " OPERATOR

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x,y;
    cout<<"Plz enter First Number ";
    cin>>x;
    cout<<"Plz Enter second Number ";
    cin>>y;
    cout<<"The Remainder of both number is = "<<x%y;
    getch();
}

OUTPUT 

In the same way we can use one or more operators at same time

USE OF ALL OPERATOR IN A PROGRAM

#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x,y;
    cout<<"Plz enter First Number ";
    cin>>x;
    cout<<"Plz Enter second Number ";
    cin>>y;
    cout<<"The sum of both number is = "<<x+y<<"\n";
    cout<<"The Answer  of both number by subtracting is = "<<x-y<<"\n";
    cout<<"The Answer by Multiplying both number is = "<<x*y<<"\n";
    cout<<"The Answer by Dividing both number is = "<<x/y<<"\n";
    cout<<"The Remainder of both number is = "<<x%y;
    getch();
}

OUTPUT

QUESTION???

Write a program which take five numbers from user and in return shows the average of the numbers???

Tuesday, 7 July 2015

Initialization of Variables

Variables And there Initialization


What are variables????
               Variables are memory location that we use in a program to store a value in computer.We can change this value when ever we want...
for example
      We want to add two numbers and we also want to take numbers from the user then we must had atleast two variables in program.
Here is the example of the variables in a program


#include<iostream.h>
#include<conio.h>
using namespace std;
int main()
{
    int x,y;
    cout<<"Plz enter First Number ";
    cin>>x;
    cout<<"Plz Enter second Number ";
    cin>>y;
    cout<<"The sum of both number is = "<<x+y;
    getch();
}

<iosream.h>

 is a header file which is used in program for "cout (to show massage or output to user ) " and "cin (to take or input value from user )


<conio.h>

 is use for "getch(); .This shall help to avoid instant closing of Program.Program shall remain on screen until you click any key to close it.

Int 

is a data type.In int data type we can only store integers in it.To know about other Data Types Click Here.
The Output of The above program is given below...

Initialization Of Variable

Why we initialize variables????

we initialize a variable because sometimes program enter the garbage value in the program instead of the value given by the user,and this produce wrong RESULT in the end.

Now Lets see how to initialize a value

type identifier = initial_value; 
For example, to declare a variable of type int called x and initialize it to a value of zero from the same moment it is declared, we can write:

 
int x = 0;


A second method, known as constructor initialization (introduced by the C++ language), encloses the initial value between parentheses (()):

type identifier (initial_value); 
For example:

 
int x (0);


Finally, a third method, known as uniform initialization, similar to the above, but using curly braces ({}) instead of parentheses (this was introduced by the revision of the C++ standard, in 2011):

type identifier {initial_value}; 
For example:

 
int x {0}; 


All three ways of initializing variables are valid and equivalent in C++.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// initialization of variables

#include <iostream>
#include <conio.h>
using namespace std;

int main ()
{
  int a=5;               // initial value: 5
  int b(3);              // initial value: 3
  int c{2};              // initial value: 2
  int result;            // initial value undetermined

  a = a + b;
  result = a - c;
  cout << result;

  getch();
}

DATA SIZE

SizeUnique representable valuesNotes
8-bit256= 28
16-bit65 536= 216
32-bit4 294 967 296= 232 (~4 billion)
64-bit18 446 744 073 709 551 616= 264 (~18 billion billion)

DATA TYPES

DATA TYPES

Here is the complete list of fundamental DATA types in C++:
GroupType names*Notes on size / precision
Character typescharExactly one byte in size. At least 8 bits.
char16_tNot smaller than char. At least 16 bits.
char32_tNot smaller than char16_t. At least 32 bits.
wchar_tCan represent the largest supported character set.
Integer types (signed)signed charSame size as char. At least 8 bits.
signed short intNot smaller than char. At least 16 bits.
signed intNot smaller than short. At least 16 bits.
signed long intNot smaller than int. At least 32 bits.
signed long long intNot smaller than long. At least 64 bits.
Integer types (unsigned)unsigned char(same size as their signed counterparts)
unsigned short int
unsigned int
unsigned long int
unsigned long long int
Floating-point typesfloat
doublePrecision not less than float
long doublePrecision not less than double
Boolean typebool
Void typevoidno storage
Null pointerdecltype(nullptr)

                                         OR

In simple form you can remember them as follow
char = 1 byte
int   =  4 byte ( Use to store numbers )
long long = 8 byte (Use to store large numbers numbers )
float = 4 byte (Use to store numbers Having Decimal)
double = 8 byte (Use to store large numbers Having Decimal )

KEY WORDS

Reserve keywords in CPP compiler

C++ uses a number of keywords to identify operations and data descriptions; therefore, identifiers created by a programmer cannot match these keywords. The standard reserved keywords that cannot be used for programmer created identifiers or variables are:

alignas,
 alignof,
 and,
 and_eq,
 asm,
 auto,
 bitand,
 bitor,
 bool,
 break,
 case,
 catch,
 char,
 char16_t,
 char32_t,
 class,
 compl,
 const,
 constexpr,
 const_cast,
 continue,
 decltype,
 default,
 delete,
 do,
 double,
 dynamic_cast,
 else,
 enum,
 explicit,
 export,
 extern,
 false,
 float,
for, 
friend, 
goto, 
if, 
inline,
 int, 
long, 
mutable,
 namespace, 
new, 
noexcept,
 not, 
not_eq,
 nullptr,
 operator,
 or,
 or_eq, 
private,
 protected,
 public, 
register, 
reinterpret_cast,
 return, 
short, 
signed, 
sizeof, 
static,
 static_assert, 
static_cast,
 struct,
 switch,
 template,
 this, 
thread_local,
 throw, 
true, 
try, 
typedef, 
typeid, 
typename,
 union, 
unsigned, 
using,
 virtual, 
void, 
volatile, 
wchar_t, 
while, 
xor, 
xor
_eq

Specific compilers may also have additional specific reserved keywords.

Introduction to Programming

What is Programming???
To tell the computer what to do is known as Programming..

It is the simplest definition of programming.. Through programming we convey our massage to computer in his language.
We all know that computer is a machine and it does not has a brain .It only understand the binary language (0 or 1) only..
Types of programming languages:
There are two main types of programming languages
         High level language
         Low level language
High level language:
              The language which is only understandable by Human machine but not by computer.
For example:
      C++, C, Java etc.
Low level language:
            The language which is only understandable by computer.
For example:
             Machine Language
What is Integrated Development Environment (IDE)???
An integrated development environment (IDE) is a programming environment that has been packaged as an application program, typically consisting of a code editor, a compiler a debugger, and a graphical user interface builder. The IDE may be a standalone application or may be included as part of one or more existing and compatible applications. The basic programming language, for example, can be used within Microsoft Office applications, which makes it possible to write a WordBasic program within the Microsoft Word application. IDEs provide a user-friendly framework for many modern programming languages, such as C,C++.
Editor:
              Part of IDE were source code can be written..
Compiler:
              Part of IDE which convert  Source code into Assembly Language
Linker:
              Part of IDE which connects Source Code with the Header Files..
Assembler:
              Part of IDE which convert Assembly Language into Machine Language
Loader:
              Part of IDE which Loads The compiled code on Memory
Debugger:
               Pat of IDE which is Helpful in finding LOGICAL errors
                                       An IDE works as follow

                     


First we write code in editor,After writing code compiler in IDE compile it. After compile it change it into assembly language, after Assembly language it is further changed into to Machine language.   
Types of Errors In source code:
  1. Logic Errors
  2. Syntax Errors

What is Logical Error???
  • 1.    Based on Poor logic of Program
  • 2.    Compiled Successfully And produce wrong Result
  • 3.    Difficult to DETECT and Remove
  • 4    Debugger is Helpful in finding Logical Errors

What is Syntax Error???
  • 1.    Base on any Error in the Source Code
  • 2.    Program does not Compile
  • 3.    Easy to Detect
  • 4.    Easily Removable 



Now we shall Move toward c++ Language.You must has A dev c++ installed in your PC or Laptop
You can install Dev c++ from Link Below
                                  Click Here to DOWNLOAD DEV C++

                                            For Any Question COMMENT below