Sunday, August 25, 2013

IF Statement

Example 1:

// IF Statement

#include <stdio.h>
int main()
{
   int a,b,sum;
   printf("Enter the value of a and b \n");
   scanf("%d%d",&a,&b);
   sum = a + b;
   printf("Sum of a and b is %d\n", sum);
   if(sum > 50)
   {
       printf("Sum is greater than 50 \n");
   }
  /* else if(sum > 40)
   {
       printf("Sum is greater than 40\n");
   }
   else if(sum > 30)
   {
       printf("Sum is greater than 30\n");
   }

   else if(sum > 20)
   {
       printf("Sum is greater than 20\n");
   }
  else
   {
       printf("Sum is less than 10 \n");
   }*/
return 0;
}

//Video Tutorial for If Statement




Example 2:

#include <iostream>
using namespace std;
int main()
{
   int a,b,sum;
   cout <<"Enter the value of a and b \n";
   cin >> a >>b;
   sum = a + b;
   cout <<"Sum of a and b is "<<sum<<"\n";
   if(sum > 20)
   {
       cout <<"Sum is greater than 20 \n";
   }
   else if(sum > 10)
   {
       cout <<"Sum is greater than 10 and less than 20\n";
   }
   else
   {
       cout <<"Sum is less than 10 \n";
   }
return 0;
}

Variables

// Variables

Rules of Declaring Variables in C++
  • Variable name can consist of letter, alphabets and start with underscore character.
  • First character of variable always should be alphabet and cannot be digit.
  • Blank spaces are not allowed in variable name.
  • Special characters like #, $ are not allowed.
  • A single variable can only be declared for only 1 data type in a program.
  • Upper and lowercase letters are distinct because C++ is case-sensitive, so if we declare a variable name and one more NAME both are two different variables.
  • C++ has certain keywords which cannot be used for variable name.
  • A variable name can be consist of 31 characters only if we declare a variable more than 1 characters compiler will ignore after 31 characters.
The following is a table of the names of the types of variables:
TypeDescription
boolStores either value true or false.
charTypically a single octet(one byte). This is an integer type.
intThe most natural size of integer for the machine.
floatA single-precision floating point value.
doubleA double-precision floating point value.
voidRepresents the absence of type.
wchar_tA wide character type.


Example 1:

#include <stdio.h>
int a=5;
int b=2;
void add()
{
int sum;
sum = a + b;
printf("Sum of a and b is %d\n",sum);
}
int main()
{
add();
return 0;
}


//Video Tutorial





Example 2:

#include <iostream>
using namespace std;
int a=5;
int b=2;
void add()
{
int sum;
sum = a + b;
cout <<"Sum of a and b is "<<sum<<"\n";
}
int main()
{
add();
return 0;
}

Friday, August 23, 2013

Relational Operators

Example 1:

// Relational Operators in C

#include <stdio.h>
int main()
{
    int a,b;
    printf("Enter the values of a and b \n");
    scanf("%d %d", &a,&b);
    if(a > b)
    printf("%d is greater than %d\n",a,b);
    else if(a < b)
    printf("%d is less than %d\n",a,b);
    if(a <= b)
    printf("%d is less than or equal to %d\n",a,b);
    else if(a >= b)
    printf("%d is greater than or equal to %d\n",a,b);
    if(a == b)
    printf("%d is equal to %d\n",a,b);
    else if (a != b)
    printf("%d is not equal to %d\n",a,b);
    return 0;
}



//Video Tutorial



Example 2:

// Relational Operators in C++

#include <iostream>
using namespace std;
int main()
{
    int a,b;
    cout <<"Enter the values of a and b \n";
    cin >>a >>b;
    if(a > b)
    cout <<a <<" is greater than " <<b <<"\n";
    else if(a < b)
    cout <<a <<" is less than " <<b <<"\n";
    if(a <= b)
    cout <<a <<" is less than or equal to " <<b <<"\n";
    else if(a >= b)
    cout <<a <<" is greater than or equal to" <<b <<"\n";
    if(a == b)
    cout <<a <<" is equal to" <<b <<"\n";
    else if (a != b)
    cout <<a <<" is not equal to " <<b <<"\n";
    return 0;
}


