Showing posts with label Programs. Show all posts
Showing posts with label Programs. Show all posts

Wednesday, 11 March 2015

C Program for Given Number is AMSTRONG or NOT

Program:
/* C Program to find given number is amstrong or not for N digit number*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main( )
{
int temp = 0,n,i = 0,r = 0,m;
long int sum = 0;
printf("Enter N Value:");
scanf("%d",&n);
temp = n;
m = n;
while( temp > 0)
{
                   i++;
                   temp = temp/10;
}
while( n > 0 )
{
r = n%10;
sum = sum + pow(r,i);
n = n/10;
}
if(sum == m)
{
printf("Given Number is AMSTONG");
}
else
{
printf("Given Number is not AMSTRONG");
}
}

Output:


Tuesday, 6 January 2015

C Language Programs to Print The Patterns in C language, Pattern Programs in C


C language programs for the following pattern



Pattern1:

          1  1  1  1
          2  2  2  2
          3  3  3  3
          4  4  4  4

Program:

#include<stdio.h>
#include<conio.h>
void pattern(int);
void main()
{
      int n;
      clrscr(); //clear the screen contents
      printf("\nEnter N value: ");
      scanf("%d",&n);
      pattern(n);
      getch();
}
void pattern(int n)
{
int i,j;
for(i=1;i<=n;i++)
{ printf("\n");
for(j=1;j<=n;j++)
{
printf("%d ",i);
}
}
}




Pattern2:

           1  2  3  4
           1  2  3  4
           1  2  3  4
           1  2  3  4

Program:

#include<stdio.h>
#include<conio.h>
void pattern(int);
void main()
{
      int n;
      clrscr(); //clear the screen contents
      printf("\nEnter N value: ");
      scanf("%d",&n);
      pattern(n);
      getch();
}
void pattern(int n)
{
int i,j;
for(i=1;i<=n;i++)
{ printf("\n");
for(j=1;j<=n;j++)
{
printf("%d ",j);
}
}
}




Pattern3:

          1  0  0  0  0
          0  1  0  0  0
          0  0  1  0  0
          0  0  0  1  0
          0  0  0  0  1

Program:

#include<stdio.h>
#include<conio.h>
void pattern(int);
void main()
{
      int n;
      clrscr(); //clear the screen contents
      printf("\nEnter N value: ");
      scanf("%d",&n);
      pattern(n);
      getch();
}
void pattern(int n)
{
int i,j;
for(i=1;i<=n;i++)
{ printf("\n");
for(j=1;j<=n;j++)
{
if(i==j)
{
printf("1 ");
}
else
{
printf("0 ");
}
}
}
}




Pattern4:

            1
            0  1
            0  0  1
            0  0  0  1
            0  0  0  0  1

Program:

#include<stdio.h>
#include<conio.h>
void pattern(int);
void main()
{
      int n;
      clrscr(); //clear the screen contents
      printf("\nEnter N value: ");
      scanf("%d",&n);
      pattern(n);
      getch();
}
void pattern(int n)
{
int i,j;
for(i=1;i<=n;i++)
{ printf("\n");
for(j=1;j<=n;j++)
{
if(i==j)
{
printf("1 ");
}
else if(i>j)
{
printf("0 ");
}
else
{
printf("  ");
}
}
}
}




Pattern5:

              1  0  0  0  0
                  1  0  0  0
                      1  0  0
                          1  0
                              1

Program:

#include<stdio.h>
#include<conio.h>
void pattern(int);
void main()
{
      int n;
      clrscr(); //clear the screen contents
      printf("\nEnter N value: ");
      scanf("%d",&n);
      pattern(n);
      getch();
}
void pattern(int n)
{
int i,j;
for(i=1;i<=n;i++)
{ printf("\n");
for(j=1;j<=n;j++)
{
if(i==j)
{
printf("1 ");
}
else if(i<j)
{
printf("0 ");
}
else
{
printf("  ");
}
}
}
}






Thanks for visiting............

Saturday, 6 December 2014

C Program for Heap Sort Algorithm

Heap Sort:

         The Heap Sort Algorithm is an improved version  of the selection sort. The heap sort selects an element from the unsorted portion of the list, but it is the largest element. Because heap is a tree structure. A Heap is tree structure in which the root contains the largest element in the tree.

Program:

#include<stdio.h>
#include<conio.h>
void arrange(int *, int);
void hs(int *, int, int);
int main()
{
             int a[20],i,j,n,tmp,k;
             clrscr( );
             printf("\nEnter Number of elements:");
             scanf("%d",&n);
             printf("\nEnter %d of elements:",i);
            for(i=0; i<n; i++)
            {
                   scanf("%d",&a[i]);
                   arrange(a,i);
            }
            j=n;
            for(i=0; i<j; i++)
           {
                   tmp=a[0];
                  a[0]=a[n];
                  a[n]=tmp;
                  n--;
                  hs(a,0,n);
           }
           printf("\nSorting list in ascending order:");
           n=j;
           for(i=0; i<n; i++)
          {
               printf("\n%d ",a[i]);
          }
           getch( );
}
void arrange(int *a, int i)
{
          int tmp;
          tmp=a[i];
          while((i>0)&&(a[i/2]<tmp))
         {
                  a[i]=a[i/2];
                  i=i/2;
          }
          a[i]=tmp;
}
void hs(int *a, int i, int n)
{
          int tmp,j;
          tmp=a[i];
          j=i*2;
          while(j<=n)
         {
               if((j<n)&&(a[j]<a[j+1]))
               {
                        j++;
               }
               if(a[j]<a[j/2])
               {
                       break;
               }
               a[j/2]=a[j];
               j=j*2;
          }
          a[j/2]=tmp;
}

Output:

Enter Number of elements:5

Enter 5 of elements:4
1
20
5
9

Sorting list in ascending order:
1
4
5
9
20








C Program for Selection Sort Algorithm

Selection Sort:

             A Selection sort algorithm is one of Internal Sorting technique. It sorts the list by selecting the largest element in the unsorted list and places it at the appropriate position.In Each position, the largest element in the list is placed at appropriate position. If there are 'n' elements in the list, we require (n-1) passes.


Program:

#include <stdio.h>
#include<conio.h>
int main()
{
            int a[100], n, i, j, k,temp;
            clrscr();
            printf("\nEnter Number of elements:");
            scanf("%d", &n);
            printf("\nEnter %d of Elements:", n);
            for( i = 0 ; i < n ; i++ )
           {
                 scanf("%d", &a[i]);
           }
           for( i = 0 ; i < n - 1; i++ )
          {
                 k = i;
                 for ( j = i + 1 ; j < n ; j++ )
                {
                 if ( a[k] > a[j] )
                 k = j;
                 }
                if ( k != i )
                {
                 temp = a[i];
                 a[i] = a[k];
                 a[k] = temp;
                 }
           }
           printf("\nSorted list in ascending order:");
           for ( i = 0 ; i < n ; i++ )
           {
                    printf("\n%d", a[i]);
           }
           getch( );
}


Output:

Enter Number of Elements:4

Enter 4 of Elements:0
3
2
10

Sorted list in ascending order:
0
2
3
10




C Program for Insertion Sort Algorithm

Insertion Sort:

                  Insertion Sort is a Technique which inserts an element at an appropriate location by comparing each element with the corresponding elements at its left and moves the rest of the elements of the given array.

Program:

#include<stdio.h>
#include<conio.h>
void main( )
{
int a[20],i,n,j,temp;
printf("\nEnter N Value:");
scanf("%d",&n);
printf("\nEnter Elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
j=i;
while(j>0&&a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
j--;
}
}
printf("\nSorted Elements are:");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
}

Output:

Enter N Value:5

Enter Elements:9
3
6
10
20

Sorted Elements are:
3
6
9
10
20











Friday, 5 December 2014

C Program for Linear Search Algorithm

Linear Search:

                    The linear search method is mainly applicable for searching elements with an unordered list.  In the linear Search method each element of the list is compared with the key in a sequential order. Linear Search is also called as Sequential Search.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,val,a[20],j=0;
clrscr();
printf("\nEnter the Number of Elements:");
scanf("%d",&n);
printf("\nEnter %d of elements:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the Search element:");
scanf("%d",&val);
for(i=0;i<n;i++)
{
if(a[i]==val)
{
printf("\nElement Found");
}
else
{
j=j+1;
}
}
if(i==j)
{
printf("\nElement not found");
}
getch();

}

Output:

Enter the Number of Elements:4

Enter 4 of elements:6
2
1
9

Enter the Search element:10

Element not found



C Program for Binary Search Algorithm

Program:

#include<stdio.h>
#include<conio.h>
void main( )
{
    int a[20],val,i,n,low,high,mid;
    clrscr( );
    printf("\nEnter the Number of Elements:");
    scanf("%d",&n);
    printf("\nEnter the %d of elements:",n);
    for(i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
    }
    printf("\nThe Elements in the array are :");
    for(i=0;i<n;i++)
    {
    printf("\n%d",a[i]);
    }
    low=0;
    high=n-1;
    printf("\nEnter the Search Element:");
    scanf("%d",&val);
    while(low<=high)
   {
  mid=(low+high)/2;
  if(a[mid]==val)
  {
   printf("\nElement found");
   break;
  }
  else if(a[mid]>val)
  {
   low=mid-1;
  }
  else if(a[mid]<val)
  {
   low=mid+1;
  }
  else
  {
   printf("\nElement not found");
  }
    }
    if(low>high)
    {
    printf("\nElement not found");
    }
}


Output:

Enter the Number of Elements:5

Enter the 5 of elements:1
2
3
4
5

The Elements in the array are :
1
2
3
4
5

Enter the Search Element:5

Element found




C Program for Binary Search using Functions

Binary Search:

Binary Search Technique is implemented on sorted list of elements. It is faster than linear search, hence this method is efficient when the number of elements are large.

Program:

#include<stdio.h>
#include<conio.h>
int binarysearch(in[],int,int,int);
void main( )
{
             int a[20],val,i,n,low,high,bsf;
             clrscr( );
             printf("\nEnter the Number of Elements:");
             scanf("%d",&n);
             printf("\nEnter the %d of elements:",n);
             for(i=0;i<n;i++)
             {
                     scanf("%d",&a[i]);
             }
             printf("\nThe Elements in the array are :");
             for(i=0;i<n;i++)
             {
                     printf("\n%d",a[i]);
             }
             low=0;
             high=n-1;
             printf("\nEnter the Search Element:");
             scanf("%d",&val);
             bsf=binarysearch(a,val,low,high);
             if(bsf == -1)
             {
                        printf("\nElement is found");
             }
             else
             {
                        printf("\nElement not found");
             }
             getch( );
}
int binarysearch(int a[ ], int val, int low, int high)
{
            int mid;
            while(low<=high)
            {
                   mid=(low+high)/2;
                   if(a[mid]==val)
                   {
                            return mid;
                   }
                   else if(a[mid]>val)
                   {
                            low=mid-1;
                   }
                   else if(a[mid]<val)
                   {
                            low=mid+1;
                   }
                   else
                   {
                            return -1;
                   }
             }
             return -1;
}

Output:


Enter the Number of Elements:5

Enter the 5 of elements:
1
2
3
4
5

The Elements in the array are :
1
2
3
4
5

Enter the Search Element:10

Element not found
             




Monday, 20 October 2014

C Program that calculates the Volume of the Sphere

Program:

#include<stdio.h>
#include<conio.h>
#define pi 3.14
void main( )
{
         int r,
         float v;
         printf("\nEnter the radius of the Sphere:");
         scanf("%d",&r);
         v=(4/3)*pi*r*r*r;
         printf("\nVolume of the Sphere is %f",v);
}


Input/Output:

Enter the radius of the Sphere:1

Volume of the Sphere is  4.082000





C Program that Calculates the area of a square by giving length of the side from the user

Program:

#include<stdio.h>
#include<conio.h>
void main( )
{
         int a,l;
         printf("\nEnter the length of the Square:");
         scanf("%d",&l);
         a=l*l;
         printf("\nArea of the Square is %d",a);
}


Input/Output:

Enter the length of the Square:10

Area of the Square is 100

Write a program to read three numbers from the user and find the highest number

Program:

#include<stdio.h>
#include<conio.h>
void main( )
{
        int x,y,z,h;
        clrscr( );
        printf("\nEnter Three Numbers:");
        scanf("%d%d%d",&x,&y,&z);
        h=((x>y)&&(x>z))?x:(y>z)?y:z;
        printf("\nHighest Number is %d",h);
        getch( );
}


Input/Output:

Enter Three Numbers:10
20
30

Highest Number is 30




Write a program to read two numbers from the user and find highest number

Program:

#include<stdio.h>
#include<conio.h>
void main( )
{
           int x,y,h;
           clrscr( );
           printf("\nEnter two Numbers:");
           scanf("%d%d",&x,&y);
           h=(x>y)?x:y;
           printf("\nHighest Number is %d",h);
           getch( );
}


Input/Output:

Enter two Numbers: 10
20

Highest Number is 20




C program to Print the following Output

1 2 3 4 5 6 7 7 7 8 9 10

Program:

#include<stdio.h>
#include<conio.h>
void main( )
{
          int i,j;
          for(i=1;i<=10;i++)
          {
                     if(i==7)
                     {
                              for(j=1;j<=3;j++)
                              {
                                          printf("%d  ",i);
                               }
                       }
                       printf("%d  ",i);
            }
}


Input/Output:

1 2 3 4 5 6 7 7 7 8 9 10



Sunday, 19 October 2014

C Program for finding Second Largest Number in the given array of elements

Program

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,j,temp,n;
        clrscr( );//clear the screen contents
printf("\nEnter size of the Array:");
scanf("%d",&n);
printf("\nEnter elements to Array:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)// Sorting logic begins
{
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;
}
}
}//ends
printf("Second largest: %d",a[n-2]);
getch();
}


Input/Output:

Enter size of the Array: 5

Enter elements to Array:5
4
8
2
1
Second largest: 5



C Program for Given Number is Even or Odd using Bit-Wise Operators

Program:

#include<stdio.h>  //header file
#include<conio.h>  //header file
int odd(int); // funtion proto type
void main( )
{ //main function begins
          int n;
          printf("\nEnter a Number:");
          scanf("%d",&n);
          if(odd(n))
          {
                printf("\n Given Number is ODD");
           }
           else
           {
                printf("\n Given Number is EVEN");
            }
}//main function ends
int odd(int n)
{
           if(n&1)
           {
                      return 1;
            }
            else
            {
                       return 0;
             }
}

Input/Output:

Enter a Number: 5

Given Number is ODD







Saturday, 6 September 2014

C Program to print the first N Natural Numbers in Reverse Order

Program:

#include<stdio.h>
#include<conio.h>
void main( )
{
             int c,n;
             printf("\nEnter N value: ");
             scanf("%d", &n);
             c = n;
             while(c>0)
             {
                      printf("\n %d",c);
                      c--;
              }
              getch( );
}

Input/Output:

Enter N Value: 5
5
4
3
2
1



C Program to Print the first N Natural Numbers

Program:

#include<stdio.h>
#include<conio.h>
void main( )
{
           int c=1,n;
           printf("\n Enter N value: ");
           scanf("%d", &n);
           while(c<=n)
           {
                    printf("\n %d",c);
                    c++;
            }
            getch( );
}

Input/Output:

Enter N value: 5
1
2
3
4
5

C Program to print the given message N times


Program:

#include<stdio.h>
#include<conio.h>
typedef int count;
void main( )
{
            count c = 1,n;
            printf("\n Enter N value: ");
            scanf("%d", &n);
            A: printf("\n Let US Code in C");
                 c++;
                 if( c<=n)
                      goto A;
             getch();
}

Input/Output:

Enter N value: 5

Let US Code in C

Let US Code in C

Let US Code in C

Let US Code in C

Let US Code in C