Monday, September 12, 2011

find whether the given string is a palindrome or not

TO find whether the given string is a palindrome or not */
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
    char string[90];
    int i,j,len,flag=0;
    clrscr();
    printf("\nEnter the string to be checked for Palindrome:\n");
    gets(string);
    len=strlen(string);   // or for(len=0;string[len]!='\0';len++);
    for(i=0,j=len-1;i<len/2;i++,j--)
      {
         if(string[i]!=string[j]) // check whether palindrome
        flag=1;
      }
    if(flag==1)
        printf("\nString is not a Palindrome");
    else
        printf("\nString is a Palindrome");
getch();
}

Change text line upper to lower case

#include<stdio.h>
#include<ctype.h>
#include<conio.h>
main(){
    char line[80];
    int i=0;
    clrscr();
    printf("\nenter a line of text in uppercase:");
    gets(line);
     //    scanf("%[^\n]",line);
    printf("uppercase of line of text converted to lowercase is \n");
    while(line[i]!='\0'){
    printf("%c",tolower(line[i]));
    i++;}
    printf("\n");
    getch();
    }

matrix with row-wise sum

matrix with row-wise sum

#include<stdio.h>
#include<conio.h>
main()
{

    int a[5][5],m,n,i,j,sum[5];
    clrscr();
    printf("\n enter the no. of rows of the matrix:");
    scanf("%d",&m);
    printf("\n enter the no. of columns of the matrix:");
    scanf("%d",&n);
    for(i=0;i<m;i++)  /* scan the entire matrix  */
        for(j=0;j<n;j++)
        {
        printf("\n enter the  a[%d][%d]  element:",i+1,j+1);
        scanf("%d",&a[i][j]);
        }
    if(m==n)  /* check whether square matrix or not  */
        printf("\n The given matrix is a square matrix and ");
    else
        printf("\n The given matrix is not a square matrix and ");
    printf("\n The elements of the given matrix are:");

    for(i=0;i<m;i++)  /* finding row-wise sum */
    {
        sum[i]=0;
        for(j=0;j<n;j++)
              sum[i]=sum[i]+a[i][j];
     }
    for(i=0;i<m;i++)  /* print the matrix with row-sum  */
    {
    printf("\n");
        for(j=0;j<n;j++)
        printf("\t %d ",a[i][j]);
        printf("\t Row Sum=%d",sum[i]);
    }
    getch();
}

print the given matrix

print the given matrix

#include<stdio.h>
#include<conio.h>
main()
{

    int a[5][5],m,n,i,j;
    clrscr();
    printf("\n enter the no. of rows of the matrix:");
    scanf("%d",&m);
    printf("\n enter the no. of columns of the matrix:");
    scanf("%d",&n);
    for(i=0;i<m;i++)  /* scan the entire matrix  */
        for(j=0;j<n;j++)
        {
        printf("\n enter the  a[%d][%d]  element:",i+1,j+1);
        scanf("%d",&a[i][j]);
        }
    if(m==n)  /* check whether square matrix or not  */
        printf("\n The given matrix is a square matrix and ");
    else
        printf("\n The given matrix is not a square matrix and ");
    printf("\n The elements of the given matrix are:");
    for(i=0;i<m;i++)  /* print the matrix  */
    {
    printf("\n");
        for(j=0;j<n;j++)
        printf("\t %d",a[i][j]);
    }
    getch();
}

multiply two given matrix

multiply two given matrix
#include<stdio.h>
#include<conio.h>
main()
{

    int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
    clrscr();
    printf("\n enter the no. of rows the matrix A:");
    scanf("%d",&m);
    printf("\n enter the no. of columns the matrix A:");
    scanf("%d",&n);
    printf("\n enter the no. of rows the matrix B:");
    scanf("%d",&p);
    printf("\n enter the no. of columns the matrix B:");
    scanf("%d",&q); /* check for the condition of multiplication  */
    if(n==p)
    {
        printf("\n The two matrices can be multiplied ");
        for(i=0;i<m;i++)     /* input matrix A */
            for(j=0;j<n;j++)
            {   
            printf("\n enter the  a[%d][%d]  element:",i+1,j+1);
            scanf("%d",&a[i][j]);
            }
            /* enter the matric B  */
        for(i=0;i<p;i++)
            for(j=0;j<q;j++)
            {
            printf("\n enter the  b[%d][%d]  element:",i+1,j+1);
            scanf("%d",&b[i][j]);
            }
        clrscr();
                /*  printing the matric A  */
    printf("\n The elements of the given matrix A are:");
    for(i=0;i<m;i++)
    {
    printf("\n");
        for(j=0;j<n;j++)
        printf("\t %d",a[i][j]);
    }
                /* printing the matric  B  */
    printf("\n The elements of the given matrix B are:");
    for(i=0;i<p;i++)
    {
    printf("\n");
        for(j=0;j<q;j++)
        printf("\t %d",b[i][j]);
    }

    for(i=0;i<m;i++)             /* multipling the two matrices */
        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];
        }
        /* printing the multiplication of the given matrices  */
    printf("\n The resultant matrix is of order %d*%d",m,q);
    printf("\n The multiplication  of the given matrices is:");
        for(i=0;i<m;i++)
        {
        printf("\n");
            for(j=0;j<q;j++)
                printf("\t %d",c[i][j]);
        }
    }
    else
        printf("\n The two matrices can not be multiplied ");
    getch();
}

