Showing posts with label Switch Case. Show all posts
Showing posts with label Switch Case. Show all posts

Sunday, 14 October 2012

Case structure006

Write a program to convert an input number in Binary, Octal and Hexadecimal number. The choice should be input by user.

#include <stdio.h>
#include <conio.h>
void main ( )
{            
        int          no, cho, md[50], c, i;
        clrscr ( );
                  
        while ( 1 )
        {                 
               printf ("\n\t\t\t\t MENU");
               printf ("\n\t\t\t\t ====");
               printf ("\n\n\t\t   1. CONVERT FROM DECIMAL TO BINARY\n");
               printf ("\t\t   2. CONVERT FROM DECIMAL TO OCTAL \n");
               printf ("\t\t   3. CONVERT FROM DECIMAL TO HEXADECIMAL \n");
               printf ("\t\t   4. EXIT \n");
               
               printf ("\n\n\n\t\t   Your Choice ? "); scanf ("%d", &cho);
                           
               switch ( cho )
               {             
                     case 1:
                                 printf ("\n\n\t\t Enter a decimal number.......:"); scanf ("%d",&no);
                                 c = 0;
                                 while ( no > 0 )
                                 {            
                                          md[c] = no % 2;
                                          no = no / 2;
                                          c++;
                                 }
                                 printf ("\n\t\t BINARY NUMBER = ");
                                 for ( i = c - 1; i >= 0; i-- )
                                 {                     
                                          printf ("%d", md[i] );
                                 }
                                 break;
                          
                     case 2:       
                                 printf ("\n\n\t\t Enter the number.......:"); scanf ("%d",&no);
                                 c = 0;
                                 while ( no > 0 )
                                 {
                                          md[c] = no % 8;
                                          no = no / 8;
                                          c++;
                                 }
                                 printf ("\n\t\t OCTAL NUMBER = ");
                                 for ( i = c - 1; i >= 0; i-- )
                                 {              
                                          printf ("%d", md[i] );
                                 }
                                 break;
                    
                     case 3:      
                                 printf ("\n\n\t\t Enter the number.......:"); scanf ("%d",&no);
                                 c = 0;
                                 while ( no > 0 )
                                 {                    
                                          md[c] = no % 16;
                                          no = no / 16;
                                          c++;
                                 }
                                 printf ("\n\t\t HEXADECIMAL NUMBER = ");
                                 for ( i = c - 1; i >= 0; i-- )
                                 {            
                                          if ( md[i] > 9 )
                                          {                       
                                                      printf ("%c", md[i] + 55 );
                                          }
                                          else
                                          {
                                                      printf ("%d", md[i] );
                                          }
                                 }
                                 break;
                    
                     case 4:
                                 exit ( );
                                              
                     default:
                                 printf("\n WRONG CHOICE.");
              }
              getch ( );
              clrscr ( );
       }

}
Click here to contact us

Sunday, 26 August 2012

Case structure005

Create a menu driven program.

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

void main ( )
{
             int          cho, no, no1, co, md, fl, s;
             unsigned long    fact;
             clrscr ( );
                                                 
             while ( 1 )
             {                                                    
                      printf ("\n\t\t\tMENU");
                      printf ("\n\t\t\t====");
                      printf ("\n\n\t\t   1. FACTORIAL\n");
                      printf ("\t\t   2. PRIME \n");
                      printf ("\t\t   3. ARMSTRONG \n");
                      printf ("\t\t   4. ODD / EVEN \n");
                      printf ("\t\t   5. EXIT \n");
                                                                           
                      printf ("\n\t\t   Your Choice ?"); scanf ("%d", &cho);
                                                                                       
                      switch ( cho )
                      {                                           
                                 case 1:
                                               fact = 1;
                                               printf ("\n\t Enter the number.......:"); scanf ("%d",&no);
                                                                                                    
                                               for ( co =1; co <= no; co++ )
                                               {                                             
                                                                 fact = fact * co;
                                               }
                                               printf ("\n\t FACTORIAL = %ld", fact);
                                               break;
                                                                
                                 case 2:                    
                                               fl = 0;
                                               printf ("\n\t Enter a number...........:"); scanf ("%d", &no);
                                                                                                                    
                                               for ( co = 2; co < no; co++ )
                                               {                                                        
                                                                  md = no % co;
                                                                  if ( md == 0 )
                                                                  {                                                    
                                                                                fl = 1;
                                                                                break;
                                                                  }
                                               }
                                               if ( fl == 0 )
                                                         printf ("\n\t It is a Prime number");
                                               else                              
                                                         printf ("\n\t It is not a Prime number");
                                               break;
                                                         
                                 case 3:                                
                                               s = 0;
                                               printf ("\n\t Enter a number.........:"); scanf ("%d",&no);
                                               no1 = no;
                                                                                                      
                                               while ( no > 0 )
                                               {                                                      
                                                                 md = no % 10;
                                                                 no = no /10;
                                                                 s = s + ( md * md * md );
                                               }                                                         
                                               if ( no1 == s )
                                                             printf ("\n\t Number = %d is an Armstrong number", no1);
                                               else           
                                                             printf ("\n\t Number = %d is not an Armstrong number",
                                                                              no1);
                                               break;
                                                                      
                                 case 4:      
                                               printf ("\n\t Enter a number.........:"); scanf ("%d",&no);
                                                                            
                                               if ( no % 2 == 0 )
                                                                  printf ("\n\t Even number");
                                               else                                                    
                                                                  printf ("\n\t Odd number");
                                               break;
                                                             
                                 case 5:    
                                               exit ( );
                                                             
                                 default:           
                                               printf("\n WRONG CHOICE.");
                      }
                      getch ( );
                      clrscr ( );
             }

}



