Pages

Visitors

Showing posts with label CP LAB. Show all posts
Showing posts with label CP LAB. Show all posts

Thursday, 5 January 2012

Binary Search


#include <stdio.h>

main()

{
    int a[30],n,i,t,low,mid,high,found = 0;

    printf("\n enter the number of elements in array");

    scanf("%d",&n);

    if(n>30)

    {
        printf("\n too many numbers");
        exit(0);
    }

printf("\n enter elements of array: ");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("\n enter element to search: ");

scanf("%d",&t);

low = 0;

high = n-1;

while (high >= low)

{
    mid = (low+high)/2;

    if (a[mid] == t)
    {
        found = 1; break;
    }

    else if (t<a[mid])

             high = mid-1;

             else low = mid + 1;
}

if (found ==0)

printf("\n not found");

else printf("\n found at %d",mid);

getch();
}

Bubble Sort


#include <stdio.h>

main()

{
     int a[30],n,i,j,temp;

     printf("\n how many numbers");

     scanf("%d",&n);

     if (n>30)

     {
         printf("\n too many numbers");


     }

     printf("\n enter the array elements: ");

     for (i=0;i<n;i++)
     scanf("%d",&a[i]);

     for (i=0;i<n-1;i++)

     for(j=0;j<(n-i)-1;j++)

     if (a[j]>a[j+1])

     {
         temp = a[j];

         a[j] = a[j+1];
         a[j+1] = temp;
     }

     printf("The numbers in sorted order are : \n");

     for(i=0;i<n;++i)

     printf("\n%d",a[i]);

     getch();
}

string compare without strcmp()


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

main()

{
    char s1[30],s2[20];

   int i,length = 0,temp;

   printf("enter string 1: ");
   gets(s1);

   printf("\n enter string 2: ");

   gets(s2);

   for(i=0;s1[i]!='\0';i++)

   {
       if(s1[i]== s2[i])

       temp = 1; else temp = 0;
   }

   if (temp == 1)

   printf("both the strings are same");

   else
   printf("both strings are not same.");
    getch();
}

string compare with strcmp()


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

main()

{
    char s1[20],s2[20];

    int result;

    printf("\n enter first string: ");

    gets(s1);

    printf("\n enter second string: ");

    gets(s2);

    result = strcmp(s1,s2);

    if(result == 0)

    printf("\n both strings are equal");

    else
    printf("\n both strings are not equal");

    getch();
}

String concatenation without strcat()


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

main()

{
    char s1[30],s2[20];

   int i,length = 0,temp;

   printf("enter string 1: ");
   gets(s1);

   printf("\n enter string 2: ");

   gets(s2);

   for(i=0;s1[i]!='\0';i++)

   length++;

   temp = length;

   for (i=0; s2[i]!= '\0';i++)

   {
       s1[temp] = s2[i];

       temp++;
   }

   s1[temp] = '\0';

   printf("\nThe concatenated string is    \n");

   puts(s1);
    getch();
}

string concatenation by strcat()


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

main()

{
    char s1[20],s2[20];

    printf("\n enter first string");

    gets(s1);

    printf("\n enter second string");

    gets(s2);

    strcat(s1,s2);

    printf("\n The concatenated string is %s",s1);

    getch();
}

fibonacci by recursion


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

int fib(int ,int , int , int);

main()

{
    int n,f,x=0,y=1,i=3;

    printf("\n enter length of series : ");

    scanf("%d",&n);

    printf("\n%d\t%d",x,y);

    fib(x,y,n,i);

    getch();
}


fib(int x, int y, int n, int i)

{
    int z;
    if (i<=n)

    {
        z = x+y; printf("\t%d",z);

        x = y;

        y =z;

        i++;

        fib(x,y,n,i);

    }
}

Call by reference swap



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

void swap(int *x,int *y)

{
     int temp;
     temp = *x;
     *x=*y;
     *y=temp;
     printf("swapped values are a= %d and b = %d",*x,*y);
}

void main()

{
     int a = 7, b = 4;

     swap(&a,&b);

     printf("Original values are a= %d and b = %d",a,b);

     printf("The values after swap are a = %d,b = %d",a,b);


     getch();
}


factorial by recursion


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

long fact(int);

main()

{
    int n;

    long f;

    printf("\n enter a number");
    scanf("%d",&n);
    f = fact(n);
    printf("\n Factorial of %d is %ld",n,f);

    getch();
}


long fact(int n)

