Friday, 31 October 2014

Single Linked List - inserting a node at the end

Aim:
To insert a node at the end of the single linked list.

Program:
#include<stdio.h>
#include<conio.h>
#include<stdib.h>
struct slinkedlist
{
   int data;
   struct slinkedlist *next;
}
typedef struct slinkedlist node;
node *start = NULL;

node* getnode ()
  {
    node *newnode;
   newnode = (node*)malloc(sizeof(node));
printf("\nEnter a data");
scanf("%d",&newnode->data);
newnode->next = NULL;
rerun newnode;
  }

void creatlist (int n)
  {
    int i ;
    node *newnode;
    node *temp; 
for ( i=0; i<n; i++)
    {
       newnode = getnode ();
    if ( start == NULL)
      {
         start = newnode;
      }
    else
       {
         temp = start;
     while( temp->next != NULL)
         temp = temp->next;
         temp->next = newnode;
        }
  }
}

void insert_beg ()
{
node *newnode;
node *temp;
newnode = getnode ();
  if ( start == NULL)
    {
     start = newnode;
    }
else
{
  newnode->next = start;
  start = newnode;
}
}

void insert_end ()
{  
    int i ;
    node *newnode;
    node *temp; 
       newnode = getnode ();
    if ( start == NULL)
      {
         start = newnode;
      }
    else
       {
         temp = start;
     while( temp->next != NULL)
         temp = temp->next;
         temp->next = newnode;
        }
}

void main()
  {
    int n;
clrscr ();
  if ( start == NULL )
{
    printf("\n Enter Number of nodes to form single linked list:");
    scanf("%d",&n);
  creatlist ( int n);
}
else
printf("\n List is already created:");
printf("\n Enter 1 to insert at beginning Or Enter 2 to insert at end: ");
scanf("%d",&num)
  if( num == 1)
{
   insert_beg();
   printf("\n a node is inserted at beginning");
  }
else  if( num == 2)
{
   insert_end();
   printf("\n a node is inserted at end");
  }
else
{
printf("\n Entered a wrong choice");
}
}

Wednesday, 29 October 2014

Palindrome ( with built-in functions )

Aim:
To check given string is palindrome or not.

Program:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main ()
  {
     char str[80],revstr[80];
     clrscr ( );
printf("\nEnter a string:");
   scanf("%s",&str)
   strcpy(revstr,str);
   strrev(revstr);
if(!strcmp(str,revstr))
   printf("\nGiven string '%s' is Palindrome",str);
else
   printf("\nGiven string '%s' is not Palindrome",str);
}

Output:
Enter a string: medem
Given string 'medem' is Palindrome

Single Linked List - Inserting a node at the beginning

Aim:
To create a linked list and insert a node at beginning.

Program:
#include<stdio.h>
#include<conio.h>
#include<stdib.h>

struct slinkedlist
{
    int data;
    struct slinkedlist *next ;
  }
typedef struct slinkedlist node;
node *start = NULL;

node* getnode()
  {
    node *newnode;
    newnode = (node*)malloc(sizeof(node));
  printf("\n Enter data:");
  scanf("%d",&newnode->data);
newnode->next = NULL;
return newnode;
}

void createlist( int n )
  {
     int n;
     node *newnode;
     node *temp;
for( i=0;i<n;i++)
   {
      newnode = getnode ();
if( start == NULL)
   {
      start = newnode ;
    }
else
   {
      temp = start ;
while( temp->next != NULL )
    temp = temp->next;
    temp->next = newnode;
   }
}
}

void insert_beg ();
{  node *newnode;
    newnode = getnode() ;
   if ( start == NULL)
      {
         start = newnode;
      }
   else
      {
         newnode->next = start;
          start = newnode;
      }
  }     

void main ()
{
    int n,num;
    if( start == NULL)
{
   printf("\n Enter Number of nodes to creatlinked list:");
   scanf("%d",&newnode->data);
creatlist( n );
}
  else
printf("\n Linked list is created");

printf("\n Enter 1 to insert a node at beginning:");
scanf("%d",&num);
if( num == 1)
      insert_beg ();
else
   printf("\nYou not intrested to insert a node at beginning:");

}
      

Single Linked list - Createl ist

Aim:
To create a single linked list with specified nodes.

Program:
#include<stdio.h>
#include<conio.h>
#include<stdib.h>
struct slinkedlist
   {
      int data;
      struct slinked list* next;
  }
typedef struct slinked list* node;
node* start = NULL;

node* getnode()
  {
     node* newnode;
newnode =(node*)malloc(sizeof(node));
    printf("\nEnter a data:");
    scanf("%d",&newnode->data);
       newnode->next = NULL;
return newnode;
   }

void creatlist(int n)
  {
    int i;
    node* newnode;
    node* temp;
for( i=0;i<n;i++)
    {
       newnode = getnode();
if(start = NULL)
    {
       start = newnode;
     }
else
    {
       temp = start;
    while( temp->next != NULL)
    temp = temp->next;
    temp->next = newnode;
    }
  }
}
void main()
  {
    int n;
  if( Start = NULL)
    {
         printf("\n Enter Number of nodes to creat linked list:");
         scanf("%d",&n);
        creatlist(n);
     }
else
   printf("\n List is created");
}

  

Sub-sting in Main string

Aim:
To find sub-string in a given string.

Program:
#include<stdio.h>
#include<conio.h>
void main ( )
{
   int count=0,count2=0,i,j,flag;
   char str[80],search[20];
clrscr();
printf("\n Enter a string:");
gets(str);
printf("\n Enter a sub-string:");
gets(search);
  while( str[count] != '\0')
    count++;
  while( search[count2] != '\0')
    count2++;
for(i=0;i<=count1-count2;i++)
  {
    for(j=i;j<i+count2;j++)
      {
        flag =1;
        if( str[i] != search[j-i] )
          {
            flag = 0;
            break;
          }
      }
  if( flag == 1)
      break;
}
  if( flag == 1)
printf("\n SEARCH SUCCESSFUL");
  else
printf("\n SEARCH NOT SUCCESSFUL");
}

Palindrome ( without built-in functions )

Aim:
To check given string is Palindrome or not.

Program:
#include<stdio.h>
#include<conio.h>
void main ( )
  {
     char str[80],revstr[80];
     int i,length=0,flag=0;
     clrscr ();
printf("\n Enter a string:");
gets (str);
for( i=0;string[i] != '\0';i++)
   {
     length++;
   }
printf("\nLength of the string \'%s'\ is : %d",str,length);
for(i=length-1;i>=0;i--)
   {
     revstr[length-i-1] = str[i];
   }
for(i=0;i<length;i++)
  {
     if(revstr[i] == str[i])
           flag = 1;
     else
          flag = 0;
    }
   if ( flag == 1)
printf("\n%s is a palindrome",str);
   else
      printf("\n%s is not a palindrome",str);
}

