1 Comment

How we can make simple returning user defined function in c++ code


It is very easy to understand the use of simple returning user defined function. By these functions we can make easy the code to understand. There are two types of the function more.
(i)Default Function                        (ii)Parametric Function

Simple Structure:


dataTypeName  FunctionName ()
{

}

Code without Prtotype:


#include <iostream>
using namespace std;
int ans;
int Fun(int a, int b)
{
                ans=a+b;
                return ans;
}
int main()
                {
                                int Total;
                                Total=Fun(2,8);
                                cout<<Total;
                }

Output:


10
Hope you understand the use of simple returning user defined in c++.

Code with Prototype:


#include <iostream>
using namespace std;
int ans;
int Fun(int a, int b);
int main()
{
                int Total;
                Total=Fun(2,7);
                cout<<"Total : "<<Total;
}
int Fun(int a, int b)
{
                ans=a+b;
                return ans;
}

Output:



1 Comment

How we can make simple non-returning user defined function in c++



It is very easy to know How we can make simple non-returning user defined function in c++. These functions not return the value. There is a lot of use of these functions in c++. Non-returning functions are mostly used in c++. You can watch the code to understand the non-returning user defined functions.

Simple Structure:


dataTypeName  FunctionName ()
{

}

Code without Prototype:


#include <iostream>
using namespace std;
void Fun()
{
                cout<<"How are you."<<endl;
                cout<<"I am fine. ";
}
int main()
{
                Fun();
}

Output:


How are you.
I am fine.

Code with Prototype:


#include <iostream>
using namespace std;
void Fun();
int main()
{
                Fun();
}
void Fun()
{
                cout<<"How are you."<<endl;
                cout<<"I am fine. ";
}

Output:


How are you.
I am fine.
Hope you understand the non-returning user defined functions in c++.

Click here for watching the video of that code



No Comment

Function overloading in c++ code:

Function overloading is very important in c++. In the function overloading, the name of both functions same but the parameters in the function are different. You can understand the use of function overloading by watching the code below.

Code without Prototype:


#include <iostream>
using namespace std;
void fun(int x)
{
                cout<<"Here printing integer number "<<x<<endl;
}
void fun(float x)
{
                cout<<"Here printing float number "<<x<<endl;
}
int main()
{
                int a=44;
                float b=66.89;
                fun(a);
                fun(b);
}

Code with Prototype:


#include <iostream>
using namespace std;
void fun(int x);
void fun(float x);
int main()
{
                int a=44;
                float b=66.89;
                fun(a);
                fun(b);
}
void fun(int x)
{
                cout<<"Here printing integer number "<<x<<endl;
}
void fun(float x)
{
                cout<<"Here printing float number "<<x<<endl;
}

Output:


Here printing integer number 44
Here printing float number 66.89

The output will be same in both codes. Hope you will understand the use of function overloading in c++.


Click here for watching the video of that code

No Comment

Simple Predefined functions in c++


Function Name
Header File
Return Value
Parameter type
Return Type
abs(x)

<cstdlib>

Returns the absolute value of its argument: abs (-7) = 7

int
int
ceil(x)

<cmath>

Returns the smallest whole number that is not less than x: ceil (56.44) = 57.0

double
double
cos(x)

<cmath>

Returns the cosine of angle x: cos(0.0) = 1.0

double
Double
exp(x)

<cmath>

Returns ex , where e=  2.71828 : exp(1.0) = 2.71828

double
double
fabs(x)

<cmath>

Returns the absolute value of its argument: fabs(-5.67) = 5.67
double
double
floor(x)

<cmath>

Returns the largest whole number that is not greater than x: floor(45.67) = 45.00

double
double
pow (x, y)

<cmath>

Returns xy : if x is negative, y must be a whole number : pow(0.16, 0.5) = 0.4

double
double








No Comment

How we can make simple shapes from for loop in c++ code



Simple Triangle Code:


#include <iostream>
#include <string>
using namespace std;
int main()
                {
        for(int i=0; i<=5; i++)
        {
                for(int j=0; j<=i; j++)
                {
                                cout<<"*";
                                                }
                                                cout<<endl;
                                }
                }

Output:

*
**
***
****
*****

Other type of Triangle Code:


#include <iostream>
#include <string>
using namespace std;
int main()
                {
        for(int i=5; i>=0; i--)
        {
                for(int j=0; j<=i; j++)
                {
                                cout<<"*";
                                                }
                                                cout<<endl;
                                }
                }

Output:


******
*****
****
***
**
*

Square Shape Code:


#include <iostream>
#include <string>
using namespace std;
int main()
                {
        for(int i=0; i<=3; i++)
        {
                for(int j=0; j<=5; j++)
                {
                                cout<<"*";
                                                }
                                                cout<<endl;
                                }
                }

Output:


******
******
******
******

Diamond Shape Code:

