Showing posts with label basicPrograms. Show all posts
Showing posts with label basicPrograms. Show all posts

Friday, December 28, 2012

Learn Function...the hardest way

These program help to understand the concept of function.

Program_1.cpp - simple add program
Program_2.cpp - same add program but using 'function'
Program_3.cpp - program to understand why function (this is question)
Program_4.cpp - Program to understand why function (here is answer)
Program_5.cpp - same program via 'Function not returning a value' (same as Program_2.cpp)(invoke_1)
Program_6.cpp - simple add program via 'Function returning a value'
Program_7.cpp - same program via 'Function not returning a value' (same as Program_5.cpp)(invoke_2)
Program_8.cpp - same program via 'Function not returning a value' (same as Program_7.cpp)(invoke_3)
Program_9.cpp - simple add program via call by value 'function returning a value'
Program_10.cpp - simple add program via call by value 'function not returning a value'
Program_11.cpp - simple add program via call by reference 'function returning a value'
Program_12.cpp - simple add program via call by reference 'function not returing a value'

==================================================================
program_1.cpp
// simple program to add 2 number
#include <iostream>
using namespace std;

int main()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

===================================================================
program_2.cpp 
//same add program but using 'function'
#include <iostream>
using namespace std;

void add()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    add();          //here we are calling (invoking) the function
}

====================================================================
Program_3.cpp
// program_3.cpp and Program_4.cpp is answer of question why function? 
#include <iostream>
using namespace std;

int main()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
   
    int a1=0;
    int b1=0;
    int c1=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a1 >> b1;
   
    c1 = a1 + b1;
   
    cout << "\n Addition of 2 number is: " << c1 << endl;
   
    int a2=0;
    int b2=0;
    int c2=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a2 >> b2;
   
    c2 = a2 + b2;
   
    cout << "\n Addition of 2 number is: " << c2 << endl;
}

==================================================================
Program_4.cpp
// program_3.cpp and Program_4.cpp is answer of question why function?
#include <iostream>
using namespace std;

void add()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    add();
    add();           //here we are calling function 3 time
    add();
}

=========================================================================
 program_5.cpp
// simple program to add 2 number using 'Function not returning a value'
#include <iostream>
using namespace std;

void add()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    add();
}
// This program is same as Program_2.cpp (in case you notice that)

====================================================================
Program_6.cpp 
// simple program to add 2 number using 'Function returning a value'
#include <iostream>
using namespace std;

int add()
{
    int a=0;
    int b=0;
    int c=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    c = a + b;
   
    return (c);
}

int main()
{
    int c;              // Did you notice this
    c = add();         // understand what is going on here
    cout << "\n Addition of 2 number is: " << c << endl;
}

=======================================================================
Program_7.cpp
// This is another way to invoking (calling) a function
// and Program_7.cpp = Program_5.cpp  (please notice the differnce in programs, but both of them doing exact same work.) (but different calling methode)
#include <iostream>
using namespace std;

void add(int a, int b)
{
    int c = 0;
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    int a=0;
    int b=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    add(a, b) ;
}

---------------------------------------------------------------------------------------------
Program_8.cpp
// Yet another ways to calling (invoke) a function
// This Program name is ; by Function not returning a value)
// and Program_8.cpp = Program_7.cpp   (both are not returning a value, but different calling methode)                      
#include <iostream>
using namespace std;

void add(int, int);         //declaration or prototype of function

int main()
{
    int a=0;
    int b=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    add(a, b);
}

