Wednesday 29 February 2012

Squre roots of a quadratic equation programme in python



#  ax2 + bx + c = 0. Recall that such an equation has two
#solutions for the value of x, and these are given by the formula
#          x = -b +- sqrt(b2 – 4ac) / 2a


import math


a=input("Enter the value of a")
b=input("Enter the value of b")
c=input("Enter the value of c")


d=(b*b-4*a*c)
d=math.sqrt(d) #predefine function in python


root1=(-b+d)/2*a
root2=(-b-d)/2*a


print 'first root is',root1
print '\nsecond root is',root2

Tuesday 28 February 2012

if condition programme in python


#use of if condition in python
- def inputValue(value):  
   if (value < 10):
       print('You are sweet little child')


   elif (value < 20) and (value > 10):
      print('you are teenage')


   elif (value < 30) and (value > 20):
     print('now you are mature focus on your career')


Monday 27 February 2012

Fibonacci series in pyhton

#print Fibonacci series in python
#In mathematics the Fibonacci numbers are the numbers in the following interger sequense:
#0,1,1,2,3,5,8,13,21,34,55....
#logic ==>   
F_n = F_{n-1} + F_{n-2},\!\,

//programme start from here



-  def  fib(n):     #definition of function


        val1,val2=0,1
        i=0
        print(val2)
-        while i<=n:
           val3 = val1 + val2
           val1 = val2
           val2 = val3
           i=i+1
         

Fibonacci series programme in C

/*print fibonacci series*/
//programme will run in linux change header file for turbo c

//In mathamatics, the Fibonacci numbers are the numbers in the following integer sequense:
0,\;1,\;1,\;2,\;3,\;5,\;8,\;13,\;21,\;34,\;55,\;89,\;144,\; \ldots\;
F_n = F_{n-1} + F_{n-2},\!\,

//programme start from here


#include<stdio.h