Tuesday, 28 October 2014

String insertion at specified location

Aim:
To insert a substring into a string.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
   char str[80],substr[20];
   char X,newstr[80];
   int pos,count1=0,count2=0,
   int i=0,t=0,l;
   clrscr ();
printf("Enter a string:");
gets(str);
printf("Enter a substring:");
gets(substr);
while( str[i] != '\0' )
    {
       count1++;
       i++;
    }
while( substr[i] != '\0' )
    {
       count2++;
       i++;
    }
printf("\nEnter the position to insert the substring");
scanf("%d",&pos);
strcpy(newstr,str);
l = pos+count2;
for( i=pos;i<=count1+count2;i++)
  {
    X = newstr[i];
    if(t < count2)
     {
       str[i] = substr[t];
          t = t+1;
      }
      str[l] = X;
      l++;
   }
printf("\nNewstring is: %s",str);
}

Program:
Enter a string: hydai
Enter a substring: un
Enter the position to insert the substring: 3
Newstring is hyundai

String Deletion from specified position

Aim:
To insert a substring into a string.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
   char str[80],newstr[80];
   int pos,len,count1=0,
   int i=0,t=0 ;
   clrscr ();
printf("Enter a string:");
gets(str);
while( str[i] != '\0' )
    {
       count1++;
       i++;
    }
printf("\nEnter the position to delete the characters:");
scanf("%d",&pos);
printf("\nEnter the number characters to delete:");
scanf("%d",&len);
t = pos;
for( i=pos+len;i<=count1-len;i++)
  {
       str[t] = substr[i];
          t = t+1;
}
printf("\nNewstring is: %s",str);
}

Program:
Enter a string: hyabundai
Enter the position to delete the characters: 3
Enter the number of characters to delete : 2
Newstring is: hyundai

String leangth using without library functions

Aim: String length using while or for loop.

Program:
Code-1:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ()
{
  char str[80];
  int length=0,i=0;
clrscr ();
printf("Enter a string:");
gets(str);
  while( str[i] != '\0' )
    {
      length++;
       i++;
    }
printf("\nLength of a string is : %d",length);
}

Code-2:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ()
{
  char str[80];
  int length=0;
clrscr ();
printf("Enter a string:");
gets(str);
  for( i=0; str[i] != '\0'; i++ )
    {
      length++;
    }
printf("\nLength of a string is : %d",length);
}

To Reverse a string using library function

Aim:
To Reverse a string using library functions

Program:

Code-1:
#include< stdio.h>
#include<string.h>
void main ()
  {
    char str[80];
    int i;
printf("Enter a String:");
gets(str);
printf("\nReverse string is:");
   for(i=strlen(str)-1;i>0;i--)
      printf("%c",str[i]);
  }

Output:
Enter a string: hahaha
Revere string is: ahahah

Code-2:
#include< stdio.h>
#include<string.h>
void main ()
  {
    char str[80];
printf("Enter a String:");
gets(str);
  printf("%s\n",str);
printf("Reverse string is:%s",strrev(str));
  }

Output:
Enter a string: cprogram
Reverse string is: margorpc

Basic String Handling Functions

Aim:
To demonstrate the string Handling Functions.