void add(int a, int b)         //definition of function
{
    int c = 0;
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

==========================================================
Program_9.cpp
// simple add program via call by value 'function returning a value'
// this is equal to Program_6.cpp

#include <iostream>
using namespace std;

int add(int a, int b)    
{
    int c = 0;
    c = a + b;
  
    return (c);
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
  
    c = add(a, b);   
    cout << "\n Addition of 2 number is: " << c << endl;
}
*/

==========================================================
Program_10.cpp
// simple add program via call by value 'function not-returning a value'
// this is equal to Program_7.cpp

#include <iostream>
using namespace std;

void add(int a, int b)    
{
    int c = 0;
    c = a + b;
  
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
  
    add(a, b);   
}
*/

=================================================================

// This is a extra program to understand 'actual' and 'formal' parameter
// which associated with the 'call by value' and 'call by reference'
#include <iostream>
using namespace std;

void add(int a, int b)      //here a, b are formal parameter
{
    int c = 0;
    c = a + b;
  
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
  
    d = a-b+c;          //actual parameter
    cout << " \n substraction is(d): " << d << endl;
  
    add(a, b);     //actual parameter
}


To learn more about 'actual parameter & formal parameter' click here

========================================================================

Program_11.cpp
// simple add program via call by reference 'function returning a value'
#include <iostream>
using namespace std;

int add(int &a, int &b)   
{
    int c = 0;
    c = a + b;
  
    return (c);
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
  
    c = add(a, b);   
    cout << "\n Addition of 2 number is: " << c << endl;
}
 

===============================================================
Program_12.cpp
// simple add program via call by reference 'function not-returning a value'
#include <iostream>
using namespace std;

void add(int &a, int &b) 
  
{
    int c = 0;
    c = a + b;
  
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
  
    add(a, b);   
}

=========================================================

// why use  'Function returning a value'
#include <iostream>
using namespace std;

int divide()
{
    int a1=0;
    int b1=0;
    int c1=0;
  
    cout << "\n Please Enter 2 number for divide: ";
    cin >> a1 >> b1;
    c1 = a1 / b1;
    return (c1);
}

int add()
{
    int a=0;
    int b=0;
    int c=0;
  
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
    c = a + b;
    return (c);
}

int main()
{
    int c1;
    c1 = divide();
  

    int c;             
    c = add(); 
  

    int d;
    d = c + c1;
    cout << "\n Addition of 2 number is: " << d << endl;
}

Friday, November 9, 2012

program to understand actual parameter and formal parameter

  • Formal and actual parameter are associated with the concept of 'call by value' and 'call by reference'
  • see the effect of 'call by value' and 'call by reference'
====================================================
//this program to illustrate call_by_reference
#include <iostream>
using namespace std;

int add(int &);

int main()
{
    int a=2;
    int square;
    square = add(a);
    cout << "\n" << a;
    cout << "\n" << square;
}

int add(int &a)
{
    a=a+1;
    return(a*a);
}


OUTPUT:
3
9


====================================================== 

// this program to illustrate call_by_value
#include <iostream>
using namespace std;

int add(int);

int main()
{
    int a=2;
    int square;
    square = add(a);
    cout << "\n" << a;
    cout << "\n" << square;
}

int add(int a)
{
    a=a+1;
    return(a*a);
}


OUTPUT:
2
9 

======================================================
// This is a extra program to understand 'actual' and 'formal' parameter
// which associated with the 'call by value' and 'call by reference'

#include <iostream>
using namespace std;

void add(int a, int b)      //here a, b are formal parameter
{
    int c = 0;
    c = a + b;
   
    cout << "\n Addition of 2 number is: " << c << endl;
}

int main()
{
    int a=0;
    int b=0;
    int c=0;
    int d=0;
   
    cout << "\n Please Enter 2 number: ";
    cin >> a >> b;
   
    d = a-b+c;          //actual parameter
    cout << " \n substraction is(d): " << d << endl;
   
    add(a, b);     //actual parameter
}

Friday, November 2, 2012

program to find GCD using recursion

//also help to understand the command line argument

#include <stdio.h>
#include <stdlib.h>

int gcd(int, int);

int main(int argc, const char* argv[])       //command line argument
{
    int a, b;
    a = atoi(argv[1]);
    b = atoi(argv[2]);
   
    printf("The greatest commaon divisor of %d and %d is %d\n", a, b, gcd(a,b));
    return 0;
}

int gcd(int a, int b)
{
    if (b == 0)
        return a;                   //recursion implementation
    else
        return gcd(b, a % b);

}


 

Thursday, November 1, 2012

A simple program to understand recursion

#include <stdio.h>
#include <conio.h>

void recTest(int i)
{
    if(i > 0)
    {
        recTest(i - 1);          //understand this crazy little insane item
        printf("%i\n", i);
    }
}

int main()
{
    recTest(5);
}


OUTPUT:
1
2
3
4
5

Thursday, September 6, 2012

Program to check ARMSTRONG number in c++

// program to check the armstrong number

#include <iostream>
using namespace std;

int main()
{
   int num, sum = 0, temp, remainder;

   cout << "Enter a number to check: " << endl;     
   cin >> num;

   temp = num;

   while( temp != 0 )
   {
      remainder = temp%10;
      sum = sum + remainder*remainder*remainder;
      temp = temp/10;

   }

   if ( num == sum )
      cout << "Your Number is an armstrong number." << endl;
   else
      cout << "Your Number is not an armstrong number." << endl;      
}

___________________________________________________________________________________

//program to generate armstrong number

#include <iostream>
using namespace std;

int main()
{
   int r;
   long long number = 0, c, sum = 0, temp;

   cout << "Enter the maximum number upto which you want to find armstrong numbers ";
   cin >> number;

   cout << "Following armstrong numbers are found from 1 to " << number << endl;

   for( c = 1 ; c <= number ; c++ )
   {
      temp = c;
      while( temp != 0 )
      {
         r = temp%10;
         sum = sum + r*r*r;
         temp = temp/10;
      }


      if ( c == sum )
         cout << c << endl;
      sum = 0;
   }
}

Monday, August 6, 2012

Give a one-line C expression to test whether a number is a power of 2

Ans. //( (x > 0) && ((x & (x - 1)) == 0) )

#include <iostream>
using namespace std;

int main()
{
    int x;
    cout << "enter the no of : ";
    cin >> x;
   
    if( (x > 0) && ((x & (x - 1)) == 0) )
    {
        cout <<"no is power of 2";
    }
   
    else
    {
        cout <<"no is not a power of 2";
    }
   
}

Program to find whether the number is power of 2 or not?

//without  using  functions.

#include <stdio.h>
#include <conio.h>

int main()
{
    int num, rem, flag=0;
    printf ("\n Enter a Number: ");
    scanf ("%d", &num);
   
    while(num > 2)
    {
        rem = num % 2;
        if( rem == 1 )
        {
            flag = 1;
            break;
        }
        else
        num = num/2;
    }
   
    if( flag == 1 )
    {
        printf("\n Number is not Power of two.");
    }
    else
    {
        printf("\n Number is Power of two");
    }
}

Wednesday, July 11, 2012

A program to print febonacci series upto first 20 trems

#include <stdio.h>
#include <conio.h>

int main()
{
    int a = 0;
    int b = 1;
    int i,sum;
    
    for(i = 0;i<20;i++)
    {
        sum = a+b;
        printf("%d\n",sum);
         
        a = b;
        b = sum;
    }
}

A program to print the FIBONACCI series


#include<iostream>
using namespace std;

main()
{
   int n, c, first = 0, second = 1, next;

   cout << "Enter the number of terms of Fibonacci series you want" << endl;
   cin >> n;

   cout << "First " << n << " terms of Fibonacci series are :- " << endl;

   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next = c;


      else
      {
         next = first + second;
         first = second;
         second = next;
      }
      cout << next << endl;
   }
}

