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])
     }  

}

Algo of QUICK SORT using psuedo-code

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

Sol)

# Quicksort(Arr, P, R)
{
      if(P<R)
       {
            q=partition(Arr, P, R)
        }


Quicksort(Arr, P, Q)
Quicksort(Arr, Q+1, R)
}

Partition(Arr, P, R)
{
   x=Arr[P]
   i=p-1
   j=r+1

 while(TRUE)
{
       do
     {
           j=j-1
      }
      while(Arr[j]>x)
          do
          {
             i=i+1
          }
          while(Arr[i]<x)
              if(i<j)
                     {
                         Exchange(Arr[i],Arr[j])
                       }
              else
                      {
                          return j
                        }

}   

19 Jan 2013

INSERTION SORTING using C language

Q.) Write a program to implies the insertion sorting method.

Sol)

#include<stdio.h>
#include<conio.h>
int main( )
{
  int arr[30];
  int i,j, size, tmp ;
  printf("---Insertion Sorting Method---\n\n") ;
  printf("Enter the total no. of elements:  ") ;
  scanf("%d", &size) ;
  for(i=0; i<size; i++)
    {
        printf("Enter %d element: ", i+1)  ;
        scanf("%d", &arr[i]) ;
    }

for(i=0; i<size; i++)
   {
       for(j=i-1; j>=0; j--)
         {  
             if(arr[j]>arr[j+1])
                { 
                  tmp=arr[j] ;
                  arr[j]=arr[j+1] ;
                  arr[j+1]=tmp;
                }
             else
                 break;
         }
    }
printf("\n\t --- Insertion Sorted Elements----\n\n") ;

for(i=0; i<size; i++)
    printf("%d", arr[i]) ;
return 0;

}

QUICK SORT using C program

Q.) Write a program to sort the given number of elements using QUICK SORT 


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

void qsort(int, int, int) ;     //Prototype

int main( )
{
    int arr[30];
    int i, size ;
    printf("Enter total number of Elements:  ") ;
    scanf("%d", &size);
    printf("Enter the Elements: \n")
    for(i=0; i<size; i++)
       scanf("%d", &arr[i]) ;
    qsort(arr, 0, size-1) ;    //calling
    printf("Quick Sorted elements are as:  \n") ;
    for(i=0; i<size; i++)
       printf("%d\t",arr[i]);

    return 0;
}

void qsort(int arr[20], int frst, int last)     //definition
 {
   int i, j, pivot, tmp;
     if(frst<last)
        {
            pivot=frst ;
            i=frst ;
            j=last;
           while(i<j)
             {
                while(arr[i]<=arr[pivot] && i<last)
                      i++  ;

                while(arr[j]>arr[pivot])
                      j-- ;
                if(i<j)
                   {
                         tmp=arr[i];
                         arr[i]=arr[j] ;
                         arr[j]=tmp;
                    }
             }                     //end of while loop
        tmp=arr[pivot] ;
        arr[pivot]= arr[j];
        arr[j]=tmp ;
        qsort(arr,frst,j=1) ;
        qsort(arr, j+1, last) ;
     }                    //end of if statement

}

18 Jan 2013

Number Pyramid 7

Q) Write a program to generate a following number structure:

54321
54321
54321
54321
54321

Sol )

#include<stdio.h>
#include<conio.h>
int main( )
{
 int num, r, c ;

printf("Enter the loop repeat number(rows):: ") ;

scanf("%d",&num);
for(r=1; num>=r; r++)
    {
          for(c=5; c>=1; c--)
                 printf("%d",c) ;
          printf("\n") ;
     }

return 0 ;

}

Number Pyramid 6

Q.) Write a program to generate a following number structure :

12345
12345
12345
12345
12345

Sol)

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

int num, r, c ;
printf("Enter loop repeat number(rows)::") ;

scanf("%d",&num) ;
for(r=1; num>=r; r++)
  {
    for(c=1; c<=num; c++)
        printf("%d",c) ;
    printf("\n") ;
  }

return 0 ;

}

17 Jan 2013

CONTACT ME

FEEL FREE TO ASK ANY PROGRAM, OR ANY TUTORIAL REGARDING C or C++ . I WILL DO THE proceedings AS SOON AS POSSIBLE... 


 You can contact me through my gmail account :                    chopra.himanshu1992@gmail.com

Search maximum and minimum number using C program

Q.) Write a program for searching maximum and minimum numbers.

Sol.)

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