Program:
#include<stdio.h>
#inude<string.h>
void main ()
{
   char S1[50],S2[50];

puts("Enter first string:");
gets(S1);
puts("\nEntet second string:");
gets(S2);

printf("\nleangths of S1 is %d and S2 is %d.",strlen (S1),strlen (S2));

if( !strcmp(S1,S2) )
     printf("\ntwo strings are equal.");
else
     printf("\ntwo strings are not equal.");

strcat(S1,S2);
      printf("\n%s",S1);

strcpy( S1,"This is a test");
       printf("%s\n",S1);

if( strchr("hello",'e');
        printf("e is in hello\n");

if( strstr("hi these","hi");
        printf("found hi");
}

Output:
Enter first string: hello
Enter second string: hello
leangths of S1 is 5 and S2 is 5.
two strings are equal
hellohello
This is a test
e is in hello
found hi

Monday, 27 October 2014

Size of Array

Non-Recursive C Program:

Aim:
To find size of an array.

Program:
#include <stdio.h>
int main( )
{
   int i,num;
printf("\nEnter the hoe many elements you want?\n"); scanf("%d",&num);

int a[num];
printf("\nEnter the %d elements:\n",n um);
for(i=0;i<num;i++)
  {
    scanf("%d",&a[i]);
   }
printf("Elements in array are:\n");       for(i=0;i<num;i++)
{
   printf("%d\n",a[i]);
  }

printf(" The size of the array is: %d Bytes.",(int)sizeof(a));

return 0 ;
}

Compare Strings

Non-Recursive C Program:

Aim: To compare two strings

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
  int count1=0,count2=0,flag=0,i; char str1[10],str2[10];
clrscr();
puts("Enter a string:");
gets(str1);
puts("Enter another string:"); gets(str2);

while (str1[count1]!='\0')
     count1++;
while (str2[count2]!='\0')
     count2++; i=0;

while ( (i < count1) && (i < count2))
{
    if (str1[i] == str2[i])
     {
        i++;
        continue;
      }
 
   if (str1[i]<str2[i])
      {
         flag = -1;
          break;
       }
        
  if (str1[i] > str2[i])
          {
             flag = 1;
             break;
          }
    }

if (flag==0)
     printf("Both strings are equal\n");
if (flag==1)
       printf("String1 is greater than string2\n", str1, str2);
if (flag == -1)
     printf("String1 is less than string2 \n", str1, str2);
  getch();
  }

Array Sorting ( Descending Order )

Non-Recursive C Program:

Aim:
To sort a array of Elements in Ascending order. ( Bubble Sort )

Program:
#include<stdio.h>
#include<conio.h>
void main ( )
{
   int A[25],i,j,m,temp;
printf("Enter the size of array:");
scanf("%d",&m);

printf("\nEnter the Elements:");
  for( i=0;i<m;i++)
    {
       scanf("%d",&A[i]);
    }

Printf("The Entered Elements: \n ");
  for( i=0;i<m;i++)
    {
       printf("%d\t",A[i]);
    }

for( i=0;i<m;i++)
{
  for( j=0;j<m-i-1;j++)
    {
       if( A[j] > A[j+1] )
         {
             temp = A[j+1];
              A[j+1] = A[j];
              A[j] = temp;
          }
      }
}

printf("\nArray Elements in Ascending order:");
  for( i=0;i<m;i++)
   {
      printf("%3d",A[i]);
    }
}

Output:
Enter the size of array: 6
Enter the Elements:
82
32
4
17
45
11
The Entered Elements:
82     32     4     17     45     11
Array Elements in Ascending order:
82      45    32    17     11    4

Binary Search

Non-Recursive C Program:

Aim:
Binary Search

Program:
#include<stdio.h>
#include<conio.h>
void main ()
{
   int A[25],i,data,flag=0,low,m,high,mid;

printf("Enter the size of array:");
scant("%d",&m);

printf("\n Enter the numbers in Ascending Order:");
  for(i=0;i<m;i++)
    {
       scanf("%d",&A[i]);
    }
printf("\n Enter the number to be search:");
scant("%d",&num);
low =0;
high = m-1;
while( low <= high )
    {
      mid = ( low+high )/2;
       if( A[mid] == data)
         {
            flag=1;
            break;
          }
       else
         {
            if( data<A[mid])
                 high = mid-1;
             else
                 low = mid+1;
           }
    }
   if( flag==1)
printf("\n Search successful, the number %d is found at %d position ",num,mid+1);
    else
printf("\nSearch unsuccessful,the number %d is not found ",num);
}

Output:
Enter the size of array : 10
Enter the numbers:
19
21
32
43
54
65
76
87
98
100
Enter the number to be search: 87
The number 87 found at 8 position.

Linear Search

Non-Recursive C Program:

Aim:
Linear Search program

Program:
#include<stdio.h>
#include<conio.h>
void main ()
{
   int A[25],i,num,flag=0,m;

printf("Enter the size of array:");
scant("%d",&m);

printf("\n Enter the numbers:");
  for(i=0;i<m;i++)
    {
       scanf("%d",&A[i]);
    }

printf("\n The Elements of array:");
for(i=0;i<m;i++)
    {
       printf("%d\t",A[i]);
    }

printf("\n Enter the number to be search:");
scant("%d",&num);

for(i=0;i<m;i++)
    {
       if(num == A[i])
         {
            flag=1;
            break;
          }
    }
   if( flag==1)
printf("\n Search successful, the number %d is found at %d position ",num,i+1);
    else
printf("\nSearch unsuccessful,the number %d is not found ",num);
}

Output:
Enter the size of array : 10
Enter the numbers:
19
21
32
43
54
65
76
87
98
100
Enter the number to be search: 87
The number 87 found at 8 position.

Array Sorting ( Ascending Order )

Non-Recursive C Program:

Aim:
To sort a array of Elements in Ascending order. ( Bubble Sort )

Program:
#include<stdio.h>
#include<conio.h>
void main ( )
{
   int A[25],i,j,m,temp;
printf("Enter the size of array:");
scanf("%d",&m);

printf("\nEnter the Elements:");
  for( i=0;i<m;i++)
    {
       scanf("%d",&A[i]);
    }

Printf("The Entered Elements: \n ");
  for( i=0;i<m;i++)
    {
       printf("%d\t",A[i]);
    }

for( i=0;i<m;i++)
{
  for( j=0;j<m-i-1;j++)
    {
       if( A[j] > A[j+1] )
         {
             temp = A[j+1];
              A[j+1] = A[j];
              A[j] = temp;
          }
      }
}

printf("\nArray Elements in Ascending order:");
  for( i=0;i<m;i++)
   {
      printf("%3d",A[i]);
    }
}

Output:
Enter the size of array: 6
Enter the Elements:
82
32
4
17
45
11
The Entered Elements:
82     32     4     17     45     11
Array Elements in Ascending order:
4      11    17    32     45    82

Sunday, 26 October 2014

Deletion of Element of Array

Non-Recursive C Program:

Aim:
To delete a element in a array.

Program:
#include<stdio.h>
#include<conio.h>
void main ( )
{
   int A[25],m,i,num,flag;
printf("\nEnter the size of array:");
scant("%d",&m);

printf("\nEntet the Elements:");
  for(i=0;i<m;i++)
    {
       scanf("%d",&A[i]);
     }

printf("\nThe Array is:");
  for(i=0;i<m;i++)
    {
       printf("%3d",A[i]);
     }

printf("\nEnter the Element to be deleted from Array:");
scant("%d",&num);

for(i=0;i<m;i++)
   {
       if( A[i]==num )
          {
            flag=1;
            pos=i;
            break;
           }
     }

if( flag==1 )
  {
     for( i=pos;i<m-1;i++)
       {
          A[i] = A[i+1];
        }
printf("\nDeletion is possible and  After Deletion");
   for( i=0;i<m-1;i++)
        {
          printf("%d\t",A[i]);
         }
   }
   else
printf("\nDeletion is not possible");
}

Insertion of Element into Array

Non-Recursive C Program:

Aim:
To insert a element into array.

Program:
#include<stdio.h>
#include<conio.h>
void main ( )
{
   int A[25],m,i,num,flag,pos;
printf("\nEnter the size of array:");
scant("%d",&m);

printf("\nEntet the Elements:");
  for(i=0;i<m;i++)
    {
       scanf("%d",&A[i]);
     }

printf("\nThe Array is:");
  for(i=0;i<m;i++)
    {
       printf("%3d",A[i]);
     }

printf("\nEnter the Element to be inserted into Array:");
scant("%d",&num);

printf("\nEnter the position of Element to be inserted into Array:");
scant("%d",&pos);

if( pos <= m+1)
{
for(i=pos;i<m+1;i++)
   {
       if( i==pos)
          A[i] = num;
        else
           {
             A[i] = A[i+1] ;
           }
     }
    
printf("\nInsertion is possible and  After Insertion");
   for( i=0;i<m+1;i++)
        {
          printf("%d\t",A[i]);
         }
   }
   else
printf("\nDeletion is not possible");
}

Sum and avg of array elements

Non-Recursive C Program:

Aim:
To insert a element into array.

Program:
#include<stdio.h>
#include<conio.h>
void main ( )
{
   int A[25],m,i,sum=0;
   float avg;
clrscr();
printf("\nEnter the size of array:");
scant("%d",&m);

printf("\nEntet the Elements:");
  for(i=0;i<m;i++)
    {
       scanf("%d",&A[i]);
     }

printf("\nThe Array is:");
  for(i=0;i<m;i++)
    {
       printf("%3d",A[i]);
     }

for(i=0;i<m;i++)
   {
     sum+ = A[i] ;
   }         
printf("\nThe sum of array Elements is %d",sum);
  avg = sum/m ;
printf("\nThe average of array Elements is %f",avg);
}

Array Search

Non-Recursive C Program:

Aim:
To search a element in a array elements

Program:
#include<stdio.h>
#include<conio.h>
void main ()
{
   int A[25],i,num,flag=0,position,m;

printf("Enter the size of array:");
scant("%d",&m);

printf("\n Enter the numbers:");
  for(i=0;i<m;i++)
    {
       scanf("%d",&A[i]);
    }

printf("\n The Elements of array:");
for(i=0;i<m;i++)
    {
       printf("%d\t",A[i]);
    }

printf("\n Enter the number to be search:");
scant("%d",&num);

for(i=0;i<m;i++)
    {
       if(num==A[i])
         {
            flag=1;
            break;
          }
    }
   if( flag==1)
printf("\n Search successful, the number %d is found at %d position ",num,i+1);
    else
printf("\nSearch unsuccessful,the number %d is not found ",num);
}

Output:
Enter the size of array : 10
Enter the numbers:
19
21
32
43
54
65
76
87
98
100
Enter the number to be search: 87
The number 87 found at 8 position.

Magic Square matrix

Non-Recursive C Program:

Aim:
To check given matrix magic square matrix or not.

Program:
#include<stdio.h>
#include<conio.h>
void main ( )
{
  int A[10][10],i,j,m,n,sum=0,sumr,suml,flag=0,sumd=0;

printf("Enter the order of matrix:\n");
scanf("%d%d",&m,&n);

printf("\nEnter the elements:");
  for(i=0;i<m;i++)
     {
        for(j=0;j<n;j++)
          {
             scanf("%d",&A[i][j]);
           }
      }

printf("\nThe matrix is:");
  for(i=0;i<m;i++)
     {
        for(j=0;j<n;j++)
          {
             printf("%3d",A[i][j]);
           }
        printf("\n");
      }

  if ( m==n)
{
printf("\n The given matrix is square matrix and");

for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
      {
         if(i==j)
           sum+ = A[i][j] ;
       }
   }

for(i=0;i<m;i++)
  {
    for(j=0;j<n;j++)
      {
         if(i==j)
           sum+ = A[i][j] ;
       }
   }

for(i=0;i<m;i++)
  { 
    sumr=0;
    for(j=0;j<n;j++)
      {
           sumr+ = A[i][j] ;
       }
    if(sum!=sumr)
      {
         flag=1;
         break;
      }
   }

for(i=0;i<m;i++)
  { 
    sumc=0;
    for(j=0;j<n;j++)
      {
           sumc+ = A[j][i] ;
       }
    if(sum!=sumc)
      {
         flag=1;
         break;
      }
   }

    if ( flag==1)
printf("\n The given matrix is not magic square matrix");
    else
printf("\n The given matrix is  magic square matrix");
}

  else
printf("\n The given matrix is not square matrix and no possible of magic square matrix");
}

