Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

Thursday, December 6, 2012

A classic difference between C & C++

In C++ it is strictly enforced that all functions must be declared before they are used. But in C this isn't necessary.

This code is valid C, but it is not valid C++.

#include <stdio.h>
int main()
{
    test();
    return 0;
}

int test()
{
    printf( "Hello C" );
}



     * invalid code *                * Valid code *                  * valid code *

#include <iostream>           #include <iostream>           #include <iostream>
using namespace std;          using namespace std;         using namespace std;
                                                                          
int main()                          int test();                          int test()
{                                                                            {
    test();                           int main()                          printf("hello cpp");
}                                      {                                     }
                                           test();                    
int test()                           }                                     int main()
{                                                                            {
    printf("Hello cpp");           int test()                            test();
}                                      {                                     }
                                           printf("hello CPP");   
                                        }                              

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

Wednesday, October 17, 2012

Program to understand STATIC variable

Static is multipurpose keyword, we can use static in context of variable, function and classes.
Here we discuss about the static variable.
__________________________________________________________________
//In this program we are not using static variable 
 #include<stdio.h>
#include<conio.h>

void show()
{
    int i=0;          //Note, we are not declaring 'i' as a static variable here
    i++;               //variable 'i' increased by 1


    printf("output is %d\n" ,i);
}

void main()
{
    show();            //here we are calling show 3 times
    show();
    show();
}


output:
       output is 1
       output is 1

       output is 1

You can see that  output is 1, although we are calling show function 3 time.
That because the variable i not retaining the value.

  •  So to retain the value we use this variable as a static variable. (see next program)
  •  Static variable is useful when we want to count the no of attempt performed by the client. (see last program)
__________________________________________________________________
//using STATIC variable
#include<stdio.h>
#include<conio.h>

int show()
{
    static int i=0;            //here we use static variable
             i++;
             printf("output is %d\n" ,i);
}

void main()
{
    show();
    show();
    show();
}

output:
          output is 1
          output is 2
          output is 3

__________________________________________________________________
//In this program we want that user have only 4 chance to enter right password
#include<iostream>
#include<string>
#include<stdlib.h>              //for exit(0);
using namespace std;

int show()
{
     static int i=0;            //here we use static variable
           // int i=0;


    
string password;           
     i++;
                      
            if(i<=3)
            {
                cout << "\nenter your password: ";
                cin >> password;
               
                if (password == "foobar")
                    {
                        cout << "\nwelcome you log in!\n";
                        exit(0);
                    }
            }
  
            else
            {
                cout << "recover your password";
            }
           
}

int main()
{
    show();              //here we can use the loop also
    show();
    show();
    show();
}

Saturday, September 1, 2012

Polymorphism in c


operator overloading--  c support;                  c++ also support
function  overloading--  c doesn't support;       c++ support

c support operator_overloading but not Function_overloading
c++ support operator_overloading as well as Function_overloading

What is operator overloading:
                               operator overloading is a specific case of polymorphism, where  operators  have different implementations depending on their arguments. 
(The whole idea in operator overloading is 'same operator' and 'different type argument')

what is Function overloading: 
                            Function overloading or method overloading is a feature in programming language that allows creating several methods with the same name which differ from each other in the type of the input and the output of the function. 
(The whole idea in function overloading is 'same function name' and 'different type argument')


As we known 'int' and 'float' are different datatype, instead of using different operator to add 'int' and 'float'; we use a common operator (+), this is known as operator overloading.


Program to shows, C support operator overloading
program to shows, cpp support operator overloading

____________________________________________________________________________
Program to show, c doesn't support Function overloading 
#include <stdio.h>
#include <conio.h>

// volume of a cube
int volume(int s)                         //Name of this function is 'volume'
{
    return(s*s*s);
}

// volume of a cylinder
double volume(double r, int h)      
//Name of this function is also 'volume' 
{
    return(3.14*r*r*h);
}

// volume of a cuboid
long volume(long l, int b, int h)     //Name of this function is also 'volume'
{
    return(l*b*h);
}

int main()
{
    cout << volume(10)        << endl;
    cout << volume(2.5,8)     << endl;
    cout << volume(10,5,15)  << endl;
}
  • when we compile this program it give error, because c doesn't support 'function overloading.'
  • जब c compiler 3 same name (volume) के फंक्शन देखता है तो कंफ्यूज हो जाता है। क्योकि c का compiler Function overloading (polymorphism) नहीं जनता, इसलिए तो  कहते है, c Function overloading support नहीं करती।

