Translate

Showing posts with label else if ladder. Show all posts
Showing posts with label else if ladder. Show all posts

Wednesday, May 13, 2020

C program to make a calculator using the switch statement.

/*perform arithmetic calculation on integers in C language*/
#include<stdio.h>
#include<conio.h>
void main()
{
    char op;
    int a, b;
    printf("enter an Operator = ");
    scanf("%c",&op);
     printf("enter first number in = ");
    scanf("%d",&a);
     printf("enter second number in = ");
    scanf("%d",&b);
    switch(op)
    {
        case '+':
        printf("Result is = %d\n", a+b);
        break;
         case '-':
        printf("Result is = %d\n", a-b);
        break;
         case '*':
        printf("Result is = %d\n", a*b);
        break;
         case '/':
        printf("Result is = %d\n", a%b);
        break;
        default:
         printf("Oops TRY AGAIN!!!");
    }
        getch();
        
    }
Output:


    Click Here to Switch statement
    Click here  to All Important Questions with Answers.
    Click Here to All C Program.


Tuesday, May 12, 2020

check whether a given number is even or odd? If the number is an even number, print it's square.


C program to check whether a given number is even or odd? if the number is even number, print it's square...

#include<stdio.h>
#include<conio.h>
void main()
{
    int num, squ;
    printf("enter number = ");
    scanf("%d",& num);
    if(num%2==0)
       { 
           printf("The number is Even");
           squ = num*num;
           printf("\nsquare of this even number is %d", squ);
       }
          
    else 
         printf("The number is Odd");
         
    getch();


}
Output:






Click Here for, All Important Questions with Answers
Click here for, C Programs


Switch Versus if-else Ladder

Q. What is the difference between the Switch statement and if- else ladder statements?

S.No.

Switch statement

If- else-if ladder statements


Instead of using the if-else-if Ladder control statement, the Switch Statement is available in program C for Handling multiple choices. In Switch statement three keywords are used switch, case, and default. we can use integer. constant. but we can not use floating point and string constants.

 

This is a type of nesting in which there is an if...else statement in every else part except the last else part. This type nesting is called the else if Ladder Statement.

 

 



 

 


Syntax:

Switch(expression)

{

  case constant 1:

  statement;

  break;

  case constant 2:

  statement;

  break;

  case constant 3:

  statement;

  break;

  default;

  statement

}

 

Syntax:

if(Condition 1)

       Statement A;

else if(Condition 2)

         Statement B;

 else if(Condition 3)

              Statement C;

  else if(Condition 4)

                Statement C;

       else

                 Statement D;

 

 

3.

It’s also called cases control statement.

It’s also called loader if statement.

 

4.

In switch statements we use switch keyword.

Here we use else if keywords.

 

5.

It is easy to use & understand.

It is complex statements.

 

6.

It can have different cases for different conditions.

But there is no different cases for different conditions.

7.

In this statement, we can not use relational & logical operators.

In this we can use relational & logical statements.

 

8.

#include<stdio.h>

#include<stdio.h>

void main()

{

     int ch;

    printf("\n1. press ch=1 for monday. \n2. press ch=2 for tuesday.\n3. press ch=3 for wednesday. \n4.press ch=4 for thursday. \n5.press ch=5 for friday.\n6.press ch=6 for saturday. \n7. press ch=7 for sunday.");

 

    printf("\n enetr your choice =");

    scanf("%d", &ch);

   

 switch(ch)

     {

 

        case 1:

        printf("today is monday");

        break;

        

        case 2:

        printf("today is tuesday");

        break;

        

        case 3:

        printf("today is wednesday");

        break;

  

        case 4:

        printf("today is thursday");

        break;

        

        case 5:

        printf("today is friday");

        break;

  

        case 6:

        printf("today is saturday");

        break;

        

        case 7:

        printf("today is sunday");

        break;

        

        default:

        printf("wrong choice Try Again!!!");

     }

        

    getch();

 

}

 

Output:

Click Here, Switch Statement 


#include<stdio.h>

#include<stdio.h>

void main()