Interchange Diagonal Elements of Matrix

Non-Recursive C Program:

Aim: To interchange diagonal elements of a matrix.

Program:
#include<stdio.h>
#include<conio.h>
void main ()
{
   int ma[10][10],i,j,m,n,a;

printf ("Enetr the order of the matix \n");
scanf ("%d %d",&m,&n);
if (m ==n )
{
printf ("Enter the co-efficients of the matrix\n");
for (i=0;i<m;++i)
  {
    for (j=0;j<n;++j)
     {
        scanf ("%dx%d",&ma[i][j]);
     }
  }

printf ("The given matrix is \n");
for (i=0;i<m;++i)
  {
   for (j=0;j<n;++j)
     {
      printf (" %d",ma[i][j]);
     }
  printf ("\n");
  }

for (i=0;i<m;++i)
  {
    a = ma[i][i];
    ma[i][i] = ma[i][m-i-1];
    ma[i][m-i-1] = a;
  }

printf ("THe matrix after changing
the main diagonal & secondary d iagonal elements\n");
  for (i=0;i<m;++i)
    {
      for (j=0;j<n;++j)
       {
         printf (" %3d",ma[i][j]);
       }
      printf ("\n");
    }
}
else
printf ("The givan order is not squa re matrix\n");
}

Equal matrix

Non-Recursive C Program:

Aim: To compare two matrix are equal or not.

Program:
#include <stdio.h>
#include <conio.h>
void main()
{
   int A[10][10], B[10][10]; int i, j, R1,  C1, R2, C2, flag =1;

printf("Enter the order of the matrix A\n");
scanf("%d %d", &R1, &C1);

printf("Enter the order of the matrix B\n");
scanf("%d %d", &R2,&C2);

printf("Enter the elements of matrix A\n");
for(i=0; i<R1; i++)
   {
    for(j=0; j<C1; j++)
       {
       scanf("%d",&A[i][j]);
       }
    }

printf("Enter the elements of matrix B\n");
for(i=0; i<R2; i++)
   {
     for(j=0; j<C2; j++)
      {
        scanf("%d",&B[i][j]);
       }
    }

printf("MATRIX A is\n");
for(i=0; i<R1; i++)
   {
     for(j=0; j<C1; j++)
      {
        printf("%3d",A[i][j]);
      }
    printf("\n");
   }

printf("MATRIX B is\n");
  for(i=0; i<R2; i++)
   {
     for(j=0; j<C2; j++)
      {
        printf("%3d",B[i][j]);
      }
      printf("\n");
    }

if(R1 == R2 && C1 == C2)
{
  printf("Matrices can be compared\n") ;
for(i=0; i<R1; i++)
   { 
     for(j=0; j<C2; j++)
      {
        if(A[i][j] != B[i][j])
          {
             flag = 0;
             break;
          }
       }
    }
}

else
{
printf(" Cannot be compared\n");
exit(1);
}

if(flag == 1 )
printf("Two matrices are equal\n"); else
printf("But,two matrices are not equ al\n");

}

Saturday, 25 October 2014

Transpose of a Matrix

Non-Recursive C Program:

Aim:
Transpose of a given matrix.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
   int a[10][10],m,n,i,j,b[10][10];
printf("Enter the rows and column size of matrix:\n");
scanf("%d%d",&m,&n);
printf("\n Enter the elements :");
  for(i=0;i<m;i++)
   {
     for(j=0;j<n;j++)
      {
         scanf("%d",&a[i][j]);
      }
  }
printf("\n The Matrix is:");
  for(i=0;i<m;i++)
   {
     for(j=0;j<n;j++)
      {
         printf("%3d",a[i][j]);
      }
    printf("\n");
  }
for(i=0;i<m;i++)
   {
     for(j=0;j<n;j++)
       {
         b[j][i]=a[i][j];
       }
    }
printf("\nThe Transpose Matrix is:");
  for(i=0;i<m;i++)
   {
     for(j=0;j<n;j++)
      {
         printf("%3d",b[i][j]);
      }
    printf("\n");
  }
}

Sum of Column Elements of Matrix

Non-Recursive Program:

Aim:
To know the sum of elements of a row.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
  int a[10][10],i,j,allcol=0,m,n,Col;
printf("Enter the rows and column size of a matrix");
scanf("%d%d",&m,&n);
printf("\nEnter the elements:");
for(i=0;i<m;i++)
   {  
     for( j=0;j<n;j++)
       {
          scanf("%d",&a[i][j]);
        }
    }
printf("\nThe matrix is:");
for(i=0;i<m;i++)
   {  
     for( j=0;j<n;j++)
       {
          printf("%3d",a[i][j]);
        }
      printf("\n");
    }
