0% found this document useful (0 votes)
8 views16 pages

EEE 2402 Structured Programming Lab Report

The document contains a structured programming laboratory assignment for EEE 2402, detailing multiple coding problems and their solutions using C programming. Each question includes an explanation of the approach taken, the corresponding code, and the expected results. The topics covered include prime number identification, permutations, nested loops, and factorial calculations.

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)
8 views16 pages

EEE 2402 Structured Programming Lab Report

The document contains a structured programming laboratory assignment for EEE 2402, detailing multiple coding problems and their solutions using C programming. Each question includes an explanation of the approach taken, the corresponding code, and the expected results. The topics covered include prime number identification, permutations, nested loops, and factorial calculations.

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: To solve this problem we first divide the code into 2 parts.
1st part : Firstly we will identify the prime and non-prime number. We know that a prime
number is only dividable by 1 and the number itself. So we used a for loop where the condition
is (i = 2 ; i <= (a - 1) ; i++). Here i=2 not 1 because any number can be divided by 1. Then we set
its range from 2 to (a-1). Here “a” is the number whom we are dividing. And the number is
being incremented by 1 each cycle. In the for loop we have used an if condition which will check
if the number has any remainder. If the number has any remained that means that the number
is not dividable by any number. But if it is dividable the remainder will be 0. Therefore it will
execute the code within in which is “flag=1”. Here “flag” is nothing but a way to know whether
the number is prime or not. Lastly there is an if condition that will check the value of “flag”. If it
is 1 that means the number is prime and the code will not execute anything. On the other hand
,if the value of flag is 0 then the number is prime and the code will print that number.
2nd part : Now we will talk about the outer loop that will increase the value of “a” which is the
number that we are trying to find the prime number. To do so we need to use a for loop in the
outer part. Here for(a = 2 ; a <= 300 ; a++) is the loop to increase the value of “a”. Here, the
entire loop of the part 1 is included however, at first we have used “flag=0” this is to reset the
value of “flag” for the next value of “a”. For example, if the loop executes for “a=2” then the
value of “flag” will become 1, but for the next value of “a” which is 3 the value of flag will
remain 1 and thus will not print that 3 is a prime number, but 3 is a prime number.
Here we have use %3d for decoration purpose. The first output is without %3d and 2nd output
is with %3d.

Code:
#include<stdio.h>
int main(){
int a,i,flag;
for(a = 2 ; a <= 300 ; a++){
flag = 0;
for(i = 2 ; i <= (a - 1) ; i++){
if(a%i == 0){
flag = 1;}
}

if(flag == 0){
printf("%3d is a prime number.\n",a);
}
}
return 0;
}

Result: it is down below. Please scroll.


Lab Assignment
Question :2
Explanation: To solve this problem we need to use nested for loop. Here we have 3 loops
for 3 numbers. We have to make sure that each number doesn’t repeat. For the first number
we have used for(a = 1 ; a <= 3 ; a++) this will give our 1st number. Within this loop is our 2nd
and 3rd loop. In our 2nd loop we have for(b = 1 ; b <= 3 ; b++). But here the 2nd number can be 1
too. To solve this problem we have use an if condition which checks whether the value of “b” is
same as “a”. Here the condition is if(b != a), if value of “a” is equal to “b” then the if condition
doesn’t execute and the value of “b” is incremented by 1. Now the value of b is 2. So now the
value of b satisfies the if condition. Inside the if condition is the 3rd loop which gives us the
value of 3rd number. Here we have also used an if condition so that the value if “c” doesn’t
match 1st and 2nd value which is why the condition is if(c != b && c != a). it checks whether the
value of c is equal to and b. If the value is equal (even any one) then the condition will not be
satisfied and the value of c will increase by 1 each time until the value of it is 3. As the value of
“a=1” and ”b=2”. Lastly it will print the values of a,b and c. And then the 1st loop will start
again with a =2.

Code:
#include<stdio.h>
int main(){
int a,b,c;
for(a = 1 ; a <= 3 ; a++){
for(b = 1 ; b <= 3 ; b++){
if(b != a){
for(c = 1 ; c <= 3 ; c++){
if(c != b && c != a){
printf("%d%d%d\n",a,b,c);}
}
}
}
}
return 0;
}

Result:

Lab Assignment
Question :3
Explanation: Required structure of number looks like a right angle pyramid consisting of
numbers. To build this structure, we can use nested for loops. First of all, we declare the
needed variables n, row, and col. Here n represents the number of lines the user wants to print,
and we take it from the user and save the value of lines in n using a scanf statement. We set a
for loop for row values. We initialize the value of row equal to 1 and set the loop control
variable to check the condition (row <= n). The row is incremented by 1. We notice that here
the row number and the column number are the same for each line. So, we set another for loop
in a way that col relates to row. For that, we initialize the value of col equal to 1, set the loop
control variable (col <= row), and increment col in every case. In the second loop, we set a
statement to print the number of col. For the first loop, we set a statement to print a new line.
How the Code Works:
Firstly, the user gives an input and scanf saves the value under n. Then the compiler will enter
the first for loop. At first, row = 1 is initialized and it satisfies the condition, so the compiler will
enter the loop and check the second loop. Here the most important part comes. In the second
loop, col = 1 is initialized and it satisfies the condition. As a result, the compiler will execute the
statement under the loop and print the number of col. Here the number of col is 1. Then col will
increment its value to 2. But that is false for the condition, so the loop will break here and
return to the first loop. There the value of row will increment, and the new value of row will be
2. Like before, as the condition is satisfied, the compiler will enter into the loop. For the second
loop now the value of col = 2. The loop will run 2 times and will print (1 2). The process will
continue until the condition in the first loop is false. The loop will run as the user puts the value
of n because we set the condition (row <= n).
Code:
#include<stdio.h>
int main()
{
int n,row,col;
printf("ENTER NUMBER OF LINE: ");
scanf("%d",&n);
for(row = 1 ; row <= n ; row++)
{
for(col = 1 ; col <= row ; col++)
{
printf("%6d",col);
}
printf("\n");
}

return 0;
}

Result:
Lab Assignment
Question :4
Explanation: We have to take some integer type variables (i, j, k, n, v, u, x). For n and r, we
take input from the user. As we know that we can only find the permutation value for a positive
number, we give a notice to the user to input a positive integer number. For finding the
factorial of n, we set a for loop and initialize the loop control variable i=0 and set the condition
i<=n. Furthermore, every time the value of i will increment by 1. Now we use an if function
under the for loop. In that case, whenever the value of i is not equal to 0, the value of v
becomes (v*i). By this process, we will get the factorial of n.
Now we also need the factorial of (n-r). By changing the variables and following the process we
used for finding the factorial of n, we can get the factorial of (n-r). We put the factorial value of
(n-r) under u.
For the last step, we just have to divide v by u, which represents the sequence-wise factorial of
n and factorial of (n-r). We put the value of (v/u)=k. If we print k, that will show the answer of
the user's given numbers permutation.

Code:
#include<stdio.h>
int main()
{
int i,j,k,n,x,r,v = 1,u = 1;
printf("N:B-PERMUTATION OF NEGATIVE NUMBER DOESN'T EXIST\n");
printf("n>r\n");
printf("ENTER A POSITIVE NUMBER FOR 'n': ");
scanf("%d",&n);

printf("ENTER A POSITIVE NUMBER LESS FOR 'r': ");


scanf("%d",&r);

x=(n - r);
for(i = 0 ; i <= n ; i++)
{
if(i != 0)
{
v = v * i ;}}

for(j = 0 ; j <= x ; j++)


{
if(j != 0)
{
u = (u * j);}}

k=(v / u);
printf("PARMUTATION OF %dP%d IS : %d\n",n,r,k);
return 0;
}

Result:
Practice Problems
Question :1
Explanation: Here we are given with a do…while loop where the initial value of “i” is 10,
and “i” is decremented by 3 each loop and the condition is “i”. This means if the value of “i” is
+ve the condition is true and if the value of “i” is zero then the condition is false. However, we
can see that the output is moving towards (-ve) infinity. This is because after the 4 th loop the
value of “i” is 1. After further decrement the value becomes -2 which is not zero. And which is
why we get infinity output.

Result:
Practice Problems
Question :2
Explanation: We used for-loop and used continue statement here to avoid printing a
number (i=3). For using “continue” statement the compiler will skip the loop when i=3 and
continue the loop as normal and the output is series of numbers without 3.

Result:

Practice Problems
Question :3
Explanation: In do...while loop firstly the statement under “do” is executed then the
condition in while is checked. If the condition is right, then the statement will execute again but
if the condition is false then the statement won't execute again. In do...while loop the
statement under “do” will execute at least one time no matter the condition is wrong or right.
Here the condition was 4<1 which is obviously false but we still got an output.

Result:
Practice Problems
Question :4
Explanation: In this loop we have two variables one is “i” and the other is ”j”. Here we
don’t have any initial value of “i” (not in the body nor in the loop). Here the condition is i=j and
“j” is decremented by 2 each loop. That means when the value of “j” is equal to “i”. So any non-
zero value is true in this case. When j=10, the value of i=10. Until when the value of “j” is zero,
then the value if “i” will be zero and the condition for “zero” is false and the loop will be over.