Print greatest element of given matrix

greatest element of given matrix

#include<stdio.h>
#include<conio.h>
main()
{

    int a[5][5],m,n,i,j,big;
    clrscr();
    printf("\n enter the no. of rows of the matrix:");
    scanf("%d",&m);
    printf("\n enter the no. of columns of the matrix:");
    scanf("%d",&n);
    for(i=0;i<m;i++)    /* input the elements */
        for(j=0;j<n;j++)
        {
        printf("\n enter the  A[%d][%d]  element:",i+1,j+1);
        scanf("%d",&a[i][j]);
        }
    if(m==n) /* check whether square matrix or not */
        printf("\n The given matrix is a square matrix and ");
    else
        printf("\n The given matrix is not a square matrix and ");
    printf("\n The elements of the given matrix are:");
    for(i=0;i<m;i++)  /* display the matrix  */
    {
    printf("\n");
        for(j=0;j<n;j++)
        printf("\t %d",a[i][j]);
    }
    big=a[0];  /* logic to find the greatest element */
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
            if(a[i][j]>=big)
                big=a[i][j];

    printf("\n biggest element in the matrix=%d",big); /* print the greatest element */

    getch();
}

transpose of the given matrix

transpose of the given matrix

#include<stdio.h>
#include<conio.h>
main()
{

    int a[5][5],b[5][5],m,n,i,j;
    clrscr();
    printf("\n enter the no. of rows of the matrix:");
    scanf("%d",&m);
    printf("\n enter the no. of columns of the matrix:");
    scanf("%d",&n);
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
        {
        printf("\n enter the  a[%d][%d]  element:",i+1,j+1);
        scanf("%d",&a[i][j]);
        }
            /* printing the given series */
    clrscr();
    printf("\n The given matrix is of order %d*%d",m,n);
    printf("\n The elements of the given matrix are:");
    for(i=0;i<m;i++)
    {
    printf("\n");
        for(j=0;j<n;j++)
        printf("\t %d",a[i][j]);
    }
    for(i=0;i<m;i++)     /* logic for transposing the matrix */
        for(j=0;j<n;j++)
            b[j][i]=a[i][j];
             /* printing the transpose */
    printf("\n ");
    printf("\n The transpose of the matrix is of order %d*%d",n,m);
    printf("\n The elements of the transpose of the given matrix are:");
    for(i=0;i<n;i++)
    {
    printf("\n");
        for(j=0;j<m;j++)
        printf("\t %d",b[i][j]);
    }
    getch();

}

add two given matrix, Martrix C

add two given matrix