for(i=0;i<m;i++)
   {   Col=0;
     for(j=0;j<n;j++)
       {
          Col+=a[i][j];
        }
    allcol+=Col;
printf("\nThe %d Row sum is %d",i+1,Col);
    }
printf("\nThe all Rows sum is %d",allcol);
}

Sum of Row Elements of Matrix

Non-Recursive Program:

Aim:
To know the sum of elements of a row.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
  int a[10][10],i,j,allrow=0,m,n,Row;
printf("Enter the rows and column size of a matrix");
scanf("%d%d",&m,&n);
printf("\nEnter the elements:");
for(i=0;i<m;i++)
   {  
     for( j=0;j<n;j++)
       {
          scanf("%d",&a[i][j]);
        }
    }
printf("\nThe matrix is:");
for(i=0;i<m;i++)
   {  
     for( j=0;j<n;j++)
       {
          printf("%3d",a[i][j]);
        }
      printf("\n");
    }
for(i=0;i<m;i++)
   {   Row=0;
     for(j=0;j<n;j++)
       {
          Row+=a[j][i];
        }
    allrow+=Row;
printf("\nThe %d Row sum is %d",i+1,Row);
    }
printf("\nThe all Rows sum is %d",allrow);
}

Sum of Elements of Matrix ( C Program )

Non-Recursive C Program:

Aim:To add all the elements of given matrix.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
  int a[10][10],i,j,m,n,sum=0;
clrscr();
printf("Enter the order of matrix:\n");
scanf("%d%d",&m,&n);

Printf("\nEnter the elements of matrix:");
  for ( i=0;i<m;i++)
   {
     for( j=0;j<n;j++)
      {
         scanf("%d",&a[i][j]);
       }
    }

Printf("\nThe matrix is :");
  for ( i=0;i<m;i++)
   {
     for( j=0;j<n;j++)
      {
         printf("%3d",&a[i][j]);
       }
    }

  for ( i=0;i<m;i++)
   {
     for( j=0;j<n;j++)
      {
         sum+ = a[i][j]
       }
    }
Printf("\nThe sum of elements of matrix:%d",sum);
}

Click here to view matrix related programs.

Matrix Programs- C Programs

Here all the matrix related C non-recursive programs links are provided.

1. Addition

2. Substraction

3. Multiplication

4. Sum of all elements

5. Sum of all row elements

6. Sum of all column elements

7. Transpose of matrix

Friday, 24 October 2014

Multiplication of Two Matrices

Non-Recursive C Program:

Aim: Two print matrix multiplication.

Program:
#include <stdio.h>
#include <conio.h>
void main()
  { 
   int A[10][10], B[10][10],mat[10][10], i, j,k, R1,C1,R2,C2;
clrscr();
printf("Enter the order of the matrice A \n");
scanf("%d %d", &R1, &C1);
printf("Enter the order of the matrice B \n");
scanf("%d %d", &R2, &C2);
if(C1==R2)
{
printf("Enter the elements of  matrix A\n");
for(i=0; i<R1; i++)
   {   for(j=0; j<C1; j++)
       {
          scanf("%d",&A[i][j]);
       }
   }

printf("MATRIX A is\n");
   for(i=0; i<R1; i++)
     { for(j=0; j<C1; j++)
          {
              printf("%3d",A[i][j]);
           }
        printf("\n");
     }

printf("Enter the elements of matrix B\n");
   for(i=0; i<R2; i++)
     {
        for(j=0; j<C2; j++)
         {
             scanf("%d",&B[i][j]);
          }
     }

  printf("MATRIX B is\n");
    for(i=0; i<R2; i++)
      {
        for(j=0; j<C2; j++)
          {
             printf("%3d",B[i][j]);
          }
          printf("\n");
      }

for(i=0; i< C1 ; i++)
   {
      for ( j=0; j < R2; j++)
         {
              mat[i][j]= 0 ;
        for (k=0; k < R2; k++)
          {
          mat[i][j]+ = A[i][k] * B[k] [j];
          }
        }
    }

printf("Mul matrix is\n");
   for(i=0; i<C1; i++)
      {
        for(j=0; j<R2; j++)
           {
              printf("%3d",sumat[i][j]) ;
            }
      printf("\n");
      }
}
else
  {
  printf("\nMatrice Multiplication is not possible");
   }
}

Click here to view matrix related programs.

Subtraction of two matrix ( C Program )

Non-Recursive C Program:

Aim: Two print matrix addition.

Program:
#include <stdio.h>
#include <conio.h>
void main()
  { 
   int A[10][10], B[10][10],      sumat[10][10], i, j, R1,C1,R2,C2;
clrscr();
printf("Enter the order of the matrice A \n");
scanf("%d %d", &R1, &C1);
printf("Enter the order of the matrice B \n");
scanf("%d %d", &R2, &C2);
if(R1==R2&&C1==C2)
{
printf("Enter the elements of  matrix A\n");
for(i=0; i<M; i++)
   {   for(j=0; j<N; j++)
       {
          scanf("%d",&A[i][j]);
       }
   }

printf("MATRIX A is\n");
   for(i=0; i<M; i++)
     { for(j=0; j<N; j++)
          {
              printf("%3d",A[i][j]);
           }
        printf("\n");
     }

printf("Enter the elements of matrix B\n");
   for(i=0; i<M; i++)
     {
        for(j=0; j<N; j++)
         {
             scanf("%d",&B[i][j]);
          }
     }

  printf("MATRIX B is\n");
    for(i=0; i<M; i++)
      {
        for(j=0; j<N; j++)
          {
             printf("%3d",B[i][j]);
          }
          printf("\n");
      }

for(i=0; i<M; i++)
{
   for(j=0; j<N; j++)
         {
           sumat[i][j] = A[i][j] + B[i][j];
          }
  }

printf("Sub matrix is\n");
   for(i=0; i<M; i++)
      {
        for(j=0; j<N; j++)
           {
              printf("%3d",sumat[i][j]) ;
            }
      printf("\n");
      }
}
else
  {
  printf("\nMatrice subtraction is not possible");
   }
}

Click here to view matrix related programs.

Addition of Two Matrixes ( C Program )

Non-Recursive C Program:

Aim: Two print matrix addition.