Result:
Example
Question :1
Explanation: In this problem, we have to program that the given number is a prime number
or not. For this, we have to create a flag variable which is 1 when the remainder is 0 after
dividing the given number by a variable 'i' which has increased from 2 to the number 1 less than
the given number in a loop. It means when the remainder becomes 0, the given number is
divisible. Hence it’s not a prime number otherwise it’s a prime number.

Result:

Example
Question :2
Explanation: We are determining the factorial of the given number which is greater than 0.
Because negative numbers don’t have any factorial and the factorial of 0 is 1. We’ve fixed the
number 1 so that the multiplier won’t go lesser than that. First, we make the initiate answer=0.
While the number is greater than 0, answer will be the multiplier of the previous answer and
the number lesser than the given number. That’s how we find the factorial of the given
number.

Result:
Example
Question :3
Explanation: To convert binary number to decimal number, multiply each binary digit by
increasing powers of 2, starting from the right. That's why we divide k by 10 and get the
remainder which will multiply with the power of 2.

Result:
Example
Question :4
Explanation: In terms of answering gcf (greatest common factor), we have to divide the two
numbers where remainder becomes the divisor and the previous devisor becomes the dividend
and this process iterate until the remainder becomes 0. From the common divisor, we get the
gcf (greatest common factor).

Result:

Example
Question :5
Explanation: To answer the sum of the series of the square of the given number, we need
to start from summing the square of 0 to the square of the given number. The variable starts
from 0 and increases the number by 1 while the iteration process run till reaching the given
number.

Result:

Common questions

Powered by AI

Handling user input in loops requires careful consideration of validation, such as ensuring inputs meet expected criteria before execution (e.g., inputting positive integers for factorials or permutations). Input values should be scanned and stored into variables that need to be checked or used within the loop to ensure that the loop operates with valid and expected data. It is important to provide guidance or error messages when inputs fall outside of acceptable ranges to ensure correct loop execution and output .

The nested loops generate a right-angled pyramid of numbers by incrementing the row number and using it as the limit for the columns in each iteration. The outer loop increments the 'row' variable from 1 up to 'n', the number of lines desired. For each row, the inner loop counts up from 1 to the current 'row' value, thus ensuring that the column number is always less than or equal to the row number, creating a right-angle pattern .

The algorithm determines if a number is prime by attempting to divide it by every integer from 2 to one less than the number itself. If any division results in a remainder of 0, the number is not prime, and the 'flag' variable is set to 1. If no such division is found, the 'flag' remains 0, indicating that the number is prime. The 'flag' thus acts as an indicator of whether or not the number is prime .

The use of '%3d' in the prime number output serves as a formatting specifier in the printf function that allocates a minimum width of 3 characters for printing the numbers. This ensures that the numbers are right-aligned for consistent visual presentation, improving the overall readability of the output .

Calculating the permutation involves determining the factorial of a number 'n' and dividing it by the factorial of 'n-r', effectively removing the order of sequences not needed from total permutations. The division by (n-r)! ensures that only the permutations of interest, where the order of all 'r' elements matters, are counted. This method follows directly from the formula for permutations, which is n!/(n-r)! .

The nested loops are designed to generate all possible combinations of three-digit numbers, ensuring that each digit is unique. The outer loop sets the first digit, and the middle loop selects the second digit only if it is different from the first. The innermost loop picks the third digit, ensuring it is different from both the first and second digits. This logic prevents any repetition of digits within each generated combination .

The 'continue' statement within a for-loop affects its execution by immediately skipping to the next iteration of the loop, bypassing any code that follows it in the current loop body. For example, if placed in a loop that prints numbers, and placed inside a conditional checking for i=3, 'continue' causes the loop to skip printing 3, leading to sequential numbers being printed except for 3 .

Factorial calculations are used in permutation formulas by taking the factorial of the total number of items 'n', and dividing by the factorial of the difference between 'n' and 'r', n-r. This accounts for selecting and ordering 'r' items from 'n', considering the order of selection. It is crucial that 'n' is greater than or equal to 'r', as permutations cannot be calculated otherwise .

Converting a binary number to its decimal equivalent is achieved by multiplying each binary digit (bit) by powers of 2 corresponding to its position from the right. Each multiplication result is summed to produce the decimal value. The role of the positional weights, thus, is to account for the place value of each bit, exponentially increasing from right to left, starting from 2⁰ .

Do...while loops execute their contained statements at least once regardless of whether the condition is false. This structure affects outputs by ensuring that the statement block runs a minimum of one time before the condition is even evaluated, which can be particularly useful for operations that need to be performed initially regardless of the loop’s terminating condition .

You might also like