Wednesday, June 20, 2012

Program to understand Do While loop

//The body of the loop will be executed at least once before the condition is checked
#include<iostream>
#include<string>
using namespace std;

int main()
{
    string password;
   
    do
    {
        cout << "Enter your password: ";
        cin >> password;
    }
   
    while (password != "foobar");         //notice semicolon
    cout << "welcome, you enterted right password";
}



Wednesday, June 13, 2012

Factorial of a number using class in cpp

//This program is compiled using GCC

#include<iostream>
using namespace std;

class factorial
{
    int num;
   
    public:
        void getinfo()                 //1st member function of class factorial
        {
            cout << "\n Enter the number for which the factorial is to be found: ";
            cin >> num;
        }
       
        void facto()          
//2nd member function of class factorial        
        {
            int i, fact=1;
           
            if(num == 0 || num == 1)
            {
                cout << "\n factorial of "<< num << " is 1";
            }
           
            else
            {
                for(i=1; i<=num; i++)
                {
                    fact = fact*i;
                }
                cout << "\n factorial of " << num << " is: " << fact;
            }
        }
};

int main()
{
    factorial f1;                //creating the object of class 'factorial'
    f1.getinfo();
    f1.facto();
   
    cin.ignore();      //these 2 fun are use
    cin.get();          //for holding the output screen
}