#include <iostream>
using namespace std;
int main()
{
int row,col;
cout<<"Enter number rows : ";
cin>>row;
cout<<"Enter number of colums : ";
cin>>col;
       for(int i=0,l=0; i<=col,l<=col; i=i+2,l=l+2)
{
for(int j=row; j>=l-2; j=j-2)
{
cout<<" ";
        }
        for(int k=0; k<=i-2; k++)
        {
    cout<<"*";
}
cout<<endl;
}
for(int i=0,l=0; i<=col,l<=col; l++,i=i+2)
{
for(int k=0; k<=l; k++)
       {
    cout<<" ";
}
for(int j=row; j>=i; j--)
{
cout<<"*";
        }
   cout<<endl;
 }
}

Output:

Enter number rows : 8
Enter number of colums : 8

         *
       ***
     *****
   *******
 *********
   *******
     *****
      ***
        *

Right Triangle Code:

#include <iostream>
using namespace std;
int main()
{
int row,col;
cout<<"Enter number rows : ";
cin>>row;
cout<<"Enter number of colums : ";
cin>>col;
for(int i=0; i<=col; i=i+2)
{
for(int j=row; j>=i; j=j-2)
{
cout<<" ";
   }
   for(int k=0; k<=i; k++)
   {
    cout<<"*";
}
cout<<endl;

}
Output:

Enter number rows : 8
Enter number of colums : 8
         *
       ***
     *****
   *******
 *********

Opposite Right Triangle Code:

#include <iostream>
using namespace std;
int main()
{
int row,col;
cout<<"Enter number rows : ";
cin>>row;
cout<<"Enter number of colums : ";
cin>>col;
for(int i=0,l=0; i<=col,l<=col; l++,i=i+2)
{
for(int k=0; k<=l; k++)
   {
    cout<<" ";
}
for(int j=row; j>=i; j--)
{
cout<<"*";
   }
   cout<<endl;
   
}

}
Output:

Enter number rows : 8
Enter number of colums : 8
 *********
   *******
     *****
       ***
         *

Another Triangle Code:

#include <iostream>
using namespace std;
int main()
{
int row,col;
cout<<"Enter number rows : ";
cin>>row;
cout<<"Enter number of colums : ";
cin>>col;
for(int i=0; i<=col; i++) 
 { 
 for(int j=row; j>=i; j--) 
 { 
 cout<<"*"; 
 } 
 cout<<endl; 
 for(int k=0; k<=i; k++) 
 { 
 cout<<" "; 
 } 
 }
 }

Output:

Enter number rows : 6
Enter number of colums : 6
*******
  ******
    *****
      ****
        ***
          **
            *




Click here for watching the video of that code






No Comment

How we can use simple do while loop in c++ code


In do while loop, the ststement will execute one time first and then check the condition. One statement will execute in every condition.

Code:


#include <iostream>
#include <string>
using namespace std;
int main() {
                int i;
                i=0;
                do
                {
                                cout<<i<<endl;
                                i++;
                }
                while(i<=9);
}

Output:


0
1
2
3
4
5
6
7
8
9
Hop you understand the use of do while loop in c++.


Click here for watching the video of that code


No Comment

How we can use nested for loop in c++ and make tables in c++ code


It is easy to use nested for loop in c++. First, we use first for loop and then use another loop within the first loop. You watch the code to understand the right way of the nested for loop.

Code:


#include <iostream>
#include <string>
using namespace std;
int main() {
    for(int i=1;i<=10;i++)
    {
                for(int j=1,ans;j<=10;j++)
                {
                                ans=i*j;
                                cout<<i<<"*"<<j<<"="<<ans<<endl;
                                }
                                cout<<endl;
                }
}

Output:


1*1=1
1*2=2
1*3=3
1*4=4
1*5=5
1*6=6
1*7=7
1*8=8
1*9=9
1*10=10

2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20

3*1=3
3*2=6
3*3=9
3*4=12
3*5=15
3*6=18
3*7=21
3*8=24
3*9=27
3*10=30

4*1=4
4*2=8
4*3=12
4*4=16
4*5=20
4*6=24
4*7=28
4*8=32
4*9=36
4*10=40

5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50

6*1=6
6*2=12
6*3=18
6*4=24
6*5=30
6*6=36
6*7=42
6*8=48
6*9=54
6*10=60

7*1=7
7*2=14
7*3=21
7*4=28
7*5=35
7*6=42
7*7=49
7*8=56
7*9=63
7*10=70

8*1=8
8*2=16
8*3=24
8*4=32
8*5=40
8*6=48
8*7=56
8*8=64
8*9=72
8*10=80

9*1=9
9*2=18
9*3=27
9*4=36
9*5=45
9*6=54
9*7=63
9*8=72
9*9=81
9*10=90

10*1=10
10*2=20
10*3=30
10*4=40
10*5=50
10*6=60
10*7=70
10*8=80
10*9=90
10*10=100


Hope you understand the use of nested for loop in c++.


Click here for watching the video of that code

No Comment

How we can use simple for loop in c++ code


For loop is also use for repetition. In for loop we initialize the object, use condition and increment operator in one small brackets with the keyword for. You can see the code and understand the use of for loop in right way.

Code:


#include <iostream>
using namespace std;
int main()
{
                int i;
                for(i=0;i<=20;i++)
                {
                                cout<<i<<endl;
                }
               
}

Output:


0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Hope you understand the use of for loop in c++.


Click here for watching the video of that code