You are on page 1of 14

DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINNERING

EEE3123
COMPUTER PROGRAMMING
GROUP 1
LAB 3: SELECTION STRUCTURES

NAME : MD AZIZUL HOQUE


MATRIC NO : 200191
LECTURER : ASSOC. PROF. DR. WAN ZUHA BIN WAN HASAN
SUBMSSION DATE : 01.12 .2020
Objective: To write, compile and execute a program that uses floating point numbers.

Equipment: Personal Computer with C Program Pre-Installed

Introduction:

If else statements in C is also used to control the program flow based on some
condition, only the difference is: it's used to execute some statement if the expression is
evaluated to true, otherwise executes else statement in the C programme.

Methodology:

1. Write, compile and execute a C program that will

Prompt the user for 5 floating numbers as shown below.

Enter number 1:

Enter number 2:

….

Enter Number 5:

Find the minimum and maximum values among these 5 numbers using if statements

Compute the average from these numbers

Prints “You entered 5 numbers. The minimum is xx, the maximum is yy and the average
is zz”
2. A program that takes the x-y coordinates of a point in the Cartesian coordinate
system and displays the points entered while informing the user in which
quadrant (refer to Fig. 3.1) the point lies in is written.

QUESTION 1

PSEUDOCODE

1.ENTER

2. INPUT FIVE NUMBER

3. FIND MINIMUM AND MAXIMUM NUMBER

CALCULATE AVERAGE OF 5 NUMBERS

4.END
CODING:

#include <stdio.h>

int main()

float a,b,c,d,e,avg;

printf("Input 1st number: ");

scanf("%f",&a);

printf("Input 2nd number: ");

scanf("%f",&b);

printf("Input 3rd number: ");

scanf("%f",&c);

printf("Input 4th number: ");

scanf("%f",&d);

printf("Input 5th number: ");

scanf("%f",&e);

if(a<=b && a<=c && a<=d && a<=e)

printf("Minimum value is: %f",a);

}
else if(b<=a && b<=c && b<=d && b<=e)

printf("Minimum value is: %f",b);

else if(c<=a && c<=b && c<=d && c<=e)

printf("Minimum value is: %f",c);

else if(d<=a && d<=b && d<=c && d<=e)

printf("Minimum value is: %f",d);

else if(e<=a && e<=c && e<=d && e<=b)

printf("Minimum value is: %f",e);

printf("\n");

if(a>=b && a>=c && a>=d && a>=e)

{
printf("Maximum value is: %f",a);

else if(b>=a && b>=c && b>=d && b>=e)

printf("Maximum value is: %f",b);

else if(c>=a && c>=b && c>=d && c>=e)

printf("Maximum value is: %f",c);

else if(d>=a && d>=b && d>=c && d>=e)

printf("Maximum value is: %f",d);

else if(e>=a && e>=c && e>=d && e>=b)

printf("Maximum value is: %f",e);

avg = (a+b+c+d+e)/5;

printf("\nAverage is: %f",avg);

}
Listing 1.1: Ques no: 1, C coding using the If… else if statement to calculate the
minimum and maximum value
Listing 1.2: Ques no: 1, C coding using the If… else if statement to calculate the
minimum and maximum value

Listing 1.3: The maximum and the minimum value using the if. else if statement
QUESTION- 2

1)

PSEUDOCODE

1. START

2. INPUT THE VALUE OF X AND Y COORDINATE

3. IF (x==0 && y==0)

THEN PRINTF (Center point);

ELSE IF(x == 0)

PRINTF (Over Y axiS);

ELSE IF (y == 0)

PRINTF (Over X axis);

ELSE IF(x>0 && y>0)

PRINTF ("Quadrant 1);

ELSE IF(x<0 && y>0)

PRINTF ("Quadrant 2);

ELSE IF(x<0 && y<0)

PRINTF (Quadrant 3)

ELSE IF(x>0 && y<0)

PRINTF (Quadrant 4);

4.END
CODING:

#include <stdio.h>

int main ()

int x,y;

printf("Input X coordinate: ");

scanf("%d",&x);

printf("Input Y coordinate: ");

scanf("%d",&y);

if(x==0 && y==0)

printf("Center point\n");

else if(x == 0)

printf("Over Y axis\n");

else if(y == 0)

{
printf("Over X axis\n");

else if(x>0 && y>0)

printf("Quadrant 1\n");

else if(x<0 && y>0)

printf("Quadrant 2\n");

else if(x<0 && y<0)

printf("Quadrant 3\n");

else if(x>0 && y<0)

printf("Quadrant 4\n");

return 0;

}
Listing 2.1: Ques no: 2, C coding using the if..else if statement to print the value of x
and y in the right quadrant.
Listing 2.2: Ques no: 2, C coding using the if..else if statement to print the
value of x and y in the right quadrant.

Listing 2.3: Ques no: 2, output value of the x and y in the right quadrant.
Discussion:

In the question1, 5 input numbers were given such as a, b,c,d, and e . To calculate the
minimum and maximum values, I have used the if ...else if statement to check the value
which one larger or which one is smaller. Such as if a>b,c,d, and e. then the Printf will
print the maximum number a and the minimum number is e . The program was written
in a way that it had checked all the value one by one using the else if statement
respectively. So, once we run the program and input the value for a, b,c, d and e it
prints the maximum value and the minimum value in the program which is stead in the
listing no 1.1 and 1.2. In the question 2, was asked to write a program to see where the
coordinate x and y value lies in the quadrant. So, to put the value in the right quadrant,
right condition was given using the if else statement. So , when both x==0 and y==0 it
lies in the center point of the graph else when the X==0 , it will be over the x axis , y==0
it will be over the Y axis and finally if the value of the x>0 and y>0 , the values will be
printed in the first quadrant . I made such condition for the remaining quadrant to check
the where the value of x and y lies in which quadrant. In the listing 2.1 and 2.2, the
coding of the program and the output to print in right quadrant stated. All the asked
question was found in the given lab so, the purpose of finding out the true numbers
using the if…else if statement were achieved.

Conclusion:

As conclusion, I can say that the ‘if-else’ statement is crucial to providing convenience
when it comes to writing, compiling and executing a program with utmost efficiency.

Reference:

1.Lab manual 3, EEE3123- Computer Programming.

You might also like