Showing posts with label simple. Show all posts
Showing posts with label simple. Show all posts

Friday, January 25, 2013

Understand pointer to a pointer (**)

// program illustrating the pointer to pointer (**)
#include <iostream>
using namespace std;

int main()
{
    int i = 3;
    int *j;
    int **k;
   
    j = &i;
    k = &j;
   
    cout << "\n Address of i: " << &i << endl;
    cout << "\n Address of i: " << j << endl;
    cout << "\n Address of i: " << *k << endl;
    cout << "\n Address of j: " << &j << endl;
    cout << "\n Address of j: " << k << endl;
    cout << "\n Address of k: " << &k << endl;
   
    cout << "\n value of i: " << j << endl;
    cout << "\n value of k: " << k << endl;
    cout << "\n value of i: " << i << endl;
    cout << "\n value of i: " << *(&i) << endl;
    cout << "\n value of i: " << *j << endl;
    cout << "\n value of i: " << **k << endl;
    return 0;
}

OUTPUT:
Pointer terminology:
    int a = 12;
    int *b;
    b = &a;
   
    1. b contains address of an int
    2. value at address contained in b is an int
    3. b is an int pointer
    4. b points to an int
    5. b is a pointer which points in the direction of an int

-----------------------------------------------------------------------------------------------

// Function returning pointer
#include <iostream>
using namespace std;

int *fun();  //fun declaration

int main()
{
    int *p;
    p = fun();
    cout << "\n" << p << endl;
    cout << *p << endl;
    return 0;
}

int *fun()
{
    int i = 20;
    return(&i);
}

OUTPUT:


Tuesday, June 19, 2012

Simple array program

#include <iostream>
using namespace std;

int main()
{
    int numbers[10];
    int i;
   
    for (i=0; i<10; i++)
        numbers[i] = i + 1;
       
    for (i=0; i<10; i++)
        cout << i << ": " << numbers[i] << endl;
       
    return 0;
}


OUTPUT:

Tuesday, March 20, 2012

Sum of number divisible by 7 between 100 and 200

#include <iostream>
using namespace std;

int main()
{
    int i, count = 0, sum = 0;
   
    for (i=101; i<200; i++)
    {
        if(i%7 == 0)
        {
            count = count++;
            sum = sum + i;
        }
    }
   
    cout << "\n Total no of integer div by 7 is: " << count << endl;
    cout << "\n sum of these number: " << sum << endl;
   
    return 0;
}


OUTPUT:

Thursday, March 15, 2012

program to print sum of its integer in given digit

#include <iostream>
using namespace std;

int main()
{
    int num, temp, sum=0;
    cout << "\n Enter your number: ";
    cin >> num;
   
    while(num > 0)
    {
        temp = num%10;
        num = num/10;
        sum = sum + temp;
    }
   
    cout << "\n the sum of integer in your digit is: " << sum << endl;
    return 0;
}


OUTPUT:
 

Program to real roots of quadratic equation with output

// d = b^2 - 4ac

#include <iostream>
#include <cmath>          //when we used c library in c++
using namespace std;

int main()
{
    float a, b, c, d, root1, root2;


    cout << "\n Enter the value of a, b, c: " ;
    cin >> a >> b >> c;
 

    d = b*b - 4*a*c;
    cout << "\n discriminant is: " << d ;
   
    if ( d<0 )
    {
        cout << "\n Roots are imagenary.";
    }
    else
    {
        root1 = (-b + sqrt(d)) / (2*a);
        root2 = (-b - sqrt(d)) / (2*a);
        cout << "\n root1 is: " << root1 << "\n root2 is: " << root2 << endl;
    }
    return 0;
}


OUTPUT:

Tuesday, March 13, 2012

Program to print star pattern in C++

#include <iostream>
using namespace std;

int main()
{
    int i,j;
   
    for(i=1; i<=5; i++)
    {
        cout << "\n";
       
        for(j=1; j<=i; j++)
        {
            cout << "*";
        }
    }
   
    return 0;
}


OUTPUT:
 

Thursday, February 23, 2012

using a for loop to print Ten Non-Negative Numbers

#include <iostream>
using namespace std;

int main()
{
    for (int i=0; i!=10; i=i+1)
    cout << "\n" << i;
}


OUTOUT: