Tuesday, 5 August 2014

C Program for Number Comparison

1.write a program to perform following operation through enter user choice- 

  1. for finding greatest no in given 2  number

  2. for finding greatest no in given  3 number

  3. for finding greatest no in given  4 number

    in this program we use( do while loop , switch case and if else condition 


    #include<stdio.h>
    #include<conio.h>
    void main()
    {
       int a,b,c,d,ch;
       clrscr();
       do
          {
             printf("\n1 for finding greatest no in  2 no");
             printf("\n2 for finding greatest no in 3 no");
             printf("\n3 for finding greatest no in 4 no");
             printf("\n0 for exit");
             scanf("%d",&ch);
             if(ch  >=  1  &&   ch  <=  3)
               {
                  printf("enter first no");
                  scanf("  %d  "   ,&a);
                 printf("enter second no");
                 scanf(" %d "    , &b);
                 if(ch    >=   2)
                      {
                            printf("enter third no");
                            scanf("  %d  "  ,&c);
                            if(ch  ==  3)
                              {
                                   printf("enter fourth no");
                                   scanf("  %d  " ,&d);
                             }
                    }
             }
    switch(ch)
        {
          case 1:

                     if( a  ==   b )                                 
                         {
                             printf("these are equal value");
                         }
  4.                   else if( a > b )
                        {
                          printf("%d  is greater than  %d ", a, b);
                         }
                    else
                        {
                         printf("%d  is greater than  %d", b, a);
                         }
                   break;
    case 2:
                   if(a > b  &&  a > c)
                           printf("%d  is greater than  %d  and %d" , a, b , c);
                   else if( b> c)
                          printf("%d  is greater than %d and %d", b, c, a);
                   else
                          printf("%d  is greater than %d and %d", c, a, b);
                  break;
    case 3:
                 if(a > b  &&  a > c  &&  a > d)
                         printf("%d  is greater than %d , %d and %d", a, b, c, d);
                else if(b > c  &&  b > d)
                        printf("%d  is greater than %d, %d and %d", b, c, d, a);
                else if( c > d  )
                         printf("%d  is greater than %d, %d and %d", c, d, a, b);
                else
                         printf("%d is greater than %d, %d and %d", d, a, b, c);
                break;
    case 0:
               printf("oops! you press wrong key"); 
               break;
    default:
               printf("wrong choice");
      }
    }
    while(  ch  !=  0 );
    getch();
    }
     
      

Saturday, 2 August 2014

C Programs to Print Patterns


1. Write a Program to Print the following pattern:
                    *
                    * *
                    * * *
                    * * * *
                    * * * * *


#include<stdio.h>
main() 
{
        int  i, j;
        for(i = 5 ; i >= 1 ; i--)
        {
                 for(j = i ; j <= 5 ; j++) 
                {
                        printf("*"); 
                } 
                printf("\n");
        }
}


2. Write a Program to Print the following pattern:
                     * * * * *  
                     * * * * 
                     * * * 
                     * * 
                     *


#include<stdio.h>
main() 
{
        int  i, j;
        for(i = 5 ; i >= 1 ; i--)
        {
                 for(j = i ; j >= 1 ; j--) 
                {
                        printf("*"); 
                } 
                printf("\n");
        }
}


3. Write a Program to Print the following pattern:
                     1
                     2 2
                     3 3 3
                     4 4 4 4
                     5 5 5 5 5 


#include<stdio.h>  
main()
 {
         int i,j;
         for(i = 1 ; i < = 5 ; i++)
         {
                 for(j = 1 ; j <= i ; j++)
                 {
                         printf("%d",i);
                 }
                 printf("\n");
         }
}


4. Write a Program to Print the following pattern:     
                     5 4 3 2 1
                     4 3 2 1
                     3 2 1 
                     2 1
                     1


#include<stdio.h>
main()
{
         int i,j;
         for(i = 5 ; i >= 1 ; i--)
         {
                 for(j = i ; j >= 1 ; j--)
                 {
                         printf("%d",i);
                 }
                 printf("\n");
         }
}



5. Write a Program to Print the following pattern:
                    1
                    1 2
                    1 2 3
                    1 2 3 4
                    1 2 3 4 5

                
#include<stdio.h>
main()
{
        int i,j;
        for(i = 1 ; i < = 5 ; i++)
        {
                for(j = 1 ; j <= i ; j++)
                {
                        printf("%d",j);
                }
                printf("\n");
        }

}



6. Write a Program to Print the following pattern:
                     *
                   *  *
                 *  *  *
               *  *  *  *
             *  *  *  *  *


#include<stdio.h>
#include<conio.h>
main()
{
      int i,j,k,c=40;
      clrscr();
      for(i = 1 ; i <= 5 ; i++)
      {
           for(j = 1 ; j <= c ; j++)
           {
                printf(" ");
           }
           for(k = 1;k <= i; k++)
           {
                printf(" *");
           }
           printf("\n");
           c--;
      }
      getch();
}    

 7. Write a Program to Print the following pattern:
  *  *  *  *  *  *  *  *  *  
  *  *  *  *      *  *  *  *
  *  *  *             *  *  *
  *  *                     *  *
  *                             *
  *  *                     *  *
  *  *  *             *  *  *
  *  *  *  *      *  *  *  *
  *  *  *  *  *  *  *  *  *

#include<stdio.h>
#include<conio.h>
 main()
   {
    int  i , j , k , l , m , c ;
    clrscr();
    do
    {
    printf(" enter greatest no of *" );
    scanf("%d",&m);
    c=m;
    for(i = 1 ; i <= m ; i++)
       {
           for(j = 1 ; j <= c ; j++)
              {
                 if(j == m)
                   {
                     continue;
                   }
                 printf("*");
             }
         for(k = 1 ; k < i ; k++)
             {
                if(k == 1)
                           printf(" ");
                else
                           printf("  ");
            }
        for(l = c ; l >= 1 ; l--)
           {
                   printf("*");
            }
         c--;
         printf("\n");
      }
      c=c+2;
      for(i = m-1 ; i >= 1 ; i--)
         {
             for ( j = 1 ; j <= c ; j++)
                 {
                     if(j == m)
                        {
                            continue;
                         }
                    printf("*");
                 }
           for(k = 1 ; k<i ; k++)
               {
                   if( k == 1)
                   {

                    printf(" ");
                   }
                   else
                   {

                     printf("  ");
                    }  
               }
         for( l = c ; l >= 1 ; l--)
            {
               printf("*");
            }
         c++;
         printf("\n");
       }

   } while(m!=0);

getch();
}