Saturday, June 19, 2021

Basic questions and answers of C program using if-else and switch Statement

 Basic questions and answers of C program using if-else and switch Statement



Here we will solve questions on if-else and switch statement

Question Write a program to calculate gross salary.

Answer

#include<stdio.h>

int main() 

{

float basic,gross_salary,da,hra;

printf("Enter the basic_salary");

scanf("%f",&basic);

if(basic<=10000){

    da=basic*0.08;

    hra=basic*0.02;

    

}  if(basic>10000 && basic<=20000){

    da=basic*0.16;

    hra=basic*0.12;

    

} if(basic>20000){

    da=basic*0.10;

    hra=basic*0.12;

    

}

gross_salary=basic+da+hra;

    printf("The gross salary=%f\n", gross_salary);

return 0;

}

OUTPUT:

The output of the gross salary program 

Question  Write a program to check whether a number is Even or Odd?

Answer

#include<stdio.h>

int main()

{

int a;

printf(“Enter a number\n”);

scanf(“%d”,&a);

            if (a%2==0){

                        printf(“%d is even”,a);

            }

            else{

                        printf(“%d is odd”,a);

            }

Return 0;

}

OUTPUT:

The output of even and the odd program

Question Write a program to check whether the given year is a leap year or not.

Answer

#include<stdio.h>

int main()

{

int year;

            printf(“Enter year:\n”);

            scanf(“%d”,&year);

            if(((year%4==0)&&(year%100!=0))||(year%400==0)){

                        printf(“d year is leap year”,year);

            }

            else{

                        Printf(“%d year is not a leap year”,year);

            }

Return 0;

}

OUTPUT:

The output of a leap year program

Question Write a program to check whether a given alphabet is vowel or not using a switch case. 

Answer

#include<stdio.h>

 

int main(){

    char alphabet;

    printf("Enter the charecter\n");

    scanf("%c",&alphabet);

    switch(alphabet){

        case 'a':

        case 'e':

        case 'i':

        case 'o':

        case 'u':

        case 'A':

        case 'E':

        case 'I':

        case 'O':

        case 'U':

        printf("It is a vowel");

        break;

       default:

       printf("It is not a vowel");

    }

return 0;

}

OUTPUT:

The output of  finding vowel program 

Question. Write a program to make a calculator using a switch-case statement?

Answer 

#include<stdio.h>

int main(){
char operator;
float a,b;
printf("Operator is \n ");
scanf("%c",&operator);
printf("Enter value of a \n");
scanf("%f",&a);
printf("Enter value of b\n ");
scanf("%f",&b);
switch(operator){
    case '+':
    printf("%f+%f=%f",a,b,a+b);
    break;
    case '-':
    printf("%f-%f=%f",a,b,a-b);
    break;
    case '*':
    printf("%f*%f=%f",a,b,a*b);
    break;
    case '/':
    printf("%f/%f=%f",a,b,a/b);
    break;
}
return 0;
}

Its output is

The output of the switch-calculator

No comments:

Post a Comment

Please do not enter any spam link in the comment box

Best of Our Website

Popular Posts