{

    int ch;

    

    printf("\n1. press ch=1 for monday. \n2. press ch=2 for tuesday.\n3. press ch=3 for wednesday. \n4.press ch=4 for thursday. \n5.press ch=5 for friday.\n6.press ch=6 for saturday. \n7. press ch=7 for sunday.");

    printf("\n enetr your choice =");

    scanf("%d", &ch);

    if(ch==1)

    {

        printf("today is monday");

    }

  else if(ch==2)

    {

       printf("today is tuesday");

    }

 else if(ch==3)

    {

       printf("today is wednesday");

    }

 else if(ch==4)

    {

        printf("today is thursday");

    }

  else if(ch==5)

    {

        printf("today is friday");

    }

  else  if(ch==6)

    {

        printf("today is saturday");

    }

  else if(ch==7)

    {

        printf("today is sunday");

    }

    getch();

    

}

Output:

 


 





Click Here, else-if Ladder


Click here for, All Important Questions with Answers

Tuesday, May 5, 2020

C Program to Print Day Name of Week using Else If ladder

#include<stdio.h>
#include<stdio.h>
void main()
{
    int ch;
    
    printf("\n1. press ch=1 for monday. \n2. press ch=2 for tuesday.\n3. press ch=3 for wednesday. \n4.press ch=4 for thursday. \n5.press ch=5 for friday.\n6.press ch=6 for saturday. \n7. press ch=7 for sunday.");
    printf("\n enetr your choice =");
    scanf("%d", &ch);
    if(ch==1)
    {
        printf("today is monday");
    }
  else if(ch==2)
    {
       printf("today is tuesday");
    }
 else if(ch==3)
    {
       printf("today is wednesday");
    }
 else if(ch==4)
    {
        printf("today is thursday");
    }
  else if(ch==5)
    {
        printf("today is friday");
    }
  else  if(ch==6)
    {
        printf("today is saturday");
    }
  else if(ch==7)
    {
        printf("today is sunday");
    }
    getch();
    
}
Output:

Friday, May 1, 2020

Program How to calculate Gross salary of employee

Calculate Gross Salary

Write a program to calculate gross salary of employee, basic salary is enter by the user, da is calculated on the basic of the following criteria.


bsal da
below 5000 10% of bsal
5000-10000 15% of bsal
10000-20000 20% of bsal
20000-above 40% of bsal

#include<stdio.h>
#include<conio.h>
void main()
{
    int bsal, da, gsal;
    printf("enter bsal = ");
    scanf("%d",& bsal);
    if(bsal<5000)
       da=(bsal*0.10);
    
    if((bsal>=5000)&&(bsal<10000))
       da=(bsal*0.15);
       
    if((bsal>=10000)&&(bsal<20000))
       da=(bsal*0.20);
       
    if(bsal>=20000)
       da=(bsal*0.40);
       
       gsal=bsal+da;
       printf("Gross salary of employee is= %d",gsal);
       getch();
       
}
Output:




Sunday, April 26, 2020

C Program to find the Largest Number among three numbers

/* Print the Largest Number*/

#include<stdio.h>
#include<conio.h>
void main()
{
   int a, b, c;
 printf("enter value in a = ");
 scanf("%d",& a);
 printf("enter value in b = ");
 scanf("%d",& b);
 printf("enter value in c = ");
 scanf("%d",& c);

  if(a>b)
     {
         if(a>c)
              printf("\na is Largest %d", a);
           else
              printf("\nc is Largest %d", c);
     }
     else
     {
         if(b>c)
            printf("\nb is Largest %d",b);
           else
            printf("\nc is Largest %d",c);
     }
    getch();

}
Output:




Saturday, April 25, 2020

The else if ladder

The else if ladder

/*Control Statement: else if ladder*/


  • The else if Ladder Statement:
This is a type of nesting in which there is an if...else statement in every else part except the last else part. This type nesting is called the else if Ladder Statement.

The Syntax of else if Ladder:

if(Condition 1)
       Statement A;
else if(Condition 2)
         Statement B;
 else if(Condition 3)
              Statement C;
  else if(Condition 4)
                Statement C;
       else
                 Statement D;

The Example of else if Ladder :

#include<stdio.h>
#include<conio.h>
void main()
{
    float per;
  
    printf("Enter your percentage = ");
    scanf("%f",&per);
    if(per>=85)
    printf( "grade B");
    else if(per>=70)
    printf( "grade B");
    if(per>=55)
    printf( "grade ='C'");
    else if(per>=40)
    printf( "grade D");
    else
    printf("Fail");
    getch();
       
}

Output:





C program to make a calculator using the switch statement.

/*perform arithmetic calculation on integers in C language*/ #include<stdio.h> #include<conio.h> void main() {     char op;     ...