#include<stdio.h>
#include<conio.h>
main()
{

    int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j;
    clrscr();
           
    printf("\n enter the no. of rows the matrix A:");
    scanf("%d",&m);
    printf("\n enter the no. of columns the matrix A:");
    scanf("%d",&n);
    printf("\n enter the no. of rows the matrix B:");
    scanf("%d",&p);
    printf("\n enter the no. of columns the matrix B:");
    scanf("%d",&q);
    if((m==p) && (n==q))
    {
        printf("\n The two matrices can be added ");
            /* enter the matrix A */
        for(i=0;i<m;i++)
            for(j=0;j<n;j++)
            {
            printf("\n enter the  a[%d][%d]  element:",i+1,j+1);
            scanf("%d",&a[i][j]);
            }
            /* enter the matric B  */
        for(i=0;i<p;i++)
            for(j=0;j<q;j++)
            {
            printf("\n enter the  b[%d][%d]  element:",i+1,j+1);
            scanf("%d",&b[i][j]);
            }
        clrscr();
                /*  printing the matric A  */
    printf("\n The elements of the given matrix A are:");
    for(i=0;i<m;i++)
    {
    printf("\n");
        for(j=0;j<n;j++)
        printf("\t %d",a[i][j]);
    }
                /* printing the matric  B  */
    printf("\n The elements of the given matrix B are:");
    for(i=0;i<p;i++)
    {
    printf("\n");
        for(j=0;j<q;j++)
        printf("\t %d",b[i][j]);
    }

    for(i=0;i<m;i++)             /* adding the two matrices */
        for(j=0;j<n;j++)
            c[i][j]=a[i][j]+b[i][j];
        /* printing the addition of the given matrices  */
    printf("\n The addition of the given matrices are:");
        for(i=0;i<m;i++)
        {
        printf("\n");
            for(j=0;j<n;j++)
                printf("\t %d",c[i][j]);
        }
    }
    else
        printf("\n The two matrices can not be added ");
    getch();
}

sum of digits of given number using Recursion

//sum of digits of given number using Recursion
#include<stdio.h>
#include<conio.h>
main()
{
    int a,n;
    clrscr();
    printf("\n enter any number:");
    scanf("%d",&n);
    a=sumdig(n);
    printf("\n sum of digits of the number =%d",a);
    getch();
    }

    sumdig(int num)
    {
    static int sum;
    int a,b;
    a=num%10;
    b=(num-a)/10;
    sum=sum+a;
    if(b!=0)
        sumdig(b);
    else
        return(sum);
    }

sum of two numbers using Recursion

//sum of two numbers using Recursion
#include<stdio.h>
#include<conio.h>
main()
{
    int a,b,i;
    clrscr();
    printf("\nEnter two non-negative nos.:");
    scanf("%d %d",&a,&b);
    printf("\n Sum of the given nos=%d",sum(a,b));
    getch();
}

    sum(int c,int d)
    {
      if(c==0)
        return(d);
      else
        sum(--c,++d);
    }


calculate HCF using recursion, Highest common factor

main()
{
    int a,b;
    clrscr();
    printf("\n Enter any two nos.:");
    scanf("%d %d",&a,&b);
    printf("\n HCF of the two nos.:%d",gcd(a,b));
    getch();
}

    gcd(int p,int q)
    {
      int r;
      r=p%q;
      if(r==0)
          return(q);
      else
          gcd(q,r);
      }

Fibonacci series using Recursion

Fibonacci series using Recursion

#include<stdio.h>
#include<conio.h>
main()
{
    int n,i;
    clrscr();
    printf("\n How many terms do you want to print:");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    printf("\n %d",fib(i));
    }
    getch();
    }

    fib(int m)
    {
       if(m==0)
          return 0;
       if(m==1)
          return 1;
       return (fib(m-1)+ fib(m-2));
    }

Calculate factorial using recursion

main()
{
    int n;
    clrscr();
    printf("\n enter the number: ");
    scanf("%d",&n);
    fact(n);
    printf("factorial of %d= %d",n,fact(n));
    getch();
  }

     /*      fact(int m)
     {
        int f=1;
        if(m==1)
           return(1);
        else
        {
           f=m*fact(m-1);
           return(f);
           }
      }          */

       fact(int z)
       {
          return(z==0?1:z*fact(z-1));
          }

exponential series using recursion

#include<stdio.h>
#include<conio.h>
#define ACCURACY 0.00001
main()
{
    int count,n;
    float x,term,sum;
    clrscr();
    printf("\n enter the value of x : ");
    scanf("%f",&x);
    n=term=sum=count=1;
    while(n<100)
      {
         term=term*x/n;
         sum=sum+term;
         count=count+1;
      if(term<ACCURACY)
           n=200;  // to get out of the loop if accuracy b/w terms is less than 0.00001
      else
           n=n+1;
      }
      printf("\n NO. of Terms=%d   sum=%f",count,sum);
           getch();
 }

Decimal to binary using recursion

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

main()
{
int n;
clrscr();
printf("\nEnter any decimal no.:");
scanf("%i",&n);
printf("\nBinary equivalent of %d =");
convert(n);
getch();
}

int convert(int x)
{
int i;
    if (x==0)
       return(0);

    i=x%2;
    convert(x/2);
    printf(" %d",i);
}

read about Decimal to Binary

find Binomial coefficient using recursion