_________________________________________________________________________________
But c++ Function overloading support करती है
Program to show, cpp support Function overloading 
#include <iostream>
using namespace std;

// volume of a cube
int volume(int s)                         //Name of this function is 'volume'
{
    return(s*s*s);
}

// volume of a cylinder
double volume(double r, int h)      
//Name of this function is also 'volume' 
{
    return(3.14*r*r*h);
}

// volume of a cuboid
long volume(long l, int b, int h)     //Name of this function is also 'volume'
{
    return(l*b*h);
}

int main()
{
    cout << volume(10)       << endl;
    cout << volume(2.5,8)    << endl;
    cout << volume(10,5,15) << endl;
}
 

This program run and
OUTPUT IS:
1000
157
750 
_________________________________________________________________  

Tuesday, August 14, 2012

Quick notes to C programmers

instead of macros use
    • const or enum to define constants
    • inline to prevent function call overload
    • template to declare families of type and families of functions 

  • use new/delete instead of free/malloc (use delete[] for arrays) 

  • don't use void* and pointer arithmetic 

  • an explicit type conversion reveals an error of conception. 

  • avoid to use C style tables, use vectors instead. 

  • don't recode what is already available in the C++ standard library. 

  • variables can be declared anywhere: initialization can be done when variable is required. 

  • whenever a pointer cannot be zero, use a reference. 

  • when using derived class, destructors should be virtual.

Monday, August 6, 2012

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;
    }
}

Monday, June 18, 2012

Write a program to find the greatest among ten numbers

// program in c++
#include <iostream>
using namespace std;

int main()
{
    int a[10];
    int i;                //int i is used in loop
    cout << "\n This program in C++";
    cout << "\n Enter your 10 number: ";
   
    //store 10 numbers in an array
    for (i=0; i<10; i++){
        cin >> a[i];
    }
   
    //assume that a[0] is greatest
    int greatest;
    greatest = a[0];
   
    for (i=0; i<10; i++){
        if (a[i] > greatest){
            greatest = a[i];
        }
    }
   
    cout << "\n Greatest of 10 number is: " << greatest << "\n\n";
    return 0;
}

-----------------------------------------------------------------------------------------------
// program in C
#include <stdio.h>
#include <conio.h>

int main()
{
    int a[10];
    printf("\n This program in C");
    printf("\n Enter your 10 number: ");
   
    //store 10 numbers in an array
    for (int i=0; i<10; i++){               //here we declare int i in loop
        scanf ("%d",&a[i]);
    }
   
    //assume that a[0] is greatest
    int greatest;
    greatest = a[0];
   
    for (int i=0; i<10; i++){     //here we again declare int i in loop, because
        if (a[i] > greatest){       //we haven't declare like previous program
        greatest = a[i];
        }
    }
   
    printf ("\n Greatest of 10 number is: %d \n\n", greatest);
    return 0;
}


OUTPUT:

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);    
}

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");
    } 
}

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);
}

Friday, February 10, 2012

Program to find out HCF and LCM

// find out hcf and lcm
// using c


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

int main()
{
    int a, b, x, y, t, gcd, lcm;
   
    printf ( "Enter two integers\n" );
    scanf ( "%d%d", &x, &y );
   
    a = x;
    b = y;
   
    while (b != 0)
    {
        t = b;
        b = a % b;
        a = t;
    }
   
    gcd = a;
    lcm = (x*y) / gcd;
   
    printf ( "greatest common divisor of %d and %d = %d\n", x, y, gcd );
    printf ( "least common multiple of %d and %d = %d\n", x, y, lcm );
}

Program for checking palindrome number

//Program to check palindrome number
// using c


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

int main()
{
    int n, reverse = 0, temp;
    printf ( "Enter a number to check if it is a palindrome or not\n" );
    scanf ( "%d", &n );
   
    temp = n;
    while ( temp != 0 )
    {
        reverse = reverse * 10;
        reverse = reverse + temp%10;
        temp = temp/10;
    }
   
    if (n == reverse)
    {
        printf ( "%d is a palindrome number.\n", n);
    }
    else
    {
        printf ( "%d is not a palidrome number.\n", n);
    }
}