You are on page 1of 13

Exercise 3 DECISION MAKING USING IF AND IF-ELSE

Q1) Write a Program to accept an integer and check if it is even or odd.

include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
// True if num is perfectly divisible by 2
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
}
2
Q2) Accept a character as input and check whether the character is a
digit.(Check if it is in the range ‘0’ to ‘9’ both inclusive)
#include<stdio.h>
#include<ctype.h>
int main()
{
char TestChar;
printf("\n Check whether a character is digit or not :\n");
printf("----------------------------------------------\n");
printf(" Input a character : ");
scanf( "%c", &TestChar );
if( isdigit(TestChar) )
printf( " The entered character is a digit. \n\n" );
else
printf( " The entered character is not a digit. \n\n" );
return 0;
}

3
Q3) Write a program, which accepts annual basic salary of an employee and calculates and displays
the Income tax as per the following rules.
Basic: < 1,50,000 Tax = 0 %
1,50,000 to 3,00,000 Tax = 20% >
3,00,000 Tax = 30%

#include <stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
float salary,tax;
printf("enter the emp basic salary :");
scanf("%f",&salary);
if(salary<=150000)
{
tax=0;
printf("tax = %f",tax);
}
else if(salary>150000 && salary<=300000)
{
tax=(salary*0.2);
printf("tax = %f",tax);
}
else if(salary>300000)
{

4
tax=(salary*0.3);
printf("tax = %f",tax);
}
}

Q4) Accept a character from the user and check whether the character is a
vowel or consonant . (Hint: a,e,i,o,u,A,E,I,O,U are vowels)

#include <stdio.h>
int main() {
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);

// evaluates to 1 if variable c is a lowercase vowel


lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

// evaluates to 1 if variable c is a uppercase vowel


uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

// evaluates to 1 (true) if c is a vowel


if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);

5
else
printf("%c is a consonant.", c);
return 0;
}

OUTPUT 1 :

OUTPUT 2:

Q5) Accept any year as input through the keyboard . Write a Program to
check whether the year is a leap year or not. ( Hint : leap year is divisible
by 4 and not by 100 or divisible by 400)

#include <stdio.h>
int main() {

6
int year;
printf("Enter a year: ");
scanf("%d", &year);

// leap year if perfectly visible by 400


if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if visible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap year
else {
printf("%d is not a leap year.", year);
}

return 0;
}

7
OUTPUT 1 :

OUTPUT 2:

Q6) Write a program to check whether given character is a digit or a


character in lowercase or uppercase alphabet. ( HINT : ASCII value of a
digit is between 48 to 58 and lowercase characters have ASCII values in the
range of 97 to 122 , uppercase is between 65 to 90)

#include <stdio.h>
int main()
{
char ch;
printf("Enter any character: ");
scanf("%c", &ch);
if(ch >= 'A' && ch <= 'Z')
{

8
printf("'%c' is uppercase alphabet.", ch);
}
else if(ch >= 'a' && ch <= 'z')
{
printf("'%c' is lowercase alphabet.", ch);
}
else if(ch>=0 && ch<=9)
{
printf("'%c' is not an alphabet.", ch);
}
}

9
Q7) Accept the x and y coordinate of a point and find the quadrant in
which the point lies.

#include <stdio.h>
void main()
{
int co1,co2;

printf("Input the values for X and Y coordinate : ");


scanf("%d %d",&co1,&co2);

if( co1 > 0 && co2 > 0)


printf("The coordinate point (%d,%d) lies in the First
quandrant.\n",co1,co2);
else if( co1 < 0 && co2 > 0)
printf("The coordinate point (%d,%d) lies in the Second
quandrant.\n",co1,co2);
else if( co1 < 0 && co2 < 0)
printf("The coordinate point (%d, %d) lies in the Third
quandrant.\n",co1,co2);
else if( co1 > 0 && co2 < 0)
printf("The coordinate point (%d,%d) lies in the Fourth
quandrant.\n",co1,co2);
else if( co1 == 0 && co2 == 0)
printf("The coordinate point (%d,%d) lies at the origin.\n",co1,co2);

10
Q8) Accept the Cost price and selling price from the keyboard .Find out if
the seller has made the profit or loss and display how much profit or loss
has been made.

#include<stdio.h>
int main()
{
float cost,price,m;
printf("enter cost of keyboard : ");
scanf("%f",&cost);
printf("\n enter selling price of keyboard : ");
scanf("%f",&price);
m=cost - price;
if(m>0)
{
printf("loss = %f",m);
}

11
else if(m<0)
{
m=(-1)*m;
printf("profit = %f",m);
}
else
{
printf("NO profit NO loss");
}
}
OUTPUT:

12
13

You might also like