Logical Operators

Example 1:
//Logical operators in C
#include <stdio.h>
int main()
{
   int a,b,c;
   printf("Enter the values of a,b, and c\n");
   scanf("%d %d %d", &a, &b, &c);
   if((a > b) && (a > c))
   printf("a is greatest \n");
   else if(b > c)
   printf("b is greatest \n");
   else
   printf("c is greatest \n");
   if((a == 0) || (b == 0) || (c == 0))
   printf("The product of a, b and c is zero \n");
   return 0;
}

//Video Tutorial





Example 2:

//Logical operators in C++
#include <iostream>
using namespace std;
int main()
{
   int a,b,c;
   cout <<"Enter the values of a,b, and c\n";
   cin >>a >>b >>c;
   if((a > b) && (a > c))
   cout <<"a is greatest \n";
   else if(b > c)
   cout <<"b is greatest \n";
   else
   cout <<"c is greatest \n";
   if((a == 0) || (b == 0) || (c == 0))
   cout <<"The product of a, b and c is zero \n";
   return 0;
}

Increment/Decrement Operator

Example 1:
//Increment and Decrement Operators in C

#include <stdio.h>
int main()
{
    int a=1;
    printf("a's value is now %d\n", a++);
    printf("a's value is now %d\n", a);
    a=1;
    printf("a's value is now %d\n", ++a);
    printf("a's value is now %d\n", a);
    a=1;
    printf("a's value is now %d\n", a--);
    printf("a's value is now %d\n", a);
    a=1;
    printf("a's value is now %d\n", --a);
    printf("a's value is now %d\n", a);
    return 0;
}
 

Example 2:

//Increment and Decrement Operators in C++

#include <iostream>
using namespace std;
int main()
{
    int a=1;
    cout <<"a's value is now "<<a++ <<"\n";
    cout <<"a's value is now "<<a <<"\n";
    a=1;
        cout <<"a's value is now "<<++a <<"\n";
    cout <<"a's value is now "<<a <<"\n";
    a=1;
    cout <<"a's value is now "<<a-- <<"\n";
    cout <<"a's value is now "<<a <<"\n";
    a=1;
    cout <<"a's value is now "<<--a <<"\n";
    cout <<"a's value is now "<<a <<"\n";
    return 0;
}



//Video Tutorial


    

Airthmetic Operator

Example 1:
// Arithmetic Operators in C
#include <stdio.h>
int main()
{
    int a,b;
    float c;
    a = 5;
    b = 2;
    c - a + b;
    printf("Sum of %d and %d is %.2f\n",a,b,c);
    c = a * b;
    printf("Product of %d and %d is %.2f\n",a,b,c);
    c = a / b;
    printf("Integer Division of %d and %d is %.2f\n",a,b,c);
    c = (float)a / b;
    printf("Real Division of %d and %d is %.2f\n",a,b,c);
    return 0;
}


// Video Tutorial for how to run an Arithmetic Operator





Example 2:

// Arithmetic Operators in C++
#include <iostream>
using namespace std;
int main()
{
    int a,b;
    float c;
    a = 5;
    b = 2;
    c = a + b;
    cout <<"Sum of " <<a <<" and " <<b <<" is " <<c <<"\n";
    c = a * b;
    cout <<"Product of " <<a <<" and " <<b <<" is " <<c <<"\n";
    c = a / b;
    cout <<"Integer Division of " <<a <<" and " <<b <<" is " <<c <<"\n";
    c = (float)a / b;
    cout <<"Real Division of " <<a <<" and " <<b <<" is " <<c <<"\n";
    return 0;
}


Simple C++ Program

//My First C++ program
#include <iostream>
using namespace std;
int main()
{
   cout <<"Talk To a Teacher\n";
   return 0;
}

//Video Tutorial for How to Run an Simple C++ Program