Recursion is the process of repeating items in a self-similar way.

#include<stdio.h>
#include<conio.h>
main()
{
    int n,m,x,bino;
    clrscr();
    printf("\n enter the value of n: ");
    scanf("%d",&n);
    printf("m/x");
    for(m=0;m<=n;m++)
       printf("%5d",m);  // print in horz.
    printf("\n---------------------------------------------------------\n");
    m=0;
    do{
        printf("%2d",m); // print for vertical
        x=0;
        bino=1;
           while(x<=m)
           {
           if((m==0)||(x==0))
               printf("%5d",bino);
           else
              {
             bino=bino*(m-x+1)/x;    // calculate C(m,r)
             printf("%5d",bino);
              }
              x=x+1;
        }
        printf("\n");
        m=m+1;
        }while(m<=n);
        getch();
}

Pointer Increment

#include<stdio.h>
#include<conio.h>
main()
{
    int i=3,*x;
    float j=1.5,*y;
    char k='c',*z;
    clrscr();
    x=&i;
    y=&j;
    z=&k;
    printf("\n Original Add. in x=%u",x);
    printf("\n Original Add. in y=%u",y);
    printf("\n Original Add. in z=%u",z);
    x++;
    y++;
    z++;
    printf("\n New Add. in x=%u",x);
    printf("\n New Add. in y=%u",y);
    printf("\n New Add. in z=%u",z);
    getch();
}

Addition and subtraction using pointer

addition & subtraction of a number from a pointer

#include<stdio.h>
#include<conio.h>
main()
{
    int a[]={10,20,30,40,50};
    int *j,*k;
    clrscr();
    j=a;   // or j=&a[0];
    k=a+4;  // or k=&a[4];
    printf("\n %d %d",*j,*k);
    j=j+2;
    k=k-1;
    printf("\n %d %d",*j,*k);
    getch();
    }

subtraction of using pointer

//subtraction of one pointer from another

#include<stdio.h>
#include<conio.h>
main()
{
    int a[]={10,20,30,45,67,56,74};
    int *i,*j;
    clrscr();
    i=&a[1];
    j=&a[5];
    printf("\n %d %d",j-i,*j-*i);
    getch();
    }

Pointers to array

main()
{
    int a[][4]={ 5,6,7,8,
             1,2,3,4,
             2,3,4,5
             };
    int *p;
    int (*q)[4];  // q is a pointer to an array of 4 integer
    clrscr();
    p=a;
    q=a;
    printf("\n Add. of :p=%u ,q=%u",p,q);
    p++;
    q++;
    printf("\n New Add. of :p=%u ,q=%u",p,q);
    getch();
}

insert element in any array

main()
{
    int n,count=0,i,j,k,m,temp;
    int a[10];
    clrscr();
    printf("\n Enter number of elements:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
      printf("\n Enter the element no. %d:",i+1);
      scanf("%d",&a[i]);
    }
    for(j=1;j<n;j++)
    {
      printf("\n Iteration number %d:-\n",j-1);
      for(m=0;m<n;m++)
        printf("%d\n",a[m]);

      for(i=0;i<j;i++)
        if(a[i]>a[j])
          {
        temp=a[j];
        for(k=j;k>i;k--)
          a[k]=a[k-1];

        a[k]=temp;
        break;
          }
       printf("\n");
       getch();
       }
      printf("last iteration is %d:-\n",j-1);
      for(i=0;i<n;i++)
     printf("%d\n",a[i]);
 getch();
 }

Insert element at desired place in array

main()
{
int n,i,j,k,temp;
int a[10];
clrscr();
printf("\n Enter number of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the %d th element",i+1);
scanf("%d",&a[i]);
}
for(j=1;j<n;j++)
   for(i=0;i<j;i++)
    if(a[i]>a[j])
    {
     temp=a[j];
         for(k=j;k>i;k--)
           a[k]=a[k-1];

     a[k]=temp;
     break;
       }
 for(i=0;i<n;i++)
   printf("%d\n",a[i]);
 getch();
 }

Find biggest number in array


#include<stdio.h>
#include<conio.h>
main()
{
    int i,j,n,a[10],max;
    clrscr();
    max=0;
    printf("\n Enter the elements:");
    for(i=0;i<10;i++)
    {
        printf("\n Enter the elements a[%d]=",i);
        scanf("%d",&a[i]);
    }
    for(i=0;i<10;i++)
        if(a[i]>max)
            {
            max=a[i];
                        }
        printf("\n %d",max);
    getch();
}