Program:
#include <stdio.h>
#include <conio.h>
void main()
  { 
   int A[10][10], B[10][10],      sumat[10][10], i, j, R1,C1,R2,C2;
clrscr();
printf("Enter the order of the matrice A \n");
scanf("%d %d", &R1, &C1);
printf("Enter the order of the matrice B \n");
scanf("%d %d", &R2, &C2);
if(R1==R2&&C1==C2)
{
printf("Enter the elements of  matrix A\n");
for(i=0; i<R1; i++)
   {   for(j=0; j<C1; j++)
       {
          scanf("%d",&A[i][j]);
       }
   }

printf("MATRIX A is\n");
   for(i=0; i<R1; i++)
     { for(j=0; j<C1; j++)
          {
              printf("%3d",A[i][j]);
           }
        printf("\n");
     }

printf("Enter the elements of matrix B\n");
   for(i=0; i<R2; i++)
     {
        for(j=0; j<C2; j++)
         {
             scanf("%d",&B[i][j]);
          }
     }

  printf("MATRIX B is\n");
    for(i=0; i<R2; i++)
      {
        for(j=0; j<C2; j++)
          {
             printf("%3d",B[i][j]);
          }
          printf("\n");
      }

for(i=0; i<R1; i++)
{
   for(j=0; j<C1; j++)
         {
           sumat[i][j] = A[i][j] + B[i][j];
          }
  }

printf("Sum matrix is\n");
   for(i=0; i<R1; i++)
      {
        for(j=0; j<C1; j++)
           {
              printf("%3d",sumat[i][j]) ;
            }
      printf("\n");
      }
}
else
  {
  printf("\nMatrice Addition is not possible");
   }
}

Click here to view matrix related programs.

Binary Number to Decimal Number

Non-Recursive C Program:

Aim:To convert Binary Number into Decimal Number.

Program:
#include<stdio>
void main()
{
    int num,r,i=0,Dec=0,n;
printf("Enter a Binary number:");
scanf("%d",&num);
n=num;
  while(n>0)
    {
       r=n%10;
       Dec+=r*pow(2,i);
       n=n/10;
        i++;
     }
printf("\nDecimal number of %d is %d,num,Dec);
}

Output:
Enter a Binary number: 1001
Hexadecimal number of 1001 is 9

Click here to get program list

Thursday, 23 October 2014

Perfect Number ( C Program )

Recursive C Program:

Aim: To check the given number is Perfect Number or Not.

Program:
#include<stdio.h>
void main()
{
      int num,sum;
printf("Enter no to check perfect number--");
scanf("%d",&num); sum=check_Perfect(num); if(sum==num)
printf("This is perfeft no.")
else
printf("this is not a perfect number !");
getch();
}
int check_Perfect(int num)
{
   int sum=0;
   int div=num;
       if(div<1)
            exit(0);   
       else if(num%div==0)
           {
             sum+=div;
           }
      return sum;
}

Output:
Enter a Number: 6
6 is a perfect Number

Decimal Number to Binary Number ( C Program )

Non-Recursive C Program:

Aim:To convert Decimal Number into Binary Number.

Program:
#include<stdio>
void main()
{
    int num,a[25],r,i=1;
printf("Enter a Decimal number:");
scanf("%d",&num);
n=num;
  while(n>0)
    {
       r=n%2;
       a[i]=r;
       n=n/2;
        i++;
     }
printf("\nBinary number of %d is:",num);
for ( j=i; j>0; j--)
printf("%d",a[j]);
}

Output:
Enter a Decimal number: 21
Binary number of 21 is :
10101

Click here to get program list

Decimal Number to Octal Number ( C Program )

Non-Recursive C Program:

Aim:To convert Decimal Number into Octal Number.

Program:
#include<stdio>
void main()
{
    int num,a[12],r,i=1;
printf("Enter a Decimal number:");
scanf("%d",&num);
n=num;
  while(n>0)
    {
       r=n%8;
       a[i]=r;
       n=n/8;
        i++;
     }
printf("\nOctal number of %d is:",num);
for ( j=i; j>0; j--)
printf("%d",a[j]);
}

Output:
Enter a Decimal number: 24
Octal number of 24 is : 30

Click here to get program list

Decimal Number to Hexadecimal Number ( C Program )

Non-Recursive C Program:

Aim:To convert Decimal Number into Hexadecimal Number.

Program:
#include<stdio>
void main()
{
    int num,a[12],r,i=1;
printf("Enter a Decimal number:");
scanf("%d",&num);
n=num;
  while(n>0)
    {
       r=n%16;
       a[i]=r;
       n=n/16;
        i++;
     }
printf("\nHexadecimal number of %d is:",num);
for ( j=i; j>0; j--)
printf("%d",a[j]);
}

Output:
Enter a Decimal number: 21
Hexadecimal number of 21 is :
15

Click here to get program list

Perfect Number ( C Program )

Non-Recursive C Program:

Aim: To check the given number is Perfect Number or Not.

Program:
#include<stdio.h>
void main()
  {
     int num,i,sum=0;
printf("\nEnter a number:"); scanf("%d",&num);
  for(i=1;i<num;i++)
{
   if(num%i==0)
   sum=sum+i;
}
     if(sum==num)
printf("\n%d is a perfect number",num);
     else
printf("\n%d is not a perfect number", num);
}

Output:
Enter a Number: 6
6 is a perfect Number

Click here to get program list

Armstrong Number ( C Program )

Non-Recursive C Program:

Aim:To find given number is Armstrong Number or Not.

Program:
#include<stdio.h>
void main()
{
   int num,num1,arms=0,rem;
printf("Enter the number:\n"); scanf("%d",&num);
num1=num;
   while(num1>0)
     {
       rem=num%10;         arms=arms+(rem*rem*rem);           num1=num1/10;
     }
   if(num==arms)
printf(" \n%d is an Armstrong number ",num);
   else
printf("\n%d is NOT an Armstrong number",num);
}

Output:
Enter the Number: 121
121 is NOT an Armstrong number

Click Here to view programs list

Armstrong Number ( C Program )

Recursive C Program:

Aim:To find given number is Armstrong Number or Not.

Program:
#include<stdio.h>
int armn(int n)
void main()
{
   int num,res;
printf("Enter the number:\n"); scanf("%d",&num);
res=armn(num);
   if(num==res)
printf(" \n%d is an Armstrong number ",num);
   else
printf("\n%d is NOT an Armstrong number",num);
}
int armn( int n)
  {
    int arms=0,rem;
while(n>0)
     {
       rem=n%10;         arms=arms+(rem*rem*rem);              n=n/10;
      }
    return ( arms) ;
  }

Output:
Enter the Number: 121
121 is NOT an Armstrong number

Click Here to view programs list

Wednesday, 22 October 2014

Fibonacci series ( C Program )

Non-Recursive C Program:

Aim:To print Fibonacci Series.

Program:
#include<stdio.h>
void main()
{
   int fib1=0,fib2=1,fib3,limit, count=2;
printf("Enter Fibonacci series limit :\n");
scant("%d",&limit);
printf("\nThe Fibonacci series:\n");
Printf("%d\t%d",fib1,fib2);
  while(count<=limit)
    {
       fib3=fib2+fib1;
       printf("\t%d",fib3);
       fib1=fib2;
       fib2=fib3;
        count++;
     }
}

Output:
Enter Fibonacci series limit: 8
The Fibonacci series:
0  1  1  2  3  5  8  13

Click Here to view programs list

Fibonacci series ( C Program )

Recursive C Program:

Aim:To print Fibonacci Series.

Program:
#include<stdio.h>
int Fibonacci( int f1,int f2,int l)
void main()
{
   int fib1=0,fib2=1,fib3,limit ;
printf("Enter Fibonacci series limit :\n");
scant("%d",&limit);
printf("\nThe Fibonacci series:\n");
Printf("%d\t%d",fib1,fib2);
Fibonacci(fib1,fib2,limit)
 }
int Fibonacci( int f1,int f2,int l)
{
   int count=2,fib3;
     while(count<=l)
        {
            fib3=f2+f1;
            printf("\t%d",fib3);
            f1=f2;
            f2=fib3;
            count++;
       }
}

Output:
Enter Fibonacci series limit: 8
The Fibonacci series:
0  1  1  2  3  5  8  13

Click Here to view programs list

GCD and LCM of two numbers ( C Program )

Recursive C Program:

Aim:To find GCD and LCM of given two numbers.

Program:
#include <stdio.h>
long gcd(long, long);
void main()
{
  long n1, n2, hcf, lcm;
printf("Enter two integers:\n"); scanf("%ld%ld", &x, &y);
   hcf = gcd(n1, n2);
   lcm = (n1*n2)/hcf;
printf("Greatest common divisor of %ld and %ld = %ld\n", x, y, hcf); printf("Least common multiple of %ld and %ld = %ld\n", x, y, lcm);
return 0;
}
long gcd(long a, long b)
  {
     if (b == 0)
        return (a);
     else
        return (gcd(b, a % b));
  }

Output:
Enter two integers:
24
9
Greatest common divisor 24 and 9 is 3
Least common multiple of 24 and 9 is 72

Click Here to view programs list

GCD and LCM of two numbers ( C Program )

Non-Recursive C Program:

Aim: To find GCD and LCM of Given two numbers.

Program:
#include <stdio.h>
int main()
{
  int n1,n2,num,den,rem, gcd, lcm;
printf("Enter two integers:\n"); scanf("%d%d", &n1, &n2);
  if(n1>n2)
  {
    num=n1;
    den=n2;
   }
  else
  {
    num=n1;
    den=n2;
   }
rem=num/den;
    while (rem!= 0)
    { 
       num = den;
       den = rem;
       rem = num/den;
     }
   gcd = den;
   lcm = (n1*n2)/gcd;
printf("\nGreatest common divisor of %d and %d is %d\n", n1, n2, gcd);
printf("Least common multiple of %d and %d is %d\n", n1, n2, lcm);
return 0;
}

Output:
Enter two integers:
24
9
Greatest common divisor of 24 and 9 is 3
Least common multiple of 24 and 9 is 72

Click Here to view programs list

Tuesday, 21 October 2014

Palindrome ( C Program )

Recursive C Program:

Aim: To check the Given Number is Palindrome or Not

Program:
#include<iostream.h>
int reverse(int n)
void main()
{
  int num,Res;
printf("Enter the Number:");
scant("%d",&num);
Res=reverse(num);
  if(num==Res)
printf"\nThe Number %d is  palindrome",num);
  else
printf"\nThe Number %d is not palindrome",num);
}
int reverse(int n)
{
int Rem,Rev;
while(n>0)
   {
      Rem=n%10;
      Rev=Rev*10+Rem;
       n=n/10;
    }
return (Rev);
}

