Sunday 23 April 2023

Exercise 3-To demonstrate decision making statements (switch case)



SET A

1. Accept a single digit from the user and display it in words. For example, if digit entered is 9,
display Nine.


#include<stdio.h>
#include<conio.h>
void main()
{
 int n;
 clrscr();
 printf("\nEnter the number:");
 scanf("%d",&n);
 switch(n)
      {
    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;
    case 6:printf("\nSix");
           break;
    case 7:printf("\nSeven");
           break;
    case 8:printf("\nEight");
           break;
    case 9:printf("\nNine");
           break;
       default:printf("\nInvalid Output");
      }
 getch();
}
/*
output:

Enter the number:9                                                             

Nine
*/
2. Write a program, which accepts two integers and an operator as a character (+ - * /),
performs the corresponding operation and displays the result.


#include<stdio.h>
#include<conio.h>
void main()
{
 int n1,n2;
 char op;
 clrscr();
 printf("\nEnter 2 numbers:");
 scanf("%d%d",&n1,&n2);
 printf("\n+.Addition\n-.Substration\n*.Multiplication\/.Division\5Exit");
 printf("\nEnter your choise");
 scanf(" %c",&op);
 switch(op)
       {
    case '+':printf("\nAddition is %d",n1+n2);
         break;
    case '-':printf("\nSubstration is %d",n1-n2);
         break;
    case '*':printf("\nMultiplication is %d",n1*n2);
         break;
    case '/':printf("\nDivision is %d",n1/n2);
         break;
       }
 getch();
}
/*
Enter 2 numbers:5                                                              
4                                                                              
                                                                               
+.Addition                                                                     
-.Substration                                                                  
*.Multiplication/.Division Exit                                                
Enter your choise  +                                                           
                                                                               
Addition is 9                                                                  
 */

3. Accept two numbers in variables x and y from the user and perform the following operations
Options Actions
1. Equality Check if x is equal to y
2. Less Than Check if x is less than y
3. Quotient and Remainder Divide x by y and display the quotient and remainder
4. Range Accept a number and check if it lies between x and y
(both inclusive)
5. Swap Interchange x and y