Click here to contact us

Saturday, 4 August 2012

Case structure004


Input two numbers from users and a choice among the following numbers.
             1->Add two numbers
             2->Subtract two numbers
             3->multiply two numbers
             4->divide two numbers

#include <stdio.h>
#include <conio.h>
void main ( )
{
        int        n1,n2,ch,r,d;
        clrscr ( );
       
        printf ("\n Enter two numbers..:"); scanf ("%d%d",&n1,&n2);
        printf ("\n Press 1 to add.");
        printf ("\n Press 2 to subtract.");
        printf ("\n Press 3 to multiply.");
        printf ("\n Press 4 to divide.");
        printf ("\n Enter the choice..:");
        scanf ("%d",&ch);
       
        switch ( ch )
        {                          
                   case 1:
                           r = n1 + n2;
                           printf ("\n Result of addition %d",r);
                           break;
                   case 2:
                           r = n1- n2;
                           printf ("\n Result of subtraction %d",r);
                           break;
                   case 3:
                           r = n1 * n2;
                           printf ("\n Result of multiplication %d",r);
                           break;
                   case 4:
                           r = n1 / n2;
                           d = n1 % n2;
                           printf ("\n Result of divition:\n Quotient=%d\tRemainder=%d",r,d);
                           break;
                   default:
                           printf("\n Invalid choice.");
        }
        getch();
}

Click here to contact us


Case structure003


Input the number of month (1 -12) and print the name of the month.


#include <stdio.h>
#include <conio.h>
void main ( )
{
        int         mon;
        clrscr ( );
        
        printf ("\n Enter the number of month..:"); scanf ("%d",&mon);
         
        switch ( mon )
        {                      
                  case 1:
                          printf ("\n JANUARY");
                          break;
                  case 2:
                          printf ("\n FEBRUARY");
                          break;
                  case 3:
                          printf ("\n MARCH");
                          break;
                  case 4:
                          printf ("\n APRIL");
                          break;
                  case 5:
                          printf ("\n MAY");
                          break;
                  case 6:
                          printf ("\n JUNE");
                          break;
                  case 7:
                          printf ("\n JULY");
                          break;
                  case 8:
                          printf ("\n AUGUST");
                          break;
                  case 9:
                          printf ("\n SEPTEMBER");
                          break;
                  case 10:
                          printf ("\n OCTOBER");
                          break;
                  case 11:
                          printf("\n NOVEMBER");
                          break;
                  case 12:
                          printf("\n DECEMBER");
                          break;
                  default:
                          printf("\n Wrong input.");
        }
        getch();
}

Case structure002

Input the number of day and print the day of week.


#include <stdio.h>
#include <conio.h>
void main ( )
{
        int         day;
        clrscr ( );
        
        printf ("\n Enter the number of day in the week (1-7):"); scanf ("%d",&day);
        
        switch ( day )
        {                                         
                  case 1:
                          printf ("\n MONDAY");
                          break;
                  case 2:
                          printf ("\n TUESDAY");
                          break;
                  case 3:
                          printf ("\n WEDNESDAY");
                          break;
                  case 4:
                          printf ("\n THURSDAY");
                          break;
                  case 5:
                          printf ("\n FRIDAY");
                          break;
                  case 6:
                          printf ("\n SATERDAY");
                          break;
                  case 7:
                          printf ("\n SUNDAY");
                          break;
                  default:
                          printf ("\n Wrong input.");
        }
        getch ( );
}


Click here to contact us

Friday, 3 August 2012

Case structure001


Input a number between 1 to 5 and print the number in word. Give appropriate error message if the number is wrong.


#include <stdio.h>
#include <conio.h>
void main ( )
{
        int         num;
        clrscr ( );
        
        printf ("\n Enter the number between 1 to 5.:"); scanf ("%d",&num);
        
        switch ( num )
        {                       
                  case 1:            
                         printf ("\nONE");
                         break;
                  case 2:
                         printf ("\nTWO");
                         break;
                  case 3:
                         printf ("\nTHREE");
                         break;
                  case 4:
                         printf ("\nFOUR");
                         break;
                  case 5:
                         printf ("\nFIVE");
                         break;
                  default:
                         printf ("\n Out of range.");
        }
        getch ( );
}


Related Posts Plugin for WordPress, Blogger...