Output:
Enter the Number: 323
The Number 323 is Palindrome

Click Here to view programs list

Palindrome ( C Program )

Non-Recursive C Program:

Aim: To Check the Given Number is Palindrome or Not.

Program:
#include<stdio.h>
void main()
{
  int num,Rem,Rev=0,n;
printf("Enter the Number:");
scanf("%d",&num);
n=num;
  while(n>0)
   {
      Rem=n%10;
      Rev=Rev*10+Rem;
       n=n/10;
    }
  if(num==Rev)
printf("\nThe Number %d is Palindrome",num);
  else
printf("\nThe Number %d is not Palindrome",num);
}

Aim: To Check the Given Number is Palindrome or Not.

Program:
#include<stdio.h>
void main()
{
  int num,Rem,Rev=0,i;
printf("Enter the Number:");
scanf("%d",&num);
n=num;
  for(i=0;n!=0;i++)
   {
      Rem=n%10;
      Rev=Rev*10+Rem;
       n=n/10;
    }
if(num==Rev)
printf("\nThe Number %d is Palindrome",num);
  else
printf("\nThe Number %d is not Palindrome",num);
}

Click here to get programs list

Reversing a Number ( C Program )

Recursive C Program:

Aim: To Reverse the Given Number.

Program:
#include<stdio.h>
int reverse(int n)
void main()
{
  int num,Res;
printf("Enter the Number:");
scanf("%d",&num);
Res=reverse(num);
printf("\nThe Reverse Number of %d is %d",num,Res);
}
int reverse(int n)
{
int Rem,Rev;
while(n>0)
   {
      Rem=n%10;
      Rev=Rev*10+Rem;
       n=n/10;
    }
return (Rev);
}

Output:
Enter the Number: 123
The Reverse Number of 123 is 321

Click Here to view Programs list

Reversing a Number ( C Program )

Non-Recursive C Program:

Aim: To Reverse the Given Number using while loop

Program:
#include<stdio.h>
void main()
{
  int num,Rem,Rev=0,n;
printf("Enter the Number:");
scanf("%d",&num);
n=num;
  while(n>0)
   {
      Rem=n%10;
      Rev=Rev*10+Rem;
       n=n/10;
    }
printf("\nThe Reverse Number of %d is %d",num,Rev);
}

Aim: To Reverse the Given Number using while loop

Program:
#include<stdio.h>
void main()
{
  int num,Rem,Rev=0,i;
printf("Enter the Number:");
scanf("%d",&num);
n=num;
  for(i=0;num!=0;i++)
   {
      Rem=n%10;
      Rev=Rev*10+Rem;
       n=n/10;
    }
printf("\nThe Reverse Number of %d is %d",num,Rev);
}

Click here to get programs list

Factorial of Number ( C Program )

Recursive C Program:

Aim:To print the factorial of Number.

Program:
#include<stdio.h>
int fact(int n)
void main()
{
int num,f;
Printf("Enter a Number:");
scant("%d",&num);
f=fact(num);
printf("\nfactorial of Number %d is %d",num,f);
}
int fact(int n)
{
   if(n==0)
      return (1);
   else
      return (n*fact(n-1));
  }

Output:
Enter a Number: 4
Factorial of 4 is 24

Click Here to know other Programs

Factorial of Number ( C Program )

Non-Recursive C Program:

Aim:To find factorial of a Number.

Program:
#include<stdio.h>
void main()
{
  int num,fact=1,i;
Printf("Enter a Number:\n");
scanf("%d",&num);
    if(num==0¦¦num==1)
printf("Factorial of Number %d is 1",num);
  else
   {
     for(i=1;i<=num;i++)
      {
         fact=fact*i ;
      }
printf("\nFactorial of Number %d is %d",num,fact);
    }
}