#include<stdio.h>
#include<conio.h>
void main()
{
 int n1,n2,ch,q,r,t;
 clrscr();
 printf("\nEnter 2 numbers:");
 scanf("%d%d",&n1,&n2);
 printf("\n1.Equality\n2.Less than\n3..Quotient and remainder\n4.Range\n5.Swap");
 printf("\nEnter your choise");
 scanf(" %d",&ch);
 switch(ch)
      {
    case 1:if(n1==n2)
          printf("\n %d equals to %d",n1,n2);
           else
          printf("\n %d not equal to %d",n1,n2);
           break;
    case 2:if(n1<n2)
          printf("\n %d less than%d",n1,n2);
           else
          printf("\n %d not less than %d",n1,n2);
           break;
    case 3:q=n1/n2;
           printf("\nQuotient is %d",q);
           r=n1%n2;
           printf("\nRemainder is %d",r);
           break;
    case 4:printf("\nEnter number :");
           scanf("%d",&t);
           if(t>n1 && t<n2)
          printf("\n %d is between %d and %d",r,n1,n2);
           else
          printf("\n %d is  not between %d and %d",r,n1,n2);
           break;
    case 5:printf("\nBefore swap no is");
           printf("First no is:%d",n1);
           printf("Second no is %d",n2);
           t=n1;
           n1=n2;
           n2=t;
           printf("\After swap no is");
           printf("First no is:%d",n1);
           printf("Second no is %d",n2);
           break;
       default:printf("\nInvalid choise:");
      }
 getch();

SET B

€ 1. Accept radius from the user and write a program having menu with the following options and
corresponding actions
Options Actions
1. Area of Circle Compute area of circle and print
2. Circumference of Circle Compute Circumference of circle and print
3. Volume of Sphere Compute Volume of Sphere and print

#include<conio.h>
#include<stdio.h>
void main()
{
 int op;
 float r,ans;
 clrscr();
 printf("\nEnter redius:");
 scanf("%f",&r);
 printf("\n1.Area of circle\n2.Circumference of circle\n3.Volume of Shere");
 printf("\nEnter your choice:");
 scanf("%d",&op);
 switch(op)
       {
     case 1:ans=3.14*r*r;
        printf("\nArea of circle is %f",ans);
        break;
     case 2:ans=2*3.14*r;
        printf("\nCircumference of circcle is %f",ans);
        break;
     case 3:ans=4/3*3.14*r*r*r;
        printf("\nVolume of Shere is %f",ans);
        break;
    default:printf("\nInvalid input:");
       }

 getch();
}
/*

Enter redius:3.2                                                               
                                                                               
1.Area of circle                                                               
2.Circumference of circle                                                      
3.Volume of Shere                                                              
Enter your choice:1                                                            
                                                                               
Area of circle is 32.153603                                                    


Enter redius:4                                                                 
                                                                               
1.Area of circle                                                               
2.Circumference of circle                                                      
3.Volume of Shere                                                              
Enter your choice:2                                                            
                                                                               
Circumference of circcle is 25.120001                                          


Enter redius:2.5                                                               
                                                                               
1.Area of circle                                                               
2.Circumference of circle                                                      
3.Volume of Shere                                                              
Enter your choice:3                                                            
                                                                               
Volume of Shere is 49.062500                                                   
*/

2. Write a program having a menu with the following options and corresponding actions
Options Actions
1. Area of square Accept length ,Compute area of square and print
2. Area of Rectangle Accept length and breadth, Compute area of rectangle
and print
3. Area of triangle Accept base and height , Compute area of triangle and
print


#include<stdio.h>
#include<conio.h>
void main()
{
 int op;
 float l,b,h,ans;
 clrscr();
 printf("\n1.Area of square\n2.Area of Rectangle\n3.Area of triangle");
 printf("\nEnter your choise:");
 scanf("%d",&op) ;
 switch(op)
       {
     case 1:printf("\nEnter length :");
        scanf("%f",&l);
        ans=l*l;
        printf("\nArea of square is %f",ans);
        break;
     case 2:printf("\nEnter length :");
        scanf("%f",&l);
        printf("\nEnter breadth:");
        scanf("%f",&b);
        ans=l*b;
        printf("\nArea of Rectangle is %f",ans);
        break;
     case 3:printf("\nEnter base:");
        scanf("%f",&b);
        printf("\nEnter height:");
        scanf("%f",&h);
        ans=1.0/2.0*b*h;
        printf("\nArea of square is %f",ans);
        break;

       }
 getch();
}
/*                                                                               
1.Area of square                                                               
2.Area of Rectangle                                                            
3.Area of triangle                                                             
Enter your choise:1                                                            
                                                                               
Enter length :4                                                                
                                                                               
Area of square is 16.000000                                                    
1.Area of square                                                               
2.Area of Rectangle                                                            
3.Area of triangle                                                             
Enter your choise:2

Enter length :6

Enter breadth:4

Area of Rectangle is 24.000000

1.Area of square                                                               
2.Area of Rectangle                                                            
3.Area of triangle                                                             
Enter your choise:3                                                            
                                                                               
Enter base:4                                                                   
                                                                               
Enter height:6                                                                 
                                                                               
Area of square is 12.000000


Set C
€ 1. Accept the three positive integers for date from the user (day, month and year) and check
whether the date is valid or invalid. Run your program for the following dates and fill the table.
(Hint: For valid date 1<=month<=12,1<= day <=no-of-days where no-of-days is 30 in case of
months 4, 6,9 and 11. 31 in case of months 1,3,5,7,8,10 and 12. In case of month 2 no-of-days is
28 or 29 depending on year is leap or not)
Date Output
12-10-1984
32-10-1920
10-13-1984
29-2-1984
29-2-2003
29-2-1900


#include<stdio.h>
#include<conio.h>
void main()
{
 int d,m,y;
 clrscr();
 printf("\nEnter date,month,year");
 scanf("%d%d%d",&d,&m,&y);
 switch(m)
       {
    case 1 :
    case 3 :
    case 5 :
    case 7 :
    case 8 :
    case 10:
    case 12:if(d>0 && d<=31)
          printf("\nValid date");
        else
          printf("\nInvalid date");
        break;
    case 4 :
    case 6 :
    case 9 :
    case 11:if(d>0 && d<31)
          printf("\nValid date");
        else
          printf("\nInvalid date");
        break;
        break;
    case 2 :if(y%4==0)
          {
           if(d>0 &&d<=29)
             printf("\nValid Date");
           else
             printf("\nInvalid date");
          }
        else
          {
           if(d>0&&d<=28)
             printf("\nValid date");
           else
             printf("\nInvalid date");
          }
        break;
    default:printf("\nInvalid month");

       }
 getch();
}
/*
Enter date,month,year29
11
1991

Valid date


Enter date,month,year31
4
2017

Invalid date

Enter date,month,year                                                          
30                                                                             
2                                                                              
1000                                                                           
                                                                               
Invalid date                                                                   
  */

2. Write a program having menu that has three options - add, subtract or multiply two fractions.
The two fractions and the options are taken as input and the result is displayed as output. Each
fraction is read as two integers, numerator and denominator
.

#include<stdio.h>
#include<conio.h>
void main()
{
 float n1,d1,n2,d2,ans;
 int op;
 clrscr();
 printf("\nEnter first fraction number:");
 printf("\nEnter numerator and denominator:");
 scanf("%f%f",&n1,&d1);
 printf("\nEnter Second fraction number:");
 printf("\nEnter numerator and denominator:");
 scanf("%f%f",&n1,&d1);
 printf("\n1.Addition\n2.Substract\n3.Multiply ")
 printf("\nEnter your choise:");
 scanf("%d",&op);
 switch(op)
       {
    case 1:
           break;
    case 1:
           break;
    case 1:
           break;
       default:printf("\nInvalid choise:");
       }
 getch();
}

No comments:

Post a Comment

Questions

  Exercise 1 Set A . Apply all the three program development steps for the following examples.  1. Accept dimensions of a cylinder and p...