{
    int m;

    if (n==1)

    return n;

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

Call By Value Swapping


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

void swap(int x,int y)

{
     int temp;
     temp = x;
     x=y;
     y=temp;
     printf("swapped values are a= %d and b = %d",x,y);
}

void main()

{
     int a = 7, b = 4;

     swap(a,b);

     printf("Original values are a= %d and b = %d",a,b);

     printf("The values after swap are a = %d,b = %d",a,b);


     getch();
}

Monday, 26 December 2011

How to Download and Set-up Code::Blocks

Code::Blocks is a C/C++ compiler:


follow these simple steps to download,install and run code::blocks.

Open the Link given below to download The setup file

CODE::BLOCKS

It's a Reasonably Big file so Please be patient and wait till the download completes.

Then simply open the executable file, if any prompt is displayed, just click OK, code::blocks is a trusted a publisher.

Then click Next-->I Agree--->

in type of install select full from the drop down menu.

chose a suitable location to install Code::blocks

Wait for the installation to complete.

Then open the Code::blocks shortcut on your desktop

go to file menu, select new option and select file.. option from the sub menu.










Then choose c/c++ source file from the pop up window that opens. then Start coding.

you will find out how useful it is once you start coding :p.

Good luck

Distance Calculator : Choose Time interval


To WAP To calculate the Displacement of a particle at a time t in time intervals input by the user along with initial velocity and acceleration.


CODE:

#include <stdio.h>

void main()

{
     float s,u,t,a,n,max;

     printf("Enter values of u and a and max time: ");

     scanf("%f%f%f",&u,&a,&max);

     printf("enter interval : \n");

     scanf("%f",&n);


     printf("\nTime          Distance\n");

     for(t=0;t<=max;t=t+n)

     {
        s = u*t+0.5*a*t*t;

        printf("%5.2f        %7.2f\n",t,s);
     }

     getch();
}

Sunday, 25 December 2011

Flow charts

Matrix Addition


Max and Min

Primes

Switch

Smallest and Largest in an array of numbers


#include <stdio.h>

void main()

{
    int a[10],n,i,min=99999,max=-99999;

    printf("enter n: ");

    scanf("%d",&n);

    printf("Enter n elements : ");

    for (i=0;i<n;i++)

    scanf("%d",&a[i]);

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

        min = a[i];
    }

    for(i=0;i<n;i++)

    {
        if(a[i]>max)

        max = a[i];
    }

    printf("smallest value is %d and largest value is %d.",min,max);

    getch();


}

Prime numbers! :D


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

void main()
{
    int num,i,j,temp;

    printf("enter range : ");

    scanf("%d",&num);

    printf("The Prime numbers between 1 and %d are : \n",num);

    printf("1\n");

    for (i=2;i<=num;i++)

    {
        temp = i;

        for (j=2;j<=temp;j++)

        {
            if(temp%j==0)

            break;

        }

        if(temp==j)

        printf("%d\n",temp);
    }

    getch();

    }

Friday, 23 December 2011

Matrix Multiplication by Arrays


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

void main()
{
   int a[30][30],b[30][30],c[30][30],i,j,m,n,p,q,k;

   printf("Enter the rows and columns of matrix A : ");

   scanf("%d%d",&m,&n);

   printf("\nEnter the rows and columns of matrix b : \n");

   scanf("%d%d",&p,&q);

   if(n==p)

   {
       printf("The matrices can be multiplied.");
       printf("\nEnter elements of A:\n");
       for (i=0;i<m;i++)
       for(j=0;j<n;j++)
       scanf("%d",&a[i][j]);

       printf("\n Enter elements of B: \n");
       for (i=0;i<p;i++)
       for(j=0;j<q;j++)
       scanf("%d",&b[i][j]);

       printf("The product of A and B is: \n");

       for(i=0;i<m;i++)
       for(j=0;j<q;j++)

       {
           c[i][j]=0;
           for (k=0;k<n;k++)
           c[i][j] = c[i][j]+a[i][k]*b[k][j];

           printf("%d\n",c[i][j]);

       }

   }

   getch();
}

Thursday, 22 December 2011

Fibonacci By Arrays


#include<stdio.h>

void main()
{
   int i,n,a[50];

   printf("enter length of the series : ");
   scanf("%d",&n);

   a[0] = 0;
   a[1] = 1;

   printf("Fibonacci Series : \n");

   printf("\n0\n1");

   for (i = 2; i<n; i++)

   {
     a[i] = a[i-1]+a[i-2];

     printf("\n%d\n",a[i]);
   }


   getch();

   }

Calculator by Switch statement


#include<stdio.h>


main()
{
   int n,a,b,c;


   printf(" enter values of two operands : ");


   scanf("%d%d",&a,&b);


   printf("Chose Operation \n1.Addition\n2.subtraction\n3.multiplication\n4.division\n");


          scanf("%d",&n);


          switch(n)


          {
              case 1 : c = a+b;
              printf("a+b is %d",c);
              break;


              case 2: c = a-b;
              printf("a-b is %d",c);
              break;


              case 3 : c = a*b;
              printf("a*b is %d",c);
              break;
              case 4:c = a/b;
              printf("a/b is %d",c);
              break;


              default : printf("wrong option");
          }


   getch();
}

Roots of a QE


Roots of a Quadratic Equation :

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

void main()

{
    float disc,x1,x2; int a,b,c;

    printf ("enter a,b,c for ax^2+bx+c = 0: ");

    scanf ("%d%d%d", &a,&b,&c);

    disc = (b*b) - 4*a*c;

    x1 = (-b + sqrt(disc))/2*a;
    x2 = (-b - sqrt(disc))/2*a;

    if (disc >=0 )
    printf("roots are %f,%f.",x1,x2);

    else
    printf ("\nroots are imaginary");
      getch();
}

1- x^2/2! +x^3/3!-.....


To Calculate The sum : 1- x^2/2! +x^3/3!-.....
#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()

{
    float sum; int x;

    printf ("Please input x : ");

    scanf ("%d", &x);

    sum = 1- pow(x,2)/1*2 + pow(x,4)/6*4 - pow(x,6)/24*30 + pow (x,8)/24*30*56 - pow (x,10)/24*30*56*90;


    printf ("\nsum is %f",sum);
      getch();
}