void main() 
  { 
     int Term,i,val3,val1=0,val2=1; 
     printf("Enter the value for Fibonacci series of nth term := "); 
     scanf("%d",&Term); 
     printf("%d %d ",val1,val2); 
   

Friday 24 February 2012

bubble sort algorithm programme in c


/*sorting of an array in ascending  order*/
#include<stdio.h>
#include<string.h>


void main()
{
int arr[5],i,temp;
static int j=0;
printf("enter any 5 number");


   while( j !=4)  //get the value from user at run time
    {
      scanf("%d",&arr[j]);
      printf("Enterd value is %d in index %d",arr[j],j);
    j++; 
   }


Wednesday 22 February 2012

friend function programme in C++ using two class


/*using friend function with two class*/
#include<iostream>
using namespace std;
class Two;  //define second class to avoid errors
class One  //first class
{
private:
int first;
public:
void data();
friend int num(One obj1,Two obj2)
}
inline void One :: data()
{
cout<<"enter the first number=>"<<endl;
cin>>first>>endl;
}

Tuesday 21 February 2012

simple intrest programme by using multilevel inheritence in c++



/*Count simple intrest by using multilevel inheritence in c++*/
#include<iostream>
using namespace std;
  class base
     {
       public:
         int prin,no;         //value of  principal and time
       public:
         void Data();
     };
   void base::data()
      {
      cout<<"-------------------------------------------------------\n";
       cout<<"enter the value of principal and time=>";
       cin>>p>>n;
     }

Convert Celsius temp to Fahrenheit in Python


# convert.py (its file name you can use anything)
# A program to convert Celsius temps to Fahrenheit
#write this programe in python IDE(integrated development enviroment ) [Green color for commenting]


def main():
Celsius = input("What is the Celsius temperature? ")
      #logic or formula to for converting temparature
Fahrenheit = (9.0 / 5.0) * celsius + 32  
print "The temperature is", Fahrenheit, "degrees Fahrenheit."
main():

Example of Class in c++



/*class in c++*/


#include<iostream>


using namespace std;
    class book
    {
      private:   //private function member cnt use by other class
          int bookno;
         char name[];
      public:   //public function member use by other class
         void GetData()
           {
              cout<<"enter the detail"<<endl;
              cout<<"enter book no."<<endl;
             

Monday 20 February 2012

Reverse string program in c language


/*print reverse string*/

#include<stdio.h>   //Header file
#include<string.h>

void  main()
    {
       char name[20];  //initialize variable
       int len,a;
      printf("enter your name");
     scanf("%s",&name);     //Get string in run time
    printf("your name is = %s",name);
    printf("\n.......................\n");
   len=strlen(name);   //find the length of stirng
   a=len;
  printf("reverse of your name is=");
   for(len=a;len>=0;len--)  //logic to print reverse stringa
     {
        printf("%c",name[len]);
     }  //End of for loop
}//End of main 

Harmonic Series programme in c


/*Print harmonic series*/

#include<stdio.h> //header file

void main()
{
       int Val,i;         //initialize variable
      float sum,var;
       sum=0.0;
        var=0.0;
       printf("Enter number of terms for harmonic series :");
       scanf("%d",&Val);        //get the value in run time
  for(i=1;i<=Val;i++) //loop for printing harmonic series and logic
     

Leap year programme in c language



/*Leap year function*/


#include<stdio.h>   //header files


void  main()
   {
      int Year,Var ;                    //initialize variable
      printf("enter the year");  
      scanf("%d",&Year);


 Var =Year%4  || Year %100 || Year%400; //logic for leap year


        //condition is true then enter otherwise goes into else part 
       

Saturday 18 February 2012

Print Number input by user is a prime or not using C language


/*Value is prime number or nor*/

#include<stdio.h>  //include header file

void main()
{
    int a,p;
    printf("enter the number");
    scanf("%d",&p);
     a=2;
     while(a<=p-1)
        {
          if(p%a==0)
           {
           

Transpose of 2x2 matrix program in C


/*Transpose of  2x2 matrix using C language this program written and compile with C compiler*/

#include<stdio.h>

void main()
{
    int a[2][2],i,j;     //initialize variable and array

    for(i=0;i<=1;i++)        //loop to store value  in array
      {
         
           for(j=0;j<=1;j++)
             {  
                printf("enter the value of a matrix=");
                scanf("%d",&a[i][j]);
             }
      }
    for(i=0;i<=1;i++)          //loop to print matrix value
       {
         for(j=0;j<=1;j++)
        {
           printf("\t%d",a[i][j]);
        }
      printf(" \t \n  ");
  }
}//End of main


How to multiplies two multidimensional array in C



/*multiplication of two arrays*/


#include<stdio.h>                          // include Header file


void main()
{
      int a[2][2],b[2][2],c[2][2],i,j;//initialize multidimensional array and vriable
     for(i=0;i<=1;i++)                       //loop to store value into the first array
      {
        for(j=0;j<=1;j++)
         {
             printf("enter the value of mat a=");
             scanf("%d",&a[i][j]);
         }
      }
   

How to add two multi dimensional array in c



/*Addition of two arrays*/


#include<stdio.h>           //header files


void main()                    //programme starts from here
{
  int a[2][2],b[2][2],c[2][2],i,j;   //initialize all variable
  for(i=0;i<=1;i++)    //for loop to store the value in two dimensional array
    {
      for(j=0;j<=1;j++)
       {
        printf("enter the values of a array= ");  
        scanf("%d",&a[i][j]);                            //store value in run time
       }
    }

Friday 17 February 2012

How to find Length of string in c using pre-define function

/*using predefine function*/
#include<stdio.h>
#include<stdlib.h>


void main()
{
     char string[100];  //varible string type
     int len;                  //varible integer type to store value of string length


     strcpy(string,"I am best");  //predefine function use to copy string to varible
     len=strlen(string);                //length of string store in variable
     printf("my string is %d",len);   //print length of string


}

Swap two variable values using C++ programming language without using third variable or temporary variable



/*Without using any third varible*/

#include<iostream.h>

 Using namespace std

    void main()
      {
             int a,b ;
             a=1 ;
             b=2 ;
    
          SwapInC++(a , b) ;  //swap function call
     
          Cout<<"values after swap a= "<< a <<endl ;
    
          Cout<<"values after swap b= "<< b <<endl ;

       }  //End of main

    void SwapInC++(int x,int y)  //definition of Swap functon
    {
          y= x-y+(x=y);
     }

Common Programming Error:-
C++ compiler read from left to right to do not Confound with swap logic.

Good Programming Practice:-
always use equation like here i used instead using another temporary storage.

Thursday 16 February 2012

Swap two variable value using C programming without using third variable or temporary variable


/* Without using any third varible*/

#include<stdio.h>

  void SwapInCv(int x , int y) ;  //function reference

  void main()
     {
             int a,b ;
             a=1 ;
             b=2 ;
         
           SwapInC(a,b) ;  //swap function call
  
             printf("values after swaping a=%d & b=%d ", a , b) ;
     }  //End of main

   void SwapInC(int x,int y)  //definition of Swap functon
    {
       y= x-y+(x=y);
    }

Common Programming Error:-
C compiler read from left to right to do not confound with swap or switch logic.

Good Programming Practice:-
always use equation like  i used instead using another temporary storage.

Tuesday 14 February 2012

How to generate Milli second digital delay using C programming


/* Milli  second digital delay (Input value assign by the user)*/

void DelayMS(int delayInMs)   //value of delayInMs=1,2,3,.......
   {
  usigned long i,j;
for(i=0;i<delayInMs;i++)

for(j=0;j<6024;j++);

    }  //End of function

void main()
{
  DelayMs(500);//generate delay of 500 ms
}

Common Programming Errors:-
Sometime programmer started with main and give the definition of function after that. its shows error to avoid that you can write only function name before main and define after main.

Good Programming Practice:-
Use uppercase letter for function name and you can use underscore( _ ) sign also.

A digital delay function in C Programming


/*Generate a long  digital delay*/

void longDelay()            //function name
{
int i, j;                           //define integer variables

for (i = 1; i < 5; i++)
{
          for (j = 1; j < 5500; j++) ; //6000
           }
   

}//end of function

Compiler for C,C++ and Python {paste your programme to see the output}