Professional Documents
Culture Documents
EEE3123
COMPUTER PROGRAMMING
GROUP 1
LAB 3: SELECTION STRUCTURES
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:
Enter number 1:
Enter number 2:
….
Enter Number 5:
Find the minimum and maximum values among these 5 numbers using if statements
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
4.END
CODING:
#include <stdio.h>
int main()
float a,b,c,d,e,avg;
scanf("%f",&a);
scanf("%f",&b);
scanf("%f",&c);
scanf("%f",&d);
scanf("%f",&e);
}
else if(b<=a && b<=c && b<=d && b<=e)
printf("\n");
{
printf("Maximum value is: %f",a);
avg = (a+b+c+d+e)/5;
}
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
ELSE IF(x == 0)
ELSE IF (y == 0)
PRINTF (Quadrant 3)
4.END
CODING:
#include <stdio.h>
int main ()
int x,y;
scanf("%d",&x);
scanf("%d",&y);
printf("Center point\n");
else if(x == 0)
printf("Over Y axis\n");
else if(y == 0)
{
printf("Over X axis\n");
printf("Quadrant 1\n");
printf("Quadrant 2\n");
printf("Quadrant 3\n");
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: