Course code: EEE 2402
Course title: Structured Programming Language Laboratory
Submitted By :
Group: 2
1. Junayet Antor ( 0212320018 )
2.Nafisul Islam Shifa (0212230061 )
3.Abu Jubair ( 0212320028 )
Submitted To :
Md. Nure Alam Dipu
Lecturer, Department of Electrical and Electronic Engineering
Lab Assignment
Question :1
Explanation: In this problem we have to use user defined function to find Armstrong
numbers from 1 to 500. An Armstrong number is when the sum of the cube of each digit of a
number is equal, that is for example, 153 the sum of 13 +53 +33 is equal to 153. One of the main
challenges of this problem was to separate individual digits from a number.
Explanation: Firstly we have to write the necessary header files to write the code, since we are
working with power therefore we user <math.h> header file which will help us to use “pow”
function. Then we have named our user defined function “AS” which is an integer type. In the
main function we have 5 integer variables namely,” num , N1 , N2 , N3 , sum”. We have used a
for loop that will increase numbers from 1 to 500. Inside the loop we have digit separator
which will separate digits from the number.
Digit separator: Assume we have a 1 digit number like 5, here N1 will divide 5 by 100 which will
result in .05 and the value of N1 will be 0 since N1 is an integer type variable. Then in N2 5 will
be divided by 10 which will be .5 and then we will take the remainder by 10 which will be the
same .5 therefore N2 will also be 0. And lastly in N3 we will take remainder by 10 which will be
5, and the value of N3 will be 5. This will work for 2 or 3 digit numbers.
After separating each digit, we put the values in out defined function “AS” and the value is
stored in a variable called “sum”.
Explanation of “AS” function: So we can assign 3 integers in AS function, in this case which is
N1, N2, N3 and the AS function will sum the cube of each digit and then return the value. To
cube each number, we have used “pow” function which helps us to find the cube of that
number. We could also use N1* N1* N1, but it looks messy.
Finally we have an “if” condition which will check whether the sum is equal to the number. If
the condition is true it will print “%3d is an Armstrong number.” else it will print “%3d is not an
Armstrong number.”. Here, %3d is for decorative purpose, nothing to do with code.
Code:
#include<stdio.h>
#include<math.h>
int AS(int N1,int N2, int N3);
int main(){
int num,N1,N2,N3,sum;
for(num=1;num<=500;num++){
N1 = num / 100;
N2 = (num / 10) % 10;
N3 = num % 10;
sum = AS(N1,N2,N3);
if(num == sum){
printf("%3d is an Armstrong number.\n",num);
}
else{
printf("%3d is not an Armstrong number.\n",num);
}
}
return 0;}
int AS(int x,int y,int z){
int SUM;
SUM=pow(x,3) + pow(y,3) + pow(z,3);
return SUM;}
Result:
Lab Assignment
Question :2
Explanation: In this problem we have to find the GCD (greatest common divisor) of two
numbers using recursive function. Here, we have declared a recursive function “gcd()” to
calculate GCD between 2 numbers. In the main function, we have declared ‘a’ and ‘b’ variables
where we assigned the input given by the user. Then we have used “gcd(a,b)” to find GCD.
How the GCD function works: We use recursion to find GCD where if a number is equal to 0, the
function will return the other number otherwise it’ll find the GCD between the given 2 numbers
by moduling a by b.
In the result, we used 20 and 10 as two inputs. As they are non-zero, the result is determined by
the module operator which determines the largest common divisor of the 2 integers which is 10.
Code:
#include<stdio.h>
int gcd(int x, int y) {
if(y==0)
return x;
else
return gcd(y,x%y);
}
int main() {
int a,b;
printf("Enter two numbers: ");
scanf("%d %d",&a,&b);
printf("The GCD of %d and %d is: %d\n",a,b,gcd(a,b));
return 0;
}
Result:
Lab Assignment
Question :3
Explanation: Here we have to find the LCM (lowest common multiple) using recursive
function. In order to solve this problem we need to calculate the GCD (greatest common
divisor) because the formula to find the LCM of two given number is (a x b)/GCD(a,b).
Explanation: This code is the same as previous code where we calculated the GCD of two
numbers using recursive function. As the formula to find the LCM of two number is
(a x b)/GCD(a,b), we just find the GCD of the given number using recursive function and then
divide the value of (a x b) by GCD value then print it using printf function.
Code:
#include<stdio.h>
int GCD(int a,int b);
int main(){
int a,b;
printf("Please input the 1st number:");
scanf("%d",&a);
printf("Please input the 2nd number:");
scanf("%d",&b);
printf("LCD of the numbers are:%d",(a*b)/GCD(a,b));
return 0;}
int GCD(int x,int y){
if(y==0){
return x;}
else {
return GCD(y,x%y);}}
Result:
Lab Assignment
Question :4
Explanation: In this problem we have to find the Fibonacci of a number (given by user)
using recursive function. The main difference between recursive function and user defined
function is that, user defined function cannot call itself. On the other hand recursive function
can call itself and perform a set of codes. However, in recursive function we must put
something called base case in order to stop the repeating.
Explanation: Here, to calculate Fibonacci number we have declared a recursive function “fibo”
which is an integer type. In the main function we have declared “num” variable where we will
assign the input from the user. Then we have used “fibo(num)” to find the value of the
Fibonacci of the number.
How the recursive function works: In the “fibo” function we will tell the what will it do. The
formula of Fibonacci is, F𝑛=F𝑛−1+F𝑛−2 (from the internet), where the base case or in Fibonacci
series, 1st and 2nd digits are 0 and 1. Here, in the code when the value of “num” is equal to 0 the
code will return or print 0 and when the value of “num” is 1, the code will return 1.
Here in this code, we have 2 examples. One where num or input in 15 and one with 5. The code
with input 15 is to show that the code works and with input 5 we tried to show how the code
works. When the value of “num” is 5 the value of fibo will be fibo(5)= fibo(4)+fibo(3) and fibo(4)
will again go through the function and the values will decrease until it becomes 1 or 0.
The easiest way to explain how the “fibo” function works is by doing by hand. Here, we have
attached a hand written note to understand the concept clear.
PLEASE TURN OVER
Code:
#include<stdio.h>
int fibo(int num);
int main(){
int num;
printf("Please enter a number:");
scanf("%d",&num);
printf("%d",fibo(num));
return 0;}
int fibo(int x){
if (x == 0){
return 0;}
else if (x == 1){
return 1;}
else{
return fibo(x - 1) + fibo(x - 2);}
}
Result:
Lab Assignment
Question :5
Explanation: Here we have a problem where we have to find the sum of digits of a number
using recursive function. We are given with the base case and recursive term. This code similar
to Fibonacci as the recursive function calls itself again and again until the base case is satisfied.
Explanation: Here we have declared a recursive function called ”sumd” which is an integer
type. In the main function we have an integer variable “n” where we will store the value of the
number given by the user, in this case n=581. Similar like Fibonacci we used “sumd(n)” to
calculate the value of sum.
How the recursive function works: Here, the initial value is 581 which doesn’t satisfy the base
case so the code returns (581%10)+ sumd(581/10) which is 1+sumd(58). Now for sumd(58) the
function runs again and since 58 is not equal to zero it fails to satisfy the base case, so it runs
the formula (n % 10) + sumd(n / 10) again until the value of n is equal to zero.
To fully understand the process how the code works we have attached a hand written note.
PLEASE TURN OVER
Code:
#include<stdio.h>
int sumd(int n);
int main(){
int n;
printf("Please enter any number:\n");
scanf("%d",&n);
printf("The sum of the digits of the number is:%d\n",sumd(n));
return 0;}
int sumd(int x){
if(x == 0){
return 0;
}
else{
return (x % 10) + sumd(x / 10);
}
}
Result:
Example
Question :1
Explanation: Here we have used user defined function to find the sum of two integers. To
use a user defined function to sum two integers we first need to declare the function before the
main() function, and inside the function we will have parameters that we will give input like
12,34,a,b. Here the function is “sum” which is a integer data type, and for parameters we have
“x” and “y”.
In the body of the function :
To make the function usable we have to define what the function will do in the code which we
have done in the last part of the program. Here, s=x+y shows that the function sum will sum
two integers when we use it.
To use the code we simply use “ s=sum(a,b); ”, this will give the sum of two integers.
Result:
Example
Question :2
Explanation: In this code we have summed the value of squared odd numbers till 25 with
the help of user defined function. In this case out function name is “func” which we have
declared before main function as we did in example 1.
In the body of the function :
This time in the body of the function we have used “for loop” this is to find the odd numbers
and to sum the value. In the ”for loop” we have intial value set to 1 and it is being incremented
by 1 each loop till it reaches 25. Here, “s+=num*num” means “s=s + num 2”, which will give the
summed value. Finally, the value of the function itself will be 2925, which we will then print by
using printf.
Result:
Example
Question :3
Explanation: Here, we have find the sum of the digits of an integer by using user defined
function. Here the name of the function is sum and inside is integer data type.
In the body of the function :
In order to sum numbers from a integer we first need to separate the numbers. We can do this
by dividing the value by 10 and taking the remainder, and then we to remove the last number
form the integer we simply divide the integer by 10 and since it is an integer, the fraction part
will be erased and we will again have whole integer number. we will do this until the value of
integer is 0 which is why we used a “while loop” with condition “n>0”. Lastly while in the loop
the values will be summed using “sum+=rem” where “rem” is remainder of the integer.
Finally, we give any integer and It will give us the summed value of the integer. In this case
number =2437, the answer will be sum=2+4+3+7=16.
Result:
Example
Question :4
Explanation: Here, we have calculated factorial using recursive function. In this case the
name of the function is “fact”.
In the body of the recursive function:
Here we have used “if” condition to check whether the value of integer (m) has reached 0. In
this case the function body acts as an loop. Outside the “if” condition we have “return
m*fact(m-1)” this means if m= 4 then we will have, 4*fact(3), here the fact(3) will again go to
the fact function body and check if the value of m is 0 or not, since the value of m is 3 the base
case is false and will execute 3*fact(2) and so on. Until the value of m=0, as soon as the value of
m is zero the condition will return 1.
Which means fact(0)=1 and it will go back and perform 1*1 =1 which is the value of fact(1),
which will then go back and perform 2*fact(1)=2*1=2 which is then the value of fact(2) and the
cycle will go on until we will get the factorial of 4 which is 24.
Result:
Practice Problems
Question :1
Explanation: Here we have a code that sums the value of two integers using user defined
function. Here we have a void type function called sum. This function will sum as well as print
the value of the sum. This code is same as example 1 but here the function doesn’t return any
value and does the printing itself. In this code we have two integers who’s values are 2 and 3.
So the sum will be 5.
Result:
Practice Problems
Question :2
Explanation: In this code, the user defined function multiplies the value given by user by 2.
And since it is an integer type function it will return values which in this case is “x”. In this code
the user given value is x=5 and the value of “y” is “func(x)”, so the value of x will go through the
user defined function, which is being multiplied by 2. Thus we print the value of y which is
“x=5*2=10”.
Result:
Practice Problems
Question :3
Explanation: In this code the function is void type and it sums the value of two integers. But
before summing, the value of 1st integer is divided by 2 and the 2nd integer in reduced by 1. In
this code the integer variables are “i” and “j”. The value is 5 and 10 respectively. But while the
value is assigned in the function the values are altered to “i/2” and ”j%3”. Which makes the
input value 2 and 1. So the result will be 1 since the value is further altered in the function
body.
Result:
Practice Problems
Question :4
Explanation: This code is similar to example 4 but instead of multiplying we are adding the
values until the given value has reach 0. In this code we have a recursive function called “fun”
which is a integer type and in the body it has “if” condition which checks whether the value is 0.
If the value is not 0 then it executes (n+fun(n-1)) it will continue until the value of n is zero. That
means for n=8 at first the code will execute 8+ fun(7), then again fun(7) will execute until the
value of “n” reaches zero. In the end we will get the sum of values , in this case
8+7+6+5+4+3+2+1=36
Result: