Ex.No.3 Usage of Control Structure Reg.
No:
16.11.2021 URK21CS7046
A) Write a program to check whether the given number even or not. If yes, display
message ‘Even Number’
Aim: To check whether the given number even or not.
Algorithm
Step 1: Start the Program.
Step 2: Create a variable number.
Step 3: Read the Input values from user.
Step 4: Use if / else statement to check whether it is even or odd number.
Step 5: Stop the Program.
Program
#include <stdio.h>
int main ()
{
int number;
printf("Enter number:",number);
scanf("%d",&number);
if (number%2==0)
printf("Even");
else
printf("Odd");
return 0;
}
Output
Result:
The program is executed successfully and checked whether the given number is even or not & printed
in the screen using input and output functions.
1
B) Write a program to check the given integer is positive or not using if...else.
Aim: To check the given integer is positive or not using if...else.
Algorithm
Step 1: Start the Program.
Step 2: Create a variable number.
Step 3: Read the Input values from user.
Step 4: Use if / else statement to check whether it is positive or negative number.
Step 5: Stop the Program.
Program
#include <stdio.h>
int main () {
int number;
printf("Enter number ", number);
scanf("%d",&number);
if (number>0)
printf("Positive");
else
printf("Negative");
return 0;
}
Output
Result:
The program is executed successfully and checked whether the given number is POSITIVE or
NEGATIVE & printed in the screen using input and output functions.
1
C) Write a program to find the smallest among three numbers using nested if.
Aim: To find the smallest among three numbers using nested if.
Algorithm
Step 1: Start the Program.
Step 2: Create Three variables ‘a’, ‘b’, ‘c’.
Step 3: Read the Input values from user.
Step 4: Use nested if statement to check which number is smallest number.
Step 5: Stop the Program.
Program
#include <stdio.h>
int main () {
int a,b,c;
printf("Enter number 'a'",a);
scanf("%d",&a);
printf("Enter number 'b'",b);
scanf("%d",&b);
printf("Enter number 'c'",c);
scanf("%d",&c);
if (a<b && a<c)
printf("'a' is smallest of all");
if (b<a && b<c)
printf("'b' is smallest of all");
if (c<a && c<b)
printf("'c' is smallest of all");
return 0;
}
Output
Result:
The program is executed successfully and checked which number is smallest & printed in the screen
using input and output functions.
1
D) Write a program to calculate the tax, given the following conditions.
a. If income is less than 1,50,000 then no tax
b. Its taxable income is in the range of 1,50,001 – 3,00,000 then charge 10% of tax
c. Its taxable income is in the range of 3,00,001 – 5,00,000 then charge 20% of tax
d. If taxable income is above 5,00,001 then charge 30% of tax
Aim: To calculate the tax.
Algorithm
Step 1: Start the Program.
Step 2: Create variable ‘amount’.
Step 3: Use If/else statement and according to the given conditions print the tax in output.
Step 4: Stop the Program.
Program
void main () {
float amount;
float tax=0;
printf("Enter the amount of income: ");
scanf("%f",&amount);
if(amount<=150000) {
tax=0;
} else if(amount>150000 && amount<=300000) {
tax=amount*0.1;
} else if(amount>300000 && amount<=500000) {
tax=amount*0.2;
} else {
tax=amount*0.3;
}
if(tax==0) {
printf("\nNo tax.\n");
} else {
printf("\nThe tax = $%.2f\n",tax);
}
getchar();
}
1
Output
Result:
The program is executed successfully and calculated the tax and printed in the screen using input and
output functions.
2
E) The Body Mass Index (BMI) is defined as ratio of the weight of a person (in kg) to the
square of the height (in meters). Write a program that receives weight and height,
calculates the BMI, and reports the BMI category as per the following table.
Aim: To find the BMI.
Algorithm
Step 1: Start the Program.
Step 2: Create Three variable ‘height’ , ‘weight’ & ‘bmi’.
Step 3: Use the formula to calculate the BMI.
Step 4: Using if/else statement assign the BMI to respective BMI category using the table.
Step 5: Stop the Program.
Program
#include <stdio.h>
int main ()
{
float height, weight, bmi;
printf("Enter height in meter\n");
scanf("%f", &height);
printf("Enter weight in kg\n");
scanf("%f", &weight);
bmi = weight / (height * height);
printf("Your Body Mass Index(BMI) is %f\n", bmi);
if(bmi < 15)
{
printf("Your BMI category is: Starvation\n");
}
else if(bmi >= 15.1 && bmi <= 17.5)
{
printf("Your BMI category is: Anorexic\n");
}
else if(bmi >= 17.6 && bmi <= 18.5)
{
printf("Your BMI category is: Underweight\n");
1
}
else if(bmi >= 18.6 && bmi <= 24.9)
{
printf("Your BMI category is: Ideal\n");
}
else if(bmi >= 25 && bmi <= 25.9)
{
printf("Your BMI category is: Overweight\n");
}
else if(bmi >= 30 && bmi <= 30.9)
{
printf("Your BMI category is: Obese\n");
}
else if(bmi >= 40)
{
printf("Your BMI category is: Morbidly Obese\n");
}
else
{
printf("Wrong entry\n");
}
return 0;
}
Output
Result:
The program is executed successfully and calculated the BMI & printed in the screen using input and
output functions.
2
F) Write a C program to create Simple Calculator using switch case.
Aim: To create Simple Calculator using switch case.
Algorithm
Step 1: Start the Program.
Step 2: Create Two integer ‘first’ & ‘second’.
Step 3: Read use which operands to use from the user.
Step 4: Use switch case and perform the operation on the numbers.
Step 5: Stop the Program.
Program
#include <stdio.h>
int main () {
char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
default:
printf("Error! operator is not correct");
return 0;
}
}
1
Output
Result:
The program is executed successfully and performed the operation on the numbers & printed in the
screen using input and output functions.