int main( )
{
int arr[10] ;
int i, num, min, max;

for(i=0; i<10; i++)
{
    printf("Enter %d number  : ",i+1) ;
    scanf("%d",&arr[i]);
}

min=max=arr[0] ;

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

{
     if(arr[i]<min)
           min=arr[i];
    else if(arr[i]>max)
           max=arr[i];
}

printf("\n Maximum number is %d",max) ;
printf("\n Minimum number is %d", min) ;

return 0 ;

}

Number Pyramid 5

Q.) Write a program to generate a following triangle :-


    #
    ##
    # # #
    # # # # 
    # # # # #

Sol)

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

int main( )

{
int num, r, c;

printf("Enter loop repeat number(rows): " );

scanf("%d", &num) ;

for (r=1; num>=r; r++)
 { 
      for(c=1; c<=r; c++)
               printf("#") ;
      printf("\n") ;    
}

return 0 ;

}

16 Jan 2013

C program to find size of DATA TYPES

Q.) Write a simple program to find the size of different basic data types in C  


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

int main( ) 
{
int i;
char ch ;
float f;
long l;

printf("\n\t Size of different data types as following \n") ;

printf("\n Size of character ch is %d", sizeof(ch));

printf("\n Size of integer i is %d", sizeof(i));

printf("\n Size of float f is %d", sizeof(f));

printf("\n Size of long l is %d", sizeof(l));

return 0;

}

15 Jan 2013

Number pyramid 4

Q) Write a PROGRAM to generate the following triangle :-

                                            # # # # #
                                               # # # #
                                                  # # #
                                                     # #
                                                        #

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

int main( )

{
int num, r=1, c, sp ;

printf(" Enter loop repeat number(rows): ") ;
scanf("%d",&num);

for( ; num>=1;num--,r++)
{
   for(sp=r; sp>1; sp--)
        printf(" ");
   for(c=1; c<=num; c++)
        printf("#") ;
   printf("\n");
}

return 0;
}

Number pyramid 3

Q) Write a program to generate a following structure:

@@@@@
@@@@@
@@@@@
@@@@@
@@@@@

Sol)

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

int main( )
{

int num, r, c;

printf(" Enter loop repeat number(rows): ");

scanf("%d", &num);

for(r=1; num>=r; r++)
{
    for(c=1; c<=num; c++)

          printf("@") ;

printf("\n") ;

}
return 0;
}


Number pyramid 2


Q) Write a C program to print the following :-

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

Sol)

#include<stdio.h>


void pyramid(int );    //prototype

int main( )
{
int num;

printf(" Enter the number of rows : ") ;

pyramid(num) ;     // Call

return 0;
}
void pyramid(int n  )    //definition
{
int r,c,x=1,y=1;
for(r=1;r<=n; r++)
   {   
     for(c=1; c<=r; c++,x++)
      {
       if(x<=9)
           printf("%d", x);
      else
         {
           printf("%d", y);
           y++;
         }
      }
printf("\n");

   }
}

Number pyramid 1

 Q.) Write a program to print the following number pyramid:

1
123
12345
1234567      

Sol)

#include<stdio.h>

int main()
int num, r, c, p=1;

printf("Enter the number of rows: ");

scanf("%d", &num) ;

for(r=1; r<=num; r++,p=p+2)
{
   for(c=1; c<=p; c++)
      {
        printf("%d",c) ;
       }
   printf("\n") ;
}

return 0 ;

}

14 Jan 2013

Find whether the number is EVEN OR ODD?

#include<stdio.h>

#include<conio.h>

void main( )                                       // returns no value

{

clrscr( ) ;

int num  ;

printf(" Enter the number ") ;

scanf("%d", &num) ;

if(num%2 = = 0)
   {
     printf(" NUMBER IS EVEN ") ;
    }
else
   {
    printf(" NUMBER IS ODD") ;
   }

getch( );

}

13 Jan 2013

FIND PRIME NUMBER USING C PROGRAM

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

void main( )

{
clrscr( ) ;
int num,count=0;

printf(" ENTER THE NUMBER TO CHECK IT IS PRIME OR NOT " ) ;

scanf("%d", &num);

for(int i=0;i<=num;i++)

{
   if(num%a= = 0)                                 //if statement used
       {     count++ ;  }
}

if(count= =2)                                       //if else selection statement used
{
   printf("Prime Number");
}
else
{
   printf("Not a  Prime Number");
}
getch( );

}

PROGRAM IN C FOR STARTERS

#include<stdio.h>           //  standard input/ output
#include<conio.h>         //  console input/output
void main( )                   //   program begins from main( ) function
{
 clrscr( ) ;                       //   clear the screen

printf("WELCOME TO C PROGRAMMING") ;

getch( ) ;
}