Wednesday, 9 March 2016

ACCEPT TWO NUMBERS IN VARIABLES X & Y FROM THE USER AND PERFORM THE FOLLOWING

OPTIONS                                          |                ACTIONS
1.EUALITY                                      |                check if x =y
2.LESS THAN                                   |               check if x<y
3.QUOTIENT & REMAINDER      |                divide x/y display que and remainder
4.RANGE                                           |               accept a no check if it lies between x and y
5.SWAP                                            |                interchange x & y

#include<stdio.h>
main()
{
int x,y,ch,n,temp;
printf("Enter two nos.\n");
scanf("%d%d",&x,&y);
printf("Enter your choice \n");
printf(" 1. Equality \n 2.Less than \n 3.Quotient and Remainder \n 4.Range \n 5.Swap ");
scanf("%d",&ch);
switch(ch)
{
case 1: if(x==y)
        printf("%d and %d are equal \n",x,y);
        else
        printf("%d and %d are not equal\n",x,y);
        break;
case 2:if(x>y)
        printf("%d is less than %d\n",y,x);
        else if(y>x)
        printf("%d is less than %d\n",x,y);
        else if(x==y)
        printf("%d and %d are equal\n");
        break;
case 3:printf("Quotient=%d\n",x/y);
       printf("Remainder=%d\n",x%y);
       break;
case 4:printf("Enter a no.\n");
       scanf("%d",&n);
       if((x<=n && y>=n) || (x>=n && y<=n))
       printf("%d lies between %d and %d\n",n,x,y);
       else
       printf("%d does not lie between %d and %d\n",n,x,y);
       break;
case 5: printf("Value of x before swapping= %d\n",x);
       printf("Value of y before swapping= %d\n",y);
       temp=x;
       x=y;
       y=temp;
       printf("Value of x after swapping= %d\n",x);
        printf("Value of y after swapping= %d\n",y);
       break;
default:printf("Invalid choice\n");
}
}


ASSIGNMENT 3)

TO DEMONSTRATE DECISION MAKING STATEMENTS (SWITCH CASE)

You should read following topics before starting this exercise

1. Different types of decision-making statements available in C.
2. Syntax for switch case statements.

The control statements that allows us to make a decision from the number of choices is called a switch-case statement. It is a multi-way decision making statement.

i) Usage of switch statement
   
    Syntax:  switch(expression)
                    {
                   case value1: block1;
                      break;
                   case value2: block2;
                      break;
                         . 
                         . 
                    default: default block;
                      break;
                   }
ii) The switch statement is used in writing menu driven programs here a menu displays several options and the user gives the choice by typing a character or number . 

No comments:

Post a Comment