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: