Menu BAR

FEEL FREE TO ASK ANY PROGRAM and please mention any error if you find it

31 Jan 2013

C-program to find PERFECT NUMBER

Q.) Find out the perfect number using C- program .


Sol)

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

int main( )
{
    int n, i=1, sum=0;
    printf("Enter a Number : ") ;
    scanf("%d",&n) ;
    
    while(i<n)
      {
           if(n%i==0)
                 {  sum=sum+i ; }
           i++ ;
       }
    if(sum==n)
           printf("\n %d is a perfect number", i) ;
    else
           printf("\n %d is not a perfect number", i)  ;
    return 0;
}

                     :: OUTPUT ::
      
Enter a Number : 6
6 is perfect number
                                            

30 Jan 2013

Linear Search using C program

#include<stdio.h>
int main( )
{
    int a[10], i, n, m, c=0 ;
    printf("Enter the size of an array : ") ;
    scanf("%d", &n) ;
    printf("Enter the elements of an array: ") ;
    for(i=0;i<=n-1;i++)
      {   
            scanf("%d",&a[i]);
       }
     printf("Enter the number to be searched: ") ;
     scanf("%d",&m) ;
     for(i=0;i<=n-1;i++)
        {
              if(a[i]==m)
                {   c=1;
                   break;
                }
          }
     if(c= =0)
                printf("The number is found at %d position",i+1) ;
     else
                printf("The number is not in an array  ") ;
     return 0 ;
}

29 Jan 2013

Table

Q.)  Write a program to printout the following table:
        For ex->
                        5*1=5
                        5*2=10
                        5*3=15
                        5*4=20
                        5*5=25
                        5*6=30
                        5*7=35
                        5*8=40
                        5*9=45
                        5*10=50

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

int main( )
{
   int x=1, num, res ;
   printf("Enter Number : " ) ;
   scanf("%d", &num) ;
   while(x<=10)
     {
          res=num*x ;
          printf("\n%d*%d=%d",num,x,res ) ;
          x++ ;
      }

    return 0;
}

Algo for even or odd Number

Q.) Write a Algorithm to find out number is odd or even .

Sol.)

Step 1: Start
Step 2: Input Number
Step 3: rem=Number mod 2
Step 4: if rem=0 then,
                     print "EVEN number"
            else
                     print "ODD number"S
Step 5: Stop

Palindrome Using Pointer

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

int main( )

{
  char str[30];
  char *p, *t ;
  printf("Enter any string : ");
  gets(str) ;
  for(p=str; *p!=NULL; p++)
        for(t=str; p--; p>=t)
            {
                 if(*p==*t)
                      {
                            p-- ;
                            t++ ;
                       }
                else
                           break ;
            }
  if(t>p)
      printf("\n\t String is PALINDROME") ;
  else
      printf("\n\t String is NOT palindrome") ;

   return 0;

}


           :: Output ::

Enter any string : MADAM
            String is PALINDROME

25 Jan 2013

Fibonacci Series

Q.)  Write a C program to Fibonacci numbers less than given user number.

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

int main( )
{
int x, y, z, num ;
printf("Enter any last Number of FIBONACCI series : ") ;
scanf("%d", &num) ;
x=0;
y=1;

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

return 0 ;

}
                   :: OUTPUT OF ABOVE PROGRAM ::
Enter any last Number of FIBONACCI series : 60

1    1    2    3     5     8      13      21       34       55

Swapping using MALLOC FUNCTION

 Q.) Write a C program for swapping of two values through malloc function.

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

struct stud
{
char nam[30] ;
struct stud *next ;
} ;

struct stud *p,*q ;

int main( )
{
 p=(struct stud *)malloc(sizeof(struct stud)) ;
 q=(struct stud *)malloc(sizeof(struct stud)) ;
printf("Enter NAME p : ") ;
gets(p->nam) ;
            //fflush(stdin) ;
printf("Enter NAME q : ") ;
gets(q->nam) ;
p->next=q ;
q->next=p ;
printf("\n Name of P : %s", p->next->nam) ;
printf("\n Name of Q : %s", q->next->nam) ;
return 0 ;
}

        :: OUTPUT of above PROGRAM ::

Enter NAME p : BUNNY
Enter NAME q : SONU

Name of P : SONU
Name of Q : BUNNY

24 Jan 2013

Algorithm for GRAPH

Q.) Write a ALGORITHM to show whether the GRAPH  is Connected or Not. 

Sol)

# Comp( Graph )
{
    For each vertex v belongs to Vertices[Graph]
        {
             make-set(v)
         }
   For each edge (u,v) belongs to Edges[Graph]
         {
              if(find-set(u) not equal to find-set(v))
                  {
                       Union(u,v)
                  }
         }

}

Connected-Comp(u,v)
{
      if (find-set(u)=find-set(v))
           {
                print "CONNECTED"
            }
      else
                print "NOT CONNECTED"
}

Algo for MErge sort using PSUEDO-CODE

Q.) Write a ALGORITHM for the set of elements using the MERGE SORT.

Sol)

MergeSort( A, p, r)
{
   if (p<r)
    {
     mid=[(p+r)/2]
    }
MergeSort(A, p, mid)
MergeSort(A, mid+1, r)
Merge(A, p, r, mid, Z)
}

Merge(A, p, r, mid, Z)
{
  Initialize two variables 'i' and 'j'

  while( (i<=mid) AND (j<=r) )
     {
       if(x[i]<=x[j])
           {
              Z={x[i]}
              i=i+1
            }
       else
           {
              Z={x[j]}
              j=j+1
           }
     }
  if( i>mid )
    {
        Z= Z + Append(x[j]....x[r])
    }
  else
     {
        Z=Z + Append(x[i].....x[mid])
     }  

}