Tuesday, June 12, 2012

Concept of global and global declaration

//program to illustrate the concept of global variable
#include <iostream>
using namespace std;

int myint;       //this is the declaration of global variable

void another_function()
{
    myint = 12345;
}

int main()
{

    cout << myint << endl; 

    myint = 10;
    cout << myint << endl;
 

    another_function();
    cout << myint << endl;
}


output:
0                         //because cpp compiler define global variable to 0; if they r not declared
10
12345 
_______________________________________________________________
//program to illustrate global declaration concept
#include <iostream>
using namespace std;

struct int_holder
{
    int my_int1;
    int my_int2;
    double my_double;
};

int main()
{
    int_holder holder;
    cout << holder.my_int1 << endl;
    cout << holder.my_int2 << endl;
    cout << holder.my_double << endl;


output: 
200044456             //these all 3 are garbage value assign by compiler
-12324924
1.4345345  
_________________________________________________________________
//program to illustrate global declaration concept
#include <iostream>
using namespace std;

struct int_holder
{
    int my_int1;
    int my_int2;
    double my_double;
};


int_holder holder;           

int main()

    cout << holder.my_int1 << endl;
    cout << holder.my_int2 << endl;
    cout << holder.my_double << endl;
 


output: 
0                    //value 0 is assign by compiler because declaration is global
0
0 
----------------------------------------------------------------------------------------------

 //More example to understand concept of Global variable
#include <iostream>
using namespace std;

int example(int x)
{
    x = x+3;
    return x;
}

int main()
{
    int x = 5;
    cout << example(x) << "\t" << x;
}



#include <iostream>
using namespace std;

 int x = 6;   //this global variable now available to every function

int example()
{
    x = x+3;
    return x;
}

int main()
{
    cout << example() << "\t" << x;

 

Monday, May 28, 2012

A program To find out smallest number in an array

#include <stdio.h>
#include <conio.h>

int main()
{
    int n, i, min;

    printf ("\nEnter Total Numbers Of Numbers In The Array: ");
    scanf ("%d",&n);

    int array[n];

    for(i=0; i<n; i++)
    {
        printf ("\nEnter %d Number: ",i+1);
        scanf ("%d",&array[i]);
    }

    min = array[0];

    for(i=0; i<n; i++)
    {
        if(array[i] < min)

        min = array[i];
    }

    printf("\nThe Smallest Number in the array is %d \n",min);    
}

Friday, May 18, 2012

A program to find largest number in an given array

#include <stdio.h>
#include <conio.h>

int main()
{
    int n, i, max;

    printf ("Enter Total Numbers Of Numbers In The Array: ");
    scanf ("%d",&n);

    int array[n];
   
    for( i=0; i<n; i++)
    {
        printf ("\nEnter %d Number: ",i+1);
        scanf ("%d",&array[i]);
    }

    max = array[0];

    for(i=0;i<n;i++)
    {
        if(array[i] > max)

        max = array[i];
    }
    printf ("\nThe Largest Number in the array is %d ",max);    
}

Monday, May 14, 2012

'c++ struct' can have private_members and function also

//A Simple program to show c++ structure can have function
// using c++, GCC complier
 
#include <iostream>
using namespace std;

struct CRectangle       //here we define a user-defined-datatype using 'struct'
{
    private:
        int x, y;


    public:
        void set_values (int,int);


        int area()
        {
            return (x*y);
        }
};

void CRectangle :: set_values (int a, int b)
{
    x = a;
    y = b;
}

int main()
{
    CRectangle rect;    //creating the object (rect) of a class (CRectangle)


    rect.set_values (3,4);
    cout << "area:" << rect.area();
   
    cin.ignore();
    cin.get();
}

______________________________________________________________
also see the same program using class (only difference, we use 'class' instead of 'struct')

Wednesday, May 9, 2012

A program to print multiplication table from 1 to 10

#include <stdio.h>
#include <conio.h>

int main()
{
    int i,j;

    int array[10][10];
    
    for(i=0; i<10; i++)
    {
        for(j=0; j<10; j++)
        {
            array[i][j] = (i+1)*(j+1);
        }               
    }
    
    for(i=0; i<10; i++)
    {
        for(j=0; j<10; j++)
        {   
            printf("%3d ",array[i][j]);
        }
        printf("\n");
    } 
}

Friday, April 20, 2012

A simple program to understand classes in C#

using System;

class building
{
    public int floors;   //number of floors         //C# class doesn't support 'public :'
    public int area;     //total square footage of building
    public int occupants;  //number of occupants
}

class building_Demo
{
    public static void Main()
    {
        building house = new building();  //create a bbuilding object
        int areaPP;    //area per person
      
        //assign values to fields to fields in house
        house.occupants = 4;
        house.area = 2500;
        house.floors = 2;
  
    //compute the area per person
    areaPP = house.area/house.occupants;
  
    Console.WriteLine("house has:\n "+
                       house.floors + " floors\n "+
                       house.occupants + " occupants\n "+
                       house.area + " total area\n "+
                       areaPP + " area per person");
    }
}


OUTPUT:
 house has:
  2  floors
  4  occupants
  2500  total area
  625 area per person

Thursday, April 5, 2012

A simple 'class' program

//class with single object
// using c++

#include <iostream>
using namespace std;

class CRectangle 
//here we define a user-defined-datatype 'CRectangle' using 'class'
{
    private:
        int x, y;


    public:
        void set_values (int,int);


        int area()
        {
            return (x*y);
        }
};

void CRectangle :: set_values (int a, int b)
{
    x = a;
    y = b;
}

int main()
{
    CRectangle rect;    //creating the object (rect) of a class (CRectangle)


    rect.set_values (3,4);
    cout << "area:" << rect.area();
   
    cin.ignore();
    cin.get();
}



Note: we must use the operator scope-resulation (::) to specify that we are defining a function that is a member of the class CRectangle and not a regular global function.
 _________________________________________________________
See also the same program using struct (only difference, we use 'struct' instead of 'class') 

A simple 'struct' program

// simple struct (user defined datatype) program
// using c++


#include <iostream>
using namespace std;

struct student                 //student is user defind data type
{
    int roll_no;
    char name;
    int age;
};

int main()
{
    student a;              //a is the object of student


    cout << "\nEnter the name of student: ";
    cin >> a.name;


    cout << "\n Enter your age: ";
    cin >> a.age;


    cout << "\n enter your roll no: ";
    cin >> a.roll_no;
   
    cout << "\n student name is :" << a.name;
    cout << "\n               age is :" << a.age;
    cout << "\n               roll no :" << a.roll_no;
   
}

Thursday, March 22, 2012

Program to find factorial using recursion in c

//1st way
#include <stdio.h>
#include <conio.h>

int fact(int);

int main()
{
    int num, f;
    printf ("\n Enter a number: ");
    scanf ("%d", &num);
    f = fact(num);
    printf (" \n Factorial of %d is: %d ", num, f);
}

int fact(int n)
{
    if(n == 1)
    {
        return 1;
    }
   
    else
    {
        return (n*fact(n-1));
    }

}

____________________________________________________________________
//2nd way
#include <stdio.h>
#include <conio.h>

int getFactorial(int n)
{
    int local;
    if (n > 1)
    {
        local = n * getFactorial(n-1);
        return local;
    }

    else
        return 1;
}

int main()
{
    int num, f;
    printf ( "\n Enter a number: " );
    scanf ("%d", &num);
    f = getFactorial(num);
    printf ( "\n Factorial of %d is: %d ", num, f);
}