0% found this document useful (0 votes)
11 views13 pages

EEE 2402 Structured Programming Lab Report

The document outlines lab assignments for the course EEE 2402, focusing on structured programming using C. It includes explanations and code for various problems, such as calculating power consumption costs, solving quadratic equations with complex numbers, and converting two-digit numbers to words. Additionally, it features practice problems demonstrating the use of conditional statements in programming.

Uploaded by

abujubair0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views13 pages

EEE 2402 Structured Programming Lab Report

The document outlines lab assignments for the course EEE 2402, focusing on structured programming using C. It includes explanations and code for various problems, such as calculating power consumption costs, solving quadratic equations with complex numbers, and converting two-digit numbers to words. Additionally, it features practice problems demonstrating the use of conditional statements in programming.

Uploaded by

abujubair0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Course code: EEE 2402

Course title: Structured Programming Language Laboratory

Submitted By :
Group: 2
1. Junayet Antor ( 0212320018 )

[Link] Islam Shifa (0212230061 )


[Link] Jubair ( 0212320028 )

Submitted To :
Md. Nure Alam Dipu
Lecturer, Department of Electrical and Electronic Engineering
Lab Assignment
Question :1
Explanation: Here, the problem can be solved by using basic (if….else) condition. Firstly we
have declared two variable namely, “unit” and “ money ”. Here “unit” is an integer as the
amount of power can’t be in fraction form. And “money” is float data type as the results can be
in fractions.
Then we have assigned “if” condition for different amount of power consumption. And based
on that, the cost is calculated. For example, if “unit = 10” then the code will look through all the
conditions and find which one to run. Here in the first if condition we have (unit >= 0 && unit
<= 100) which means that unit between 0 to 100 will operate here. Therefore, the cost will be
“unit * 4.5” and thus the result is 45.
Similarly if the “unit =110 / 210 /410 ” the values will go to individual locations and run that
part of the code thus we will get the below answers.

Code:
#include <stdio.h>
int main(){
int unit;
float money;

printf("Please enter the amount of power consumed:");


scanf("%d",&unit);

if (unit >= 0 && unit <= 100){

money = unit * 4.5;


printf("The amount need to be paid is:%.2f",money);
}
else if (unit >= 101 && unit <= 200){
money = ( unit * 5 ) + 100;
printf("The amount need to be paid is:%.2f",money);
}
else if (unit >= 201 && unit <= 400){

money = ( unit * 6 ) + 200;


printf("The amount need to be paid is:%.2f",money);
}
else if (unit >= 401){

money = ( unit * 7 ) + 400;


printf("The amount need to be paid is:%.2f",money);
}
else {
printf("The value is invalid, please input a valid number");
}

return 0;
}

Result:
Lab Assignment
Question :2
Explanation: To solve this problem we need a new header file which is #include
<complex.h>. We are using this as we are working with complex numbers.
Firstly we are asking user to input three integers. And to store the complex numbers we used
“double complex ” data type (ANS_P, ANS_N).
Now, we at first use a “if” condition that will prevent users to input “a=0” as it would not be a
quardratic equation. If the user gives a= 0 the code will not run and will print (This is not a
quadratic equation ).
If the quadratic equation is correct then we can apply formula to get the results. But we can see
that there is +ve and –ve in the formula [(-b +/- csqrt((b * b) - (4 * a * c))) / (2 * a)]. This is why
we are taking 2 complex variable which are ANS_P and ANS_N. ANS_P is for the formula with
+ve sign and ANS_N is for formula with negative sign. In the formula we have used “csqrt()”
instead of the traditional “sqrt()”, this is because “sqrt” can not square root a negative
number only “csqrt()” can square root a negative number.
Now the complex variables contain both real and imaginary part. In order to give the output in
the form of (a+ib) we first need to identify the real and imaginary part of the complex number.
For that we used “creal()” for real and “cimag()”for imaginary part of the complex number.
Now to print the numbers we need to use “%lf” as the data type is double. We write the
output “%.2lf + %.2lfi” in order to give the output in the form of (a+ib).
For instance, if the value of a=1 b=6 c=8, then we get only real part which is -2 and -4 and the
imaginary part is 0. An example for complex solution is when a=5 b=3 c=3. As we can see that
we got imaginary part for complex number and it is in the form of “a+ib” .

Code:
#include <stdio.h>
#include <complex.h>
#include <math.h>
int main(){
int a, b, c;
double complex ANS_P, ANS_N;
printf("Please input the value of a:");
scanf("%d", &a);
printf("Please input the value of b:");
scanf("%d", &b);
printf("Please input the value of c:");
scanf("%d", &c);

if (a == 0) {
printf("This is not a quadratic equation.");
}
else {
ANS_P = (-b + csqrt((b * b) - (4 * a * c))) / (2 * a);
ANS_N = (-b - csqrt((b * b) - (4 * a * c))) / (2 * a);
printf("The answers are ( %.2lf + %.2lfi ) and ( %.2lf %.2lfi )", creal(ANS_P), cimag(ANS_P),
creal(ANS_N), cimag(ANS_N));
}

return 0;
}

