Wednesday 18 April 2012

How to implement Recursion algorithm using C programming language

//Green line and line having double back slash are comments
//* function within function Logic is one of the beneficial example for    recursion*/

#include stdio.h >     //preprocessor directive contain all predefine function                                   //codes

    main(void)    // Main start here
      {
          int index;
     
         index = 8;
      
         CountindexDown(index);//function call

      }  //Main Ends here



     CountindexDown( int count)  //Definition of function

        {
             count--;
                printf("The value of the count is %d\n",count);
             
             if (count > 0)
               CountDown(count);
            printf("Current value of count is %d\n",count);
        }

/*To verify the result or output paste the code in  codepad area and compile or run this*/

Tuesday 17 April 2012

C program to calculate percentile of student array



/*calculate percentile of student...i used predefined numbers and students
we can take value from user at run time  also using input fucntion*/



#include<stdio.h>
int main() 
 {
       //a[0]=obtain marks of first student 
      int a[]={25,60,80,70,26} ; 
      int n=5 ;
      int i,j ;
     //n=no. of student int percent; int count; 
        for( i=0;i<=n-1;i++) 
           { 
                count=0; 
                for(j=0;j<=n-1;j++)
                  { 
                      if(a[i]>a[j]) 
                        { 
                           count=count+1; 
                        } 
                  } 
                percent = (count * 100) / (n-1) ; //logic to calculate percentile

                printf( "\n the percentile of a[%d] is %d", i, percent) ;
            } 
 } //end of main



Calculate percentile of Students in a class using C++


/*calculate percentile of student...i did hardcode
we can get the value at run time using input fucntion*/


#include<iostream.h>
using namespace std;

int main()
{
 //a[0]=first candidate and so on
    int a[]={25,60,80,70,26},n=5,i,j;//n=no. of candidate
    int percent;
    int count;
       for( i=0;i<=n-1;i++)
         { 
             count=0;
             for(j=0;j<=n-1;j++)
               { 
                   if(a[i]>a[j])
                    {
                       count=count+1;
                    }
                }
                     percent = (count * 100) / (n-1); 
               cout << "\n the percentile of " << " a ["<<i<<"] " << percent ;
          }
 
} //End of main

/*OUTPUT:-
1-the percentile ofa[0]0
2-the percentile ofa[1]50 
3-the percentile ofa[2]100 
4-the percentile ofa[3]75 
5-the percentile ofa[4]25
*/

Friday 13 April 2012

C++ program to print decimal value of Keys appear in Keyboard


//Print decimal value of buttons appear in keyboard

#include<iostream.h> 



using namespace std;

 int main()
 { 
       int a; 
       char b; 
         for(a=0;a<=255;a++)
            { 
               b=(char)a;
               cout<<"\nASCII value of character=> "<<b<<" "<<a;
            } 
     return 0; 
}
/*:-output wiil be like:-
ASCII value of character   : 32
ASCII value of character '!' : 33 
ASCII value of character' " '   : 34 
ASCII value of character '#'  : 35 
ASCII value of character '$'  : 36 
ASCII value of character '%'  : 37 
*/


C program to print ASC|| value of character of keyboard


//C program to print ASC|| value of character of keyboard

#include<stdio.h


int main()
       int a; 
       for(a=0;a<=255;a++) 
          {
            printf("ASCII value of character %c: %d\n",a,a); 
          }
   return 0; 
}
/* OUTPUT */

C program to Concatnate two string or addition of two string

//Green lines shows they are commented
#include<stdio.h>  //header files
#include<string.h> //Header files
 
void main()  //starting of main
{
   char str1[100], str2[100];
 
   printf("Enter the first string\n");
   gets(str1);
 
   printf("Enter the second string\n");
   gets(str2);
 
   strcat(str1,str2);  //predefine function to perform concatnation
 
   printf("String obtained on concatenation is %s\n",str1);
 
} //End of main

Tuesday 3 April 2012

C program to find Factorial of a positive number

/*Green color shows that its a comment*/
/*Programme to find factorial of a number*/

#include<stdio.h>

int main()
{
          int i , facto=1,num;
        printf("Enter your number\n");
        scanf("%d",&num);
   
          for(i=1;i<=num;i++)//logic to find factorial
          {
              facto=facto*i;
          }

        printf("Factorial of %d is=>%d",num,facto);  //Print output

} //End of main program

Saturday 24 March 2012

Swap two string in Python using third string


-def InputString():
    Str1=raw_input("Enter your first string")
    Str2=raw_input("Enter your second string")
    print("Befor swaping")
    print'string 1 is=>',Str1
    print'string 2is=>',Str2

    Temp=Str1
    Str1=Str2
    Str2=Temp
    print("\nAfter swapping")
    print 'string 1 is=>',Str1
    print 'string 2is=>',Str2