Output:
Enter a Number: 4
Factorial of Number 4 is 24

Click here to get program list

Prime Numbers between two given numbers ( C Program )

Non-Recursive Program:

Aim: To print Prime numbers between given two numbers.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
  int fnum,lnum,i,j,flag=0;
printf("Enter First Number:\n");
scant("%d",&fnum);
printf("Enter Last Number:\n");
scant("%d",&lnum);
  if(lnum<2)
{
printf("There are no primes upto %d\n",lnum);
exit(0);
}
printf("Prime Numbers between %d and %d are:\n",fnum,lnum);
for(i=fnum;i<=lnum;i++)
  {
    for(j=2;j<i/2;j++)
      {
         if(i%j==0)
            {
                flag=1;
                break;
             }
       }
      if(flag==0)
      printf("\t%d",i);
   }

Output:
Enter First Number: 10
Enter Last Number:  20
Prime Numbers between 10 and 20 are: 11 13  17  19

Click here to know programs list in this blog

Prime Numbers between given two numbers (C Program)

Recursive Program:

Aim: To print Prime numbers between given two numbers.

Program:
#include<stdio.h>
#include<conio.h>
int primes( int n1,n2)
void main()
{
  int fnum,lnum;
printf("Enter First Number:\n");
scant("%d",&fnum);
printf("Enter Last Number:\n");
scant("%d",&lnum);
printf("Prime Numbers between %d and %d are:\n",fnum,lnum);
primes(fnum,lnum);
}
int primes(int n1,int n2)
{
  int i,j,flag=0;
if(n2<2)
{
printf("There are no primes upto %d\n",lnum);
exit(0);
}
  for(i=n1;i<=n2;i++)
  {
    for(j=2;j<i/2;j++)
      {
         if(i%j==0)
            {
                flag=1;
                break;
             }
       }
      if(flag==0)
      printf("\t%d",i);
   }

Output:
Enter First Number: 10
Enter Last Number:  20
Prime Numbers between 10 and 20 are: 11 13  17  19

Click here to get more programs.

Prime Number ( C Program )

Recursive Program:

Aim: To find given number is Prime Number or Not.

Program:
#include<studio.h>
#include<conio.h>
int prime( int n )
void main ()
{
   int num,res;
printf("Enter a number:");
scanf("%d",&num);
res=prime( int num );
    if (res==0)
printf("\n Given number %d is Prime Number:",num);
    else
printf("\n Given number %d is Not Prime Number:",num);
}
int prime(int n)
{
    int i,flag=0;
if(n<2)
  { flag=1;
    return(flag);
  }
for (i=2; i<n/2; i++)
  {
      if (n/i==0)
        { flag=1;
          return (flag);
         }
      else
          return (flag) ;
  }
}

Output:
Enter a Number: 11
The Given number 11 is  Prime Number
 
Click here to get more programs.

Monday, 20 October 2014

Prime Number ( C Program )

Non-Recursive C Program:

Aim:To find given number is prime Number or Not.

Program:
#include<studio.h>
#include<conio.h>
void main()
{
   int num,i,flag=0;
clrscr();
printf("Enter a Number:");
scanf("\t%d",&num);
if(num<2)
{
printf("%d is not a prime Number:",num);
exit(0);
}
  for(i=2;i<num/2;i++)
    {
       if(num/i==0)
         {
           flag=1;
           break;
         }
     }
  if(flag==0)
printf("\nThe number %d is Prime Number:",num);
  else
printf("\nThe number %d is Not a Prime Number:",num);
}

Output:
Enter a Number: 15
The number 15 is not a prime Number

Click here to know Program List

Programs List

Non-Recursive C Programs:

1.Even Number or Odd Number

2. Even Numbers

3. Prime Number

4. Prime Numbers between given            two numbers

5. Factorial of Number

6. Reversing a Number

7. Palindrome ( number )

8. Fibonacci Number

9. Armstrong Number

10. Perfect Number

11. Decimal to Binary Number

12. Decimal to Octal Number

13. Decimal to Hexadecimal

Recursive C Programs:

1.Even Number or Odd Number

2.Even Numbers

3. Prime Number

4. Prime Numbers between given            two numbers

5. Factorial of Number

6. Reversing a Number

7. Palindrome ( Number )

8. Fibonacci Number

9. Armstrong Number

Even Numbers ( C Program)

Recursive C Program:

Aim:To print even numbers between given two numbers.

Program:
#include<stdio.h>
#include<conio.h>
int Even(int n1,int n2)
void main()
{
   int num1,num2,res;
   clescr();
printf("Enter First Number:");
scant("%d",&num1);
printf("\nEnter Last Number:");
scant("%d",&num2);
printf("The even numbers between%d and %d are:\n",num1,num2);
res=even(int num1,int num2);
printf("%d\t",res);
}
int Even(int n1,int n2)
{
     int i;
for(i=n1;i<=n2;i++)
   {  
      if(i%2==0)
       return (i);
   }
}

Output:
Enter First Number: 10
Enter Last Number: 15
The even Numbers between 10 and 15 are:  10  12  14

Click here to get Program list.

Even Numbers (C Program)

Non-Recursive C Program:

Aim:To print even numbers between given two numbers.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
   int num1,num2,i;
printf("Enter First Number:");
scant("%d",&num1);
printf("\nEnter Last Number:");
scant("%d",&num2);
printf("The even numbers between%d and %d are:\n",num1,num2);
  for(i=num1;i<=num2;i++)
   {
      if(i%2==0)
      printf("%d\t",i);
   }
}

Output:
Enter First Number: 10
Enter Last Number: 15
The even Numbers between 10 and 15 are:  10  12  14

Click here to know Program list.

Even Number or Odd Number ( C Program)

Recursive C Program:

Aim: To find given number is even or odd Number.

Program:
#include<iostream.h>
#include<conio.h>
int evenorodd(int n)
void main()
{
   int num,res;
   printf("Enter a number:");
   scant("%d",&num);
res=evenorodd(num);
  if (res==0)
printf("Given number %d is even Number",num);
   else
printf("Given number %d is odd Number",num);
}
int evenorodd(int n)

   if(n%2==0)
       return (0);
   else
       return (1);
}

Click here to know program list.

Even Number or Odd Number ( C Program)

Non-Recursive C Program:

Aim: To find given number is even Number or odd number.

Program
#include<studio.h>
#include<conio.h>
Void main()
{
  int num;
  cleave();
printf("Enter a number:\n");
scanf("%d",&num);
  if (num%2==0)
printf("Given number %d is even            Number",num);
  else
printf("Given number %d is odd            Number",num);
}

Output:
Enter a number:
20
Given number 20 is even Number

Click here to know programs list in this blog.