Result:
Lab Assignment
Question :3
Explanation: For this code we will divide it into 3 sections, Firstly, we need users to give
only 2 digit numbers, so for only numbers between 10 to 99 will work in this code. We have
applied a basic if condition to direct the numbers to their respected place to operate. If the
number is not 2 digits it will simply print “The number must be two digit”.
Now we know that the numbers between 10 to 19 are spelled differently than others. So for
that we have used if condition for numbers between 10 to 19. Here the two digit numbers are
being stored in “a” which is an integer data type. When the value of “a” is between 10 to 19 the
value will go to “switch”. Here, the value will go through each case until it finds the value which
is same and will print whichever the digit it was put in.
For numbers between 20 to 99. The way the first number is spelled same, for example, 21,
23,26 all this numbers starts with “twenty” and then the number which is “one, three, six”
respectively. So we first need to separate the 1st and 2nd value. For the 1st value we can simply
find it by dividing it by 10, for instance, 25/10 = 2.5 , but since we are storing the value in an
integer data type (F_num) the .5 will be erased , and for the 2nd digit we have to use
remainder. Here 25%10 = 5 and store it into (S_num), and this is how we get the second
number.
After getting the two numbers we use Switch. For first digit, it will go through case from 2 to 9
and print the words, for example for 2 it will print twenty, for 3 = thirty, for 6 =sixty etc. Then
the same thing will be applied for 2nd digit. But this time, the for each number it will print the
word of that number, for example, for 2 = two, for 6= six . Therefore we solved the problem
using switch.
And as default we have used “invalid” in order to see if we have any problem with the code.

Code:
#include<stdio.h>
int main(){
int a,F_num,S_num;

printf("Please input a two digit number:");


scanf("%d",&a);
if(a >= 10 && a <= 19){
switch(a){
case 10: printf("Ten"); break;
case 11: printf("Eleven"); break;
case 12: printf("Twelve"); break;
case 13: printf("Thirteen"); break;
case 14: printf("fourteen"); break;
case 15: printf("Fifteen"); break;
case 16: printf("Sixteen"); break;
case 17: printf("Seventeen"); break;
case 18: printf("Eighteen"); break;
case 19: printf("Nineteen"); break;
}
}
else if(a >= 20 && a<= 99){
F_num = a / 10;
S_num = a % 10;
switch (F_num){
case 2: printf("Twenty "); break;
case 3: printf("Thirty "); break;
case 4: printf("Forty "); break;
case 5: printf("Fifty "); break;
case 6: printf("Sixty "); break;
case 7: printf("Seventy "); break;
case 8: printf("Eighty "); break;
case 9: printf("Ninety "); break;
default : printf("Invalid");
}
switch (S_num){
case 0: printf("."); break;
case 1: printf("one"); break;
case 2: printf("two"); break;
case 3: printf("three"); break;
case 4: printf("four"); break;
case 5: printf("five"); break;
case 6: printf("six"); break;
case 7: printf("seven"); break;
case 8: printf("eight"); break;
case 9: printf("nine"); break;
default : printf("Invalid");
}
}
else {
printf("The number must be two digit.");
}
return 0;
}

Result:
Practice Problems
Question :1
Explanation: In the first problem, ‘if’ statement help the compiler to read the statements true or
false. As we already determine a=1 but in the statement a=2 which is not true. That’s why compiler
found this false and didn’t read the rest of the statement and print “Queen” directly.

Result:

Practice Problems
Question :2
Explanation: In this problem, a=1 and b=3 where the first statement shows that a=2 which is not
true as determined first. So, the compiler skipped the rest of the statements and as it’s not true, it
printed “King” according to else statement. If a and b remained same as determined, compiler would
print “Prince”. If b remained false, it would print “Queen” for the right statement for a.

Result:
Practice Problems
Question :3
Explanation: In this code there are two “if” condition. The first “if” condition checks
whether the value of “temp” is less than 80, as the assigned value of “temp” is assigned 32, so
the condition is full filed and then it can move to the next “if” condition which is whether the
value of temp is greater than 60. This time the condition is not satisfied. Now the 2 nd if
condition has “else” so, as the condition is not true therefore the “else” will run which is “Sure
IT is hot!”.
If the 2nd condition was true, then the code would print “Nice day!”.
And if the 1st if condition is false the code wouldn’t have printed anything cause only the 2nd
condition has the “printf” functions.

Result:
Practice Problems
Question :4
Explanation: This code is similar to the previous Practice problem 3. Here, we have assigned
two integers, a= 2 and x= 10. There are two if conditions, in the 1 st if conditions it looks whether
the value of a is equal to 2, and it satisfies the conditions therefore the code runs further and
faces 2nd if condition where it checks if the value of x is equal to 8, this time the condition is not
satisfied so the “else” will be printed which is “a is not equal to 2”.
If the 1st condition was not satisfied, then nothing would have printed.

Result:

Example
Question :1
Explanation: compiler read the condition under if and find out the condition is true so it
execute the statement under if.

Result:
Example
Question :2
Explanation: compiler read the condition under if .when the condition is true compiler
execute the statement under if but when the condition is false then the compiler will execute
the statement under else("statement").
Here condition under if is true so the compiler print: (a is larger than b).

Result:

Example
Question :3
Explanation: compiler read the condition under if when the condition is true compiler
execute the statement under if but when the condition is false then the compiler will execute
the statement under else("statement")
Here condition under if is true so the compiler print: (a is larger than b and c).

Result:
Example
Question :4
Explanation: Compiler read the condition under if when the condition is true compiler
execute the statement under if but when the condition is false then the compiler will jump over
and go through the next condition under (else if ), and so on till finding out any condition that is
true and execute the statement under the function or will execute the statement under
else("statement")'.

Result:

Example
Question :5
Explanation: Switch operation work by finding equality in variable and every value in the
variable that is under switch is a case. When a value is taken under the variable that is set in
switch, the statement under that case for that specific given value will execute.

Result:

You might also like