-def main():
    InputString()

main()

Swap two string in C using third variable


/*Swap two string using third string*/
//swap one by one character using while loop



#include<stdio.h>
int main()
{
  int i=0,j=0;
  char String1[20],String2[20],Temp[20];
  puts("Enter first string");//can use "printf" also
  gets( String1 );//for read the input from user
  puts("Enter second string");
  gets( String2 );
  printf("Before swaping the strings are\n");
  puts( String1);
  puts( String2);
     while( String1 [i]!='\0')//loop till null character
      {
        Temp[j++]= String1[i++];
      } 

Wednesday 21 March 2012

Reverse of any string using C language


/*Print Reverse of a string*/

#include<stdio.h>
int main()
{
    char string[]="hi i am Nilesh";
    char RevString[50];
    int i=-1,j=0;
   
      while(str[++i]!='\0');//logic to print reverse strin
        { //we can use "strrev" function also
         while(i>=0)
           {
            rev[j++] = str[--i];
           }
       rev[j]='\0';
      }
    printf("Reverse of string is : %s",rev);
  
    return 0;

} //End of main

C program to print Pascal Triangle programme


/* Pascal triangle in c*/

#include<stdio.h>
int main()
 {
    int line,a,b,c;
    printf("Enter the no. of lines: ");
    scanf("%d",&line);
    for(a=1;a<=line;a++)
       {
           for(b=1;b<=line-a;b++)
           printf(" ");
          for(c=1;c<a;c++)
          printf( "%d" ,c);
         for(c=a;c>=1;c--)
       

Tuesday 20 March 2012

C++ Program to subtract two numbers without using minus (' - ') operator


/*logic to add two numbers in c++ without using plus operator*/
//for positive output use if condition this code will show negative output 

#include<stdio.h>
using namespace std;
int main()
 {
       int a=25,j=200,i;
      for(i=0;i<j;i++)
           {
             a--;
            }
      cout<<a<<endl;
 }
/* output will=> -175 */

C Program to subtract two numbers without using Minus (' - ') operator


/*perform subtraction of two integer without using Minus (" -- ") operator*/
//for positive output use if condition this code will show negative output


#include<stdio.h>


void main()
 {
          int a=10,b=20;
          while(b--) //start decrement b and also decrese a also
          {
            a-- ;
          }     
    printf("Sum of two number is=>%d",a); 
 } 

C++ Program to add two numbers without using plus('+') operator


/*logic to add two numbers in c++ without using plus operator*/
#include<stdio.h>
using namespace std;
int main()
 {
       int a=25,j=200,i;
      for(i=0;i<j;i++)
           {
             a++;
            }
      cout<<a<<endl;
 }

C Program to add two numbers without using plus('+') operator


/*perform addtion of two integer without using plus ("+") sign*/
#include<stdio.h>
void main()
 {
          int a=10,b=20;
          while(b--)//decrease value of b one by one and increase the value of a
          {
            a++ ;
          }    
    printf("Sum of two number is=>%d",a);
 }

Thursday 15 March 2012

C Program to find Length of Link List


/*this programme create three nodes in link list and print the lenght of 
  link list*/
#include<stdio.h>
#include<stdlib.h>

struct node
   {
    int data;
    struct node* next;
   };

struct node* CreateLinkList()
   {
    struct node* head = NULL;
    struct node* second = NULL;
    struct node* third = NULL;
    head = malloc(sizeof(struct node)); // declare 3 nodes in the heap
    second = malloc(sizeof(struct node));
    third = malloc(sizeof(struct node));
   

Programme to create a Link List in c


/*this programme create three nodes in link list then you can
  perform any further action*/
#include<stdio.h>
#include<stdlib.h>

struct node
   {
    int data;
    struct node* next;
   };

struct node* CreateLinkList()
   {
    struct node* head = NULL;
    struct node* second = NULL;
    struct node* third = NULL;
    head = malloc(sizeof(struct node)); // declare 3 nodes in the heap
    second = malloc(sizeof(struct node));
    third = malloc(sizeof(struct node));
   

C program to Calculate string length without using predefine function


/*Calculate string length without using predefine function in C language*/


#include<stdio.h>

void main()
 {
        char str[100]="I am the best",*ptr;
            ptr=str ;
         int count = 0,i;
           for(i=0;ptr[i]!='\0';i++)//can use also--while(*(ptr+count))
             {
                 count++;
              }
     
        printf("\nThe length of the string is %d.",count);

}//End of  main


:-Programming Error
carefully use ++ oprator and pointer.
loop should be run till null

:-Details of Programme
*ptr = its a pointer which is use for reference ;
count=0 count shuld initialize here with 0 because if its not then compiler assign garbage value to count and output will be diferent.


Tuesday 13 March 2012

GUI interface in python programme display window and four button


#show window and four button on that window
from Tkinter import *

root = Tk()
frame = Frame(root)
frame.pack()

bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )

redbutton = Button(frame, text="Red", fg="red",bd='4',activebackground="cyan")
redbutton.pack( side = RIGHT)

greenbutton = Button(frame, text="green",bd=4 ,fg="green",bg="violet")
greenbutton.pack( side = LEFT )

Monday 12 March 2012

Print string or other symbol without using semicolon in programme in c


//here programme to print hello world without using semicolon
//anywhere programme


#include<stdio.h>


void main()
 {
   if(printf("hello world!"))
    {
       //when it print hello world printf will return 1
       //which is true condition for if and programme 
      //do not show any error 
    }
 }//end of main

Read date of birth from user in dd-mm-yyyy format and show the day of birth in pytho


#Program to find your Day of Birth given Date of Birth
#indent and syntax problem may occur for different IDE 


-def main():
     
     d=input("Enter your date")
     m=input("Enter your month in mm format")
     y=input("Enter your year in yyyy format")
-     if( d>31 or m>12 or (y<1900 or y>=2000) ):
         print('INVALID INPUT')
     year = y-1900
     year = year/4
     year = year+y-1900
-     if m==1 or m==10:
           month = 1

Wednesday 7 March 2012

Swap two variable in c using pointer and third or temporary variable

/*Swap two variable using pointer and third variable*/
//here we use pointer to swap value of two variable
#include<stdio.h>
void main()
  {
     int a,b;
     printf("enter the value of a ");
     scanf("%d",&a);
     printf("enter the value of b");
     scanf("%d",&b);
   

File handling programme in python perform read write operation


#file handling in python
#create a new file and perform read,write operaion


str1="this is a test string" 


file=open("NewFile.txt","w")#open file in write mode
txt=file.write(str1)#this will overwrite the file
file.close()#close file after operation 


file1=open("NewFile.txt","r")#open file in read mode
txt2=file1.read()
file1.close()


print ("content of file:-")
print(txt2)

File handling programme in c read six input from user and write even and odd no. to different file



  /*even and odd no.using file handelling*/
 #include<stdio.h>
void main()
 {
    FILE *f1,*f2,*f3;
    int number,i;
    printf("contents of data file\n\n");
    f1=fopen("data","w");
    for(i=0;i<=6;i++)//enter six values 
      {
        scanf("%d",&number);//read from user
         if(number==-1)
         break;
         putw(number,f1);//write data into file 1(source,destination)
       }
 

Tuesday 6 March 2012

Python programme read three inputs from user and show whether or not they are a Pythagorean triple or right angle traingle


#Pythagoras theorem
# a*a + b*b = c*c
#Show input are pythgorean triple or not


-def PythaGorean(a,b,c):
    x=a*a+b*b
    y=c*c
    if (x==y) :
        print 'It is a right angle traingle or Pythagorean triple'
    else:
        print 'It is not a right angle traingle'


 -def ReadValues():
 

Sunday 4 March 2012

Read the radious of circle from user and gives area,diameter and circumference of circle programme in python


#read the radious of circle from user and display area,diameter and #circumference of circle
- def Radious(x):
    area=x*x*3.141
    dia=2*x
    circum=2*x*3.141
    print 'area is ',area
    print 'diameter of circle is ',dia
    print 'circumference of circle is',circum


- def main():
    rad=input("Enter the radius of the circle:-")
    Radious(rad)
main()    

Saturday 3 March 2012

Read three input from user and show largest number programme in c


/*read three inputs from user and return the largest number*/


#include<stdio.h>
void main()
{
     int a,b,c;
     printf("enter your number");
     scanf("%d%d%d",&a,&b,&c);
        if((a!=b) && (b!=c) && (a!=c))
         {
             if((a>b) && (a>c))
                {
                    printf("largest number is a->%d",a);
                }
             else
               {
                 

Read three numeric values from the user, and then print the largest of the three in python

#programme for python language
#output will be largest value


-def InputVal(x,y,z):  #logic for highest value
    print 'values are ',x,y,z
    if (x>y)and(x>z):
        print'a is largest=>',x
    elif(x<y)and(y>z):
        print 'b is largest=>',y
    else:
        print'c is largest=>',z

-def Main():
    a=input("Enter first value:")
    b=input("Enter second value:-")
    c=input("Enter third value:-")
    InputVal(a,b,c)
Main()     

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

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