# Practice questions on Decide if/else

1.

2.

3.

4.

5.

## Level 2

1.

### Take values of length and breadth of a rectangle from user and check if it is square or not.

```#include <stdio.h>
int main()
{
printf("Enter the values for length and breadth\n");
{
printf("Yes, it is a square.\n");
}
else
{
printf("No, it is a rectangle.\n");
}
return 0;
}
```

2.

3.

### A shop will give discount of 10% if the cost of purchased quantity is more than 1000.Ask user for quantitySuppose, one unit will cost 100.Judge and print total cost for user.

```#include <stdio.h>
int main()
{
int quantity,total_cost;

printf("Enter quantity\n");
scanf("%d",&quantity);
total_cost = quantity*100;

if(total_cost>1000){
total_cost = total_cost-(.1*total_cost);
}
printf("Total cost is %d\n",total_cost);

return 0;
}
```

4.

5.

### A school has following rules for grading system:a. Below 25 - Fb. 25 to 45 - Ec. 45 to 50 - Dd. 50 to 60 - Ce. 60 to 80 - Bf. Above 80 - AAsk user to enter marks and print the corresponding grade.

```#include <stdio.h>
int main()
{
int marks;
printf("Enter marks\n");
scanf("%d",&marks);

if(marks<25 && marks >=0){
printf("F\n");
}
else if(marks>=25 && marks<45){
printf("E\n");
}
else if(marks>=45 && marks<50){
printf("D\n");
}
else if(marks>=50 && marks<60){
printf("C\n");
}
else if(marks>=60 &&marks;<80){
printf("B\n");
}
else if(marks>=80 && marks<=100){
printf("A\n");
}
else{
printf("Enter valid marks\n");
}

return 0;
}
```

6.

7.

### Write a C program to print absolute vlaue of a number entered by user. E.g.-INPUT: 1        OUTPUT: 1INPUT: -1        OUTPUT: 1

```#include <stdio.h>
int main()
{
int num;
printf("Enter number\n");
scanf("%d",&num);

if(num<0)
{
num = -1*num;
}
printf("%d\n",num);

return 0;
}
```

8.

9.

### A student will not be allowed to sit in exam if his/her attendence is less than 75%.Take following input from userNumber of classes heldNumber of classes attended.And printpercentage of class attendedIs student is allowed to sit in exam or not.

```#include <stdio.h>
int main()
{
int num_of_held,num_of_atten;
printf("Total number of classes held and attended?\n");
scanf("%d %d",&num_of_held, &num_of_atten);
float per = (num_of_atten/(float)num_of_held)*100;
printf("You have attended %f of total classes\n",per);
if(per>=75){
printf("Yes, you are allowed\n");
}
else{
printf("No\n");
}
return 0;
}
```

10.

## Level 3

1.

### Write a C program to check if a year is leap year or not.If a year is divisible by 4 then it is leap year but if the year is century year like 2000, 1900, 2100 then it must be divisible by 400.

Use % ( modulus ) operator.

2.

### A 4 digit number is entered through keyboard. Write a C program to print a new number with digits reversed as of orignal one. E.g.-INPUT : 1234        OUTPUT : 4321INPUT : 5982        OUTPUT : 2895

```#include <stdio.h>
int main()
{
int x;
printf("Enter number\n");
scanf("%d",&x);
int first_digit = x%10;
int second_digit = (x/10)%10;
int third_digit = (x/100)%10;
int fourth_digit = (x/1000)%10;
int new_number = (first_digit*1000)+(second_digit*100)+(third_digit*10)+(fourth_digit*1);
printf("%d\n",new_number);
return 0;
}
```

3.

### Write a C program to check whether a entered character is lowercase ( a to z ) or uppercase ( A to Z ).

You can use ASCII values.

4.

### Ask user to enter age, sex ( M or F ), marital status ( Y or N ) and then using following rules print their place of service.if employee is female, then she will work only in urban areas.if employee is a male and age is in between 20 to 40 then he may work in anywhereif employee is male and age is in between 40 to 60 then he will work in urban areas only.And any other input of age should print "ERROR".

```#include <stdio.h>
int main()
{
char sex;
char marital_status;
int age;

printf("Enter sex, marital status and age\n");
scanf(" %c %c %d",&sex,&marital_status,&age);

if(sex == 'F' && age>=20 && age<=60)
{
printf("You will work in urban area.\n");
}
else if(sex=='M' && age>=20 && age<=40)
{
printf("You can work anywhere.\n");
}
else if(sex=='M' && age>40 && age<=60)
{
printf("You will work in urban area.n\n");
}
else
{
printf("Error.\n");
}
return 0;
}
``` 