In this article, We will learn writing a program using decision making statement. We will learn to use if else statements and switch case.
- Write a program to input a number and check whether it is odd or even.
#include<stdio.h>
int main()
{
int a;
printf("Enter a number : ");
scanf("%d",&a);
if (a%2==0)
{
printf("Even");
}
else
{
printf("Odd");
}
return 0;
}
Enter a number : 5
Odd
2. Write a program to input 3 numbers and find the middle number.
#include<stdio.h>
int main()
{
float a,b,c;
printf("Enter any three numbers : ");
scanf("%f%f%f",&a,&b,&c);
if (a>b && a<c || a<b && a>c)
{
printf("Middle Number : %f",a);
}
else if (b>a && b<c || b<a && b>c)
{
printf("Middle Number : %f",b);
}
else
{
printf("Middle Number : %f",c);
}
return 0;
}
Enter any three numbers : 5 4 3
Middle Number : 4.000000
3. Write a program to input electricity units and find the billing amount as follows:
Electricity Units | Price Per Unit |
For First 20 Units | Rs. 0 |
For Next 100 Units | Rs. 12 |
For Next 100 Units | Rs. 11 |
For Above 220 Units | Rs. 10 |
#include<stdio.h>
int main()
{
int unit,amt;
printf("Enter the electricity units consumed : ");
scanf("%d",&unit);
if (unit<=20)
{
amt=0;
}
else if (unit<=120)
{
amt=0+12*(unit-20);
}
else if (unit<=220)
{
amt=0+12*100+11*(unit-120);
}
else
{
amt=0+12*100+11*100+10*(unit-220);
}
printf("Total Billing Amount : Rs. %d",amt);
return 0;
}
Enter the electricity units consumed : 235
Total Billing Amount : Rs. 2450
4. Write a program to check vowel / consontant.
#include<stdio.h>
int main()
{
char a;
printf("Enter a letter :");
scanf("%c",&a);
switch(a)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u': printf("Vowel");
break;
default : printf("Consonant");
}
return 0;
}
Enter a letter :u
Vowel
5. Write a program to input two numbers and then input the operator (+ , – , * , /) and find the result using switch.
#include<stdio.h>
int main()
{
int a,b,d;
char c;
printf("Enter any two numbers : ");
scanf("%d%d",&a, &b);
printf("Enter operator (+, -, *, /):");
scanf(" %c",&c); /* Space before %c */
switch(c)
{
case '+':
printf("Sum is : %d ",a+b);
break;
case '-':
printf("Difference is : %d ",a-b);
break;
case '*':
printf("Product is : %d ",a*b);
break;
case '/':
printf("Division is : %d ",a/b);
break;
default : printf("Enter Again");
}
return 0;
}
Enter any two numbers : 15 5
Enter operator (+, -, *, /):/
Division is : 3
6. Write a program to create a menu driven program to add / subtract / multiply / divide two numbers using switch.
#include<stdio.h>
int main()
{
int a,b,c,choice;
printf("Enter the two numbers:");
scanf("%d%d",&a,&b);
printf("1. Add \n 2. Subtract \n 3. Multiply \n 4. Divide : \n");
printf("Enter your choice:");
scanf("%d",&choice);
switch (choice)
{
case 1: c=a+b;
printf("The result is : %d",c);
break;
case 2: c=a-b;
printf("The result is : %d",c);
break;
case 3: c=a*b;
printf("The result is : %d",c);
break;
case 4: c=a/b;
printf("The result is : %d",c);
break;
default:printf("Enter again:");
}
return 0;
}
Enter the two numbers:2 4
1. Add
2. Subtract
3. Multiply
4. Divide :
Enter your choice:3
The result is : 8