Basic Of Of C++ Programming || Data Types, cout, cin, If-else, for, while in cpp | Math Table Generator In cpp

 Basic Of Of C++ Programming 

Starter Template Of C++ :

#include <iostream> //HEADER FILE
using namespace std;

int main() //MAIN FUNCTION WHICH RETURN TYPE IS INTEGER
{
    //WRITE STUFF HERE
    return 0;
}


Data Types in C++:


/*
Data Type   Meaning                  Size (in Bytes)
int         Integer                    2 or 4
float      Floating-point                4
double     Double Floating-point         8
char        Character                    1
wchar_t     Wide Character               2
bool        Boolean                      1
void        Empty                        0
*/


How to Print In C++:-

#include <iostream> //HEADER FILE
using namespace std;

int main() //MAIN FUNCTION WHICH RETURN TYPE IS INTEGER
{
    cout<<"Hello India!"<<endl; //cout is used to print some values
    return 0;
}

Output - 

Hello India!


How to Take Input From User In C++:-

#include <iostream> //HEADER FILE
using namespace std;

int main() //MAIN FUNCTION WHICH RETURN TYPE IS INTEGER
{
    int x; //Defining a variable named "x" which data type is integer
    cout<<"Please Enter A Number"<<endl;//Asking User To Enter A Number
    cin>>x; //Taking Input From User
    cout<<"User Entered "<<x<<endl; //Printing The Values Entered By User
    return 0;
}

Output - 

Please Enter A Number

22

User Entered 22


If-Else In C++:-

#include <iostream> //HEADER FILE
using namespace std;

int main() //MAIN FUNCTION WHICH RETURN TYPE IS INTEGER
{
    const int real_pin = 2204;
    int pin; 
    cout<<"Please Enter Your 4-DIgit ATM Card PIN "<<endl;
    cin>>pin; //Taking Input From User
    if(pin == real_pin) //Checking Condition
    {
        cout<<"PIN Matched Successfully"<<endl; //Statement
    }
    else //Else Condition
    {
        cout<<"Wrong PIN"<<endl; //Statement
    }
    
    /*
   YOU CAN WRITE IT IN THIS MANNER 

    if (pin == real_pin)
    cout<<"PIN Matched Successfully"<<endl;
    else
    cout<<"Wrong PIN"<<endl;
    */
    return 0;
}


Output - 

Please Enter Your 4-DIgit ATM Card PIN 

2204

PIN Matched Successfully


For Loop In C++:-

#include <iostream> //HEADER FILE
using namespace std;

int main() //MAIN FUNCTION WHICH RETURN TYPE IS INTEGER
{
    cout<<"Printing 10 Numbers With For Loop"<<endl;
    for (int i = 0; i < 10; i++) 
    //Syntax - for(data_type var = initial_value; condtion; any operation)
    {
        cout<<i<<endl;
    }
    
    /*YOU CAN WRITE IT IN THIS MANNER 
    for (int i = 0; i < 10; i++) 
    cout<<i<<endl;
    */

    return 0;
}

Output - 

Printing 10 Numbers With For Loop

0

1

2

3

4

5

6

7

8

9


While Loop In C++:-

Syntax - 

  while (/* condition */)
    {
        /* code */
    }
    

Math Table Generator Using While Loop - 

#include <iostream> //HEADER FILE
using namespace std;

int main() //MAIN FUNCTION WHICH RETURN TYPE IS INTEGER
{
    //for ex.- 12 x 3 = 36
    int num; //12
    int res; //36
    int multi_num = 1;//3
    cout<<"Printing Table In C++"<<endl;
    cout<<"Which Digit Table Do You Want?"<<endl;
    cin>>num;
    
    while (multi_num<11)
    {
        res = num*multi_num; //36 = 12*3
        cout<<num<<" x "<<multi_num<<" = "<<res<<endl; //Printing Results
        multi_num++; //Incrementing Mutiplication Number - 3
        
    }
    
    /*YOU CAN WRITE IT IN THIS MANNER 
    
    while (multi_num<10)
    res = num*multi_num; //36 = 12*3
    cout<<num<<" x "<<multi_num<<" = "<<res<<endl; //Printing Results
    multi_num++; //Incrementing Mutiplication Number - 3
        
    
    */

    return 0;
}



Output - 

Which Digit Table Do You Want?

12

12 x 1 = 12

12 x 2 = 24

12 x 3 = 36

12 x 4 = 48

12 x 5 = 60

12 x 6 = 72

12 x 7 = 84

12 x 8 = 96

12 x 9 = 108

12 x 10 = 120


Comments

Popular posts from this blog

Array Of Objects In C++ | Array Of Objects Explained With Real Life Example

Pointers In C++ | Pointer Explained With Real Life Examples | Pointers In OOPs C++

Destructor In C++ | Destructor Explained With Real Life Examples