Insert any element into an array and then sort the array

Insert any element into an array  and then sort the array
#include<stdio.h>
#include<conio.h>
main()
{
    int a[15],n,m,i,j,temp;
    clrscr();
    printf("\n How many elements: ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
      {
          printf("\n Enter element no. %d : ",i+1);
          scanf("%d",&a[i]);
      }
    printf("\n Original unsorted elements are:");
    for(i=0;i<n;i++)
         printf(" %d ",a[i]);
    printf("\n enter the element to be inserted : ");
    scanf("%d",&a[n]);

    m=n+1;  // n is increased b'coz of entry of new element
    for(i=0;i<m;i++)  // Bubble Sort
        for(j=0;j<m-1;j++)
            if(a[j]<=a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
    printf("\n Sorted elements of the array are:");
    printf("\n");
    for(i=0;i<m;i++)
         printf(" %d ",a[i]);
    getch();
}

Insert any element into an array at a desired position

Insert any element into an array at a desired position
#include<stdio.h>
#include<conio.h>
main()
{
    int a[15],n,i,item,pos;
    clrscr();
    printf("\n How many elements: ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
      {
          printf("\n Enter element no. %d : ",i+1);
          scanf("%d",&a[i]);
      }
    printf("\n Array elements are:");
    for(i=0;i<n;i++)
         printf(" %d ",a[i]);
    printf("\n enter the element to be inserted : ");
    scanf("%d",&item);
    printf("\n enter the position of insertion : ");
    scanf("%d",&pos);
    for(i=n;i>=pos;i--)  // shifting the elements
         a[i]=a[i-1];

    a[--pos]=item;
      /* pos=pos-1; // pos. is decremented b'coz of Array storage process
    a[pos]=item; */

    printf("\n New array elements are:");
    printf("\n");
    for(i=0;i<=n;i++)
         printf(" %d ",a[i]);
    getch();
}

Reverse array

main()
{
    int a[10],n,i,j,t;
    clrscr();
    printf("\n Enter no. of terms:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
     {
       printf("\n Enter ele. no. %d-:",i+1);
       scanf("%d",&a[i]);
     }

    for(i=0,j=n-1;i<=n/2;i++,j--)
       {
          t=a[i];
          a[i]=a[j];
          a[j]=t;
       }
    printf("\n Reversed Array\n");
    for(i=0;i<n;i++)
       printf("\n %d",a[i]);
    getch();
}

Array Bubble sort

-: Bubble Sort :-
#include<stdio.h>
#include<conio.h>
main()
{
    int i,j,n,a[10],temp;
    clrscr();
    printf("\nEnter the number of terms:");
    scanf("%d",&n);
    printf("\n Enter the elements:");
    for(i=0;i<n;i++)
    {
        printf("\n a[%d]=",i);
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
        for(j=0;j<n-1;j++)
            if(a[j]<=a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
    for(i=0;i<n;i++)
        printf("\n %d",a[i]);
    getch();
}

Selection Sort


//                    -: Selection Sort :-
#include<stdio.h>
#include<conio.h>
main()
{
    int i,j,n,a[10],temp;
    clrscr();
    printf("\nEnter the number of terms:");
    scanf("%d",&n);
    printf("\n Enter the elements:");
    for(i=0;i<n;i++)
    {
        printf("\n a[%d]=",i);
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
        for(j=i;j<n;j++)
            if(a[i]>a[j])
            {
temp=a[j];
                a[j]=a[i];
                a[i]=temp;
            }
    for(i=0;i<n;i++)
        printf("\n %d",a[i]);
    getch();
}

Insertions Short

main()
{
    int a[25], i, j, k, m, t ;

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


    for(i=1;i<=24;i++)
    {
        t = a[i] ;
        for(j=0;j<i;j++)
        {
            if(t<a[j])
            {
                for(k=i;k>=j;k--)
                     a[k] = a[k-1] ;

                a[j] = t ;
                break ;
            }
        }
    }

    for(i=0;i<=24;i++ )
                     printf("%d",a[i] ) ;
}

Delete an element from the Array

// Delete an element from the Array
#include<stdio.h>
#include<conio.h>
main()
{
    int a[15],n,i,j,flag=0,item,num;
    clrscr();
    printf("\n How many elements : ");
    scanf("%d",&n);
    num=n;  //It wll be used to find the no. of duplicates
    for(i=0;i<n;i++)
        {
        printf("\n enter the element no. %d: ",i+1);
        scanf("%d",&a[i]);
        }
    printf("\n The elements are: \n");
    for(i=0;i<n;i++)
        printf(" %d ",a[i]);
    printf("\n enter the element to be deleted : ");
    scanf("%d",&item);



    for(i=0;i<n;i++)        // Main logic
       {
          if(a[i]==item)     // i.e. item is found in the array
        {
            flag=1;
            for(j=i;j<n;j++)
            a[j]=a[j+1]; //using shifting, item is deleted
            n--;
        }    // Looping to check whether more 'item' ;left or not
       }  //end of main logic

       if(flag==0)
        printf("\n element not found  ");
       else
       {
      printf("\n Array has %d duplicates",num-n);
      printf("\n Array after deletion \n");
      for(i=0;i<n;i++)
        printf(" %d ",a[i]);
    }
    getch();
 }

find the prime factor of the given number

find the prime factor of the given number */
#include<stdio.h>
#include<conio.h>
main()
{
    int i=2,j=0,k,n,arr[15];
    clrscr();
    printf("enter any integer greater than 1:");
    scanf("%d",&n);
    while(i<=n)
    {
        if(n%i==0)
        {
            do{
            arr[j]=i;
            n=n/i;
            j++;
            }while(n%i==0);
        }
        else
        i++;
    }
    printf("prime factors of the number are\n");
     /*    for(k=j-1;k>=0;k--)
    printf("%d\t",arr[k]);    */
    for(k=0;k<=j-1;k++)
    printf("%d\t",arr[k]);
    getch();
}


convert decimal number into binary

// to convert decimal  number into binary
#include<stdio.h>
#include<conio.h>
main()
{
    int n,a[25],i=0,j;
    clrscr();
    printf("\n Enter any integer : ");
    scanf("%d",&n);
    while(n>0)
    {
        a[i]=n%2;
        n=n/2;
        i++;
    }
    for(j=i-1;j>=0;j--)
      printf(" %d ",a[j]);
       getch();
}

find number of digits in the given integer

/* To find the no. of digits in the given integer */
#include<stdio.h>
#include<conio.h>
main()
{
    int x,i=0,a[10],k;
    long n;
    clrscr();
    printf("\n enter an integer :");
    scanf("%ld",&n);
    while(n>0)
    {
    x=n%10;
    a[i]=x;
    n=n/10;
    if(a[i]==0)
        break;
    else
        i++;
    }
    printf("\n The number of digits in the given number are:%d",i);
    printf("\n And the numbers are:");
    for(k=i-1;k>=0;k--)
        printf("\n%d",a[k]);

    getch();
    }

Calculate HCF of two numbers

main()
{
    int a,b;
    clrscr();
    printf("\n Enter any two nos.:");
    scanf("%d %d",&a,&b);
    printf("\n HCF of the two nos.:%d",gcd(a,b));
    getch();
}

    gcd(int n,int d)
    {
      int r;
      do{
         r=n%d;
         n=d;
         d=r; }while(r!=0);
      return(n);
      }

Reverse any given integer

/* to reverse any given integer */
#include<stdio.h>
#include<conio.h>
main()
{
    int n,m,x,sum=0;
    clrscr();
    printf("\ngive the number:");
    scanf("%d",&n);
    m=n;
    while(n>0)
    {
    x=n%10;
    sum=sum*10+x;
    n=n/10;}
    printf("\ngiven number is =%d",m);
    printf("\nreversed number is =%d",sum);
       getch();
       }

Program for prime number

#include<conio.h>
#include<time.h>
#include<dos.h>
#include<math.h>
#include<iostream.h>
long isprime( long n){
long no =n;
long sw=0;
for(long i = 2;i<(sqrt(n)+1);i++){
//for(long i = 2;i<n;i++){
if(no%i==0){
sw =1;
break;
}}
if(sw==1)
return 0;
return 1;
}

void main(){
clrscr();
 time_t t1 , t2;
time(&t1);
long l,u;
cout<<"\n Enter lower range : ";
cin>>l;
cout<<"\n Enter upper range : ";
cin>>u;
cout<<"\n From "<<l<<" to "<<u<<" the following numbers are prime : \n";
for(long i=l;i<=u;i++){
if(isprime(i))
cout<<i<<"\t";
}
time(&t2);

cout<<"The difference is "<<difftime(t2,t1);
getch();
}

Popular 5 Posts Last 7 days