EEE 105 Lab Manual
EEE 105 Lab Manual
Lab Manual
Course : EEE 105
Credit Title : Computer Programming
Instructor : Nusrat Jahan Ananna, Lecturer, EEE Department
Objective
Familiarizing with computer and windows operating system
Familiarizing with C language and integrated development environment (IDE)
Your First C program.
An example of a program in C:
#include <stdio.h>
int main( )
{
printf(“Dhaka\tChittagong Khulna Sylhet");
printf(“\nBarishal \t\tRangpur");
return 0 ;
}
Output:
Dhaka Chittagong Khulna Sylhet
Barishal Rangpur
Problem 1:
Write a program to print the following in C language.
I live in Dhaka.
Problem 2:
Write a program to print the following in C language.
My Family Information-
Name: Devid
Father: Daud
Mother: Stella
Problem 4:
---------------------------------------------------
---------------------------------------------------
|| Name :-- MD. Al Amin ||
|| House No:-- 45, Street:- Fular Road||
||City:-- Dhaka, Post Code:-- 1217 ||
---------------------------------------------------
---------------------------------------------------
Problem 5:
Write a program to print the following in C language.
*
**
***
****
Problem 6:
Problem 7:
Page 2 of 3
----------- --------
| | | |
| | >>-------------------> | |
----------- ---------
Home Work:
* **** *****
** *** * *
*** ** * *
**** * *****
You have to print the above TEXT using printf and \n only.
Page 3 of 3
East West University
Department of Electrical & Electronic Engineering
Plot No-A/2, Main Road, Jahurul Islam City, Aftabnagar, Dhaka-1219
Lab Manual
Course : EEE 105
Credit Title : Computer Programming
Instructor : Nusrat Jahan Ananna, Lecturer, EEE Department
In the previous lab we go through the printf in detain. So, we can start with scanf. As
discussed in the class scanf is the C library’s counterpart to printf. Actually, scanf requires
a format string to specify the appearance of the input data. Example of using scanf to read
an int value:
scanf("%d", &i);
The above code will read an integer and stores into variable i. The & symbol is usually
(but not always) required when using scanf.Now write the following code:
#include <stdio.h>
int main()
{
int i;
printf("Enter The Number i\n");
scanf("%d",&i);
printf("Your Entered Integer is = %d",i);
return 0;
}
3. If not then there is obviously something wrong in your code, in that case knock your
teacher.
4. If there is nothing wrong enter 10 form your keyboard and press enter.
Page 1 of 4
5. Your output should be as follows, otherwise knock your teacher.
6. Now execute the code again and take 10.50 form your keyboard.
7. The output should be the same as in step 5. In output .50 is missing, this is because we
take the variable i as an integer. If we want to print the exact 10.50, we should declare
variablei as float or double.
8. Reading a float value requires a slightly different call of scanf:
scanf("%f", &i);
"%f" tells scanf to look for an input value in float format i.e. the number may contain a
decimal point, but doesn’t always have to.
9. If youdeclare i as double your scanf should be as follows:
scanf("%lf", &i);
"%lf" tells scanf to look for an input value in double format.
10. For the printf you have to use %d or %f or %lf respectively. Look at table 1 for more
detail. Don’t worry about the char and string we will discuss more detail in the
upcoming labs.
printf scanf
int %d %d
float %f %f
double %f %lf
char %c %c
string %s %s
11. If you want to enter more than one value i.e., serialized the inputs you can do it by the
following code.
Exercise 1:In this initial exercise you are asked to calculate the volume of a room. As
you know volume = width ×height×length.In the C this formula will be volume =
height * length * width;
That is operation * in C is same as × in mathematics. For more operators in C look at
following table
Your job is to declare width, height and length as double. Write the code to take these as
input from keyboard. Print the volume.
Page 2 of 4
Sample input Sample Output
Exercise 3:Write a program to calculate the area and the perimeter of a circle using the
formulas area = r2 and perimeter = 2r, where is a constant whose value is
3.1415926 and r is the radius of the circle. Use float type variable for r.
Exercise 4: Compute the straight line distance between two points in a plane. i.e. your job
is to take the points as input from keyboard and print the outputs. The coordinates of
points should be declared as float and for this you should know how to use sqrt()with
#include<math.h>as discussed in the class.
Page 3 of 4
Home Work:
Problem 1: A computer manufacturing company has the following monthly compensation
policy to their sales-persons:
Minimum base salary : 1500.00
Bonus for every computer sold : 200.00
Commission on the total monthly sales : 2 percent
Given the base salary, bonus and commission rate, the inputs necessary to calculate the
gross salary are, the price of each computer and the number sold during the month.
Calculate the gross salary of a sales-person.
Problem 2: Write a program to convert the distance in miles and yards to kilometer. To
convert miles to kilometers, multiply by the conversion factor 1.609. [1 mile = 1760
yards]
Enter the distance in miles and yards The distance in kilometer is: 42.185970
Miles: 26 km
Yards: 385
Problem 3: Write a program to evaluate the following function for different values of x.
[Hints: use math.h functions, pow(), sin(), sqrt() ]
f(x) = 3x5 – 5x -6sin(x)
Page 4 of 4
East West University
Department of Electrical & Electronic Engineering
Plot No-A/2, Main Road, Jahurul Islam City, Aftabnagar, Dhaka-1219
Lab Manual
Course : EEE 105
Credit Title : Computer Programming
Instructor : Nusrat Jahan Ananna, Lecturer, EEE Department
Lab-3: Selection Statements if, if-else, if-else-if, Switch and Conditional operators
As discussed in the class, selection structures are used to choose among alternative
courses of action: In this problem we want to check whether a student is passes or failed.
According to the rule of EWU If the marks of a student is greater than or equal to 60
he/she “Passed”. Thus, the flowcharts of this problem will be as follows:
if(grade>=60)
printf( "Passed\n" );
i. Fill the printf and scanf lines such that the output is as follows:
Page 1 of 5
ii. As you see there is no output for the input 59 .This is because if statement is a single-
entry/single-exit structure, i.e. our code will perform action for the true condition only.
iii. Using an else concatenate with if we can easily covert the above code such it can perform
an action (for example print failed) for the false condition too. In that case, it flow chart
will be as follows
iv. In the above code replace the if parts with the following code.
if(expression)
Statement
Page 2 of 5
Placing braces around a group of statements forces the compiler to treat it as a single
statement, i.e., for the following C code, all n statements will be executed if the
expression is true.
if(expression)
{
Statement_1;
Statement_2;
… … …
Statement_n;
}
Exercise 2: Consider the following flowchart, and Modify your code of exercise 1, such that
it can comply outputs shown in right
Outputs
Note: For this you should use separate printf for each text i.e., one printf for Sorry, one for
Failed, one for Congratulations and another one for Passed .
Exercise 3: You are given two integer numbers. Find the quotient and remainder. When 2nd
input is zero, you should print “Error!! Can’t divide.”
Exercise 4: In this exercise you need to write a program that will take an integer input
representing year and print whether this is a leap year or not leap year.
Page 3 of 5
Exercise 5: Write a C code that can check whether a number is odd or even. Your number
should be input from the keyboard. If the number is odd your program should print “The
number you entered is ODD” otherwise it should print “The number you entered is EVEN”.
A number is odd if the number mod 2 equals to zero, otherwise it is an even number.
Exercise 6: Given a score and the following grading scale write a program with if-else-if only
to find the corresponding grade.
90-100 A
80-89 B
70-79 C
60-69 D
0-59 F
Your program should have proper error checking. For example, if a user input a negative
number or more than 100 it should print “Invalid input.”
Exercise 7: Suppose we have two tasks A and B, A takes Ah hours, Am minutes, and As
seconds. On the other hand B takes Bh hours, Bm minutes, and Bs seconds. Write if-else
statements to print out which task takes more time?
Exercise 8: Write a program that reads 3 integer numbers a, b and c from user and computes
minimum, median and maximum of the numbers.
Page 4 of 5
Sample input Sample Output
Please Enter a = 2 Min = 2, Max = 5, Median = 3
Please Enter b = 5
Please Enter c = 3
Please Enter a = 2 Min = 2, Max = 3, Median = 2
Please Enter b = 2
Please Enter c = 3
Page 5 of 5
East West University
Department of Electrical & Electronic Engineering
Plot No-A/2, Main Road, Jahurul Islam City, Aftabnagar, Dhaka-1219
Lab Manual
Course : EEE 105
Credit Title : Computer Programming
Instructor : Nusrat Jahan Ananna, Lecturer, EEE Department
Loop control statements are one of the most important parts of structured programming. The basis
of program control starts with loop. In this lab we will go to the detail of C’s while loop and do
while loop. A while statement is a C looping statement. It allows repeated execution of a statement
or block of statements as long as the condition remains true (nonzero). If the condition is not true
when the while command is first executed, the statement(s) is never executed. The form while
Statement is following:
while (condition)
statement
Here, condition is any valid C expression, usually a relational expression. When condition
evaluates to false (zero), the while statement terminates, and execution passes to the statement
followingfirst statement; otherwise, the first C statement in the statements is executed. Statement is
the C statement that is executed as long as condition remains true. Note that, thestatement
issingular here too, not plural alike if-else-if. To make awhile statement control two or more
statements, generally we use a compound statement. Compound statement has the form:
{
Statement_1;
Statement_2;
… … …
Statement_n;
}
Placing braces around a group of statements forces the compiler to treat it as a single statement,
i.e., for the following C code, all n statements will be executed while the expression is true.
while(condition)
{
Statement_1;
Statement_2;
… … …
Statement_n;
}
Page 1 of 5
As you can imagine, this flow chart continuously print the value of x and i until i is becomes
greater than 9. In other words, it allows repeated execution of a statements x=x*i, and i=i+1 as
long as the (i<=9) remains true. The C code for this problem is as follows,
int main()
{
int x = 1, i =1;
while (i<=9)
{
x = x*i;
i = i+1;
printf("\nThe value of x and i is %d, %d, respectively\n", x,i );
}
return 0;
}
Output:
Your1sttask is to write the above code and match your output will given one.
Can you code program this without loop? Let’s try it for 10 Mins,
Exercise: 2 In this program we want to take input from command line until we get one greater
than 99, i.e., our program should repeatedly take inputs form keyboard and print it values.
However, if the value of the input is greater than 99 we stop. Write the following code and check
you output.
Page 2 of 5
scanf("%d", &nbr );
printf("You Enter -> %d\n", nbr );
}
Match your output with the given two on right. Next, your task to draw a Flowchart(in a paper)of
the above program. Can you solve this problem without loop. The ans is ________.
Exercise: 3C's second loop construct is the do...while loop, which executes a block of statements
as long as a specified condition is true. The do...while loop tests the condition at the end of the
loop rather than at the beginning, as is done by the while loop.The structure of the do...while loop
is as follows:
do
statement
while (condition);
Here, condition is any C expression, and statement is a single or compound C statement. When
program execution reaches a do...while statement, the following events occur:
The statements associated with a do...while loop are always executed at least once. This is because
the test condition is evaluated at the end, instead of the beginning, of the loop. In contrast, while
loops evaluate the test condition at the start of the loop, so the associated statements are not
executed at all if the test condition is initially false.The do...while loop is used less frequently than
while loops. It is most appropriate when the statement(s) associated with the loop must be
executed at least once.
Now, your task is to Code the Exercise 1 and 2 with do while loop. The output should be
identical as shown in that exercise.
Page 3 of 5
Exercise: 4Write a program that will take an integer number `n` as input and print the result of the
following series:
a. 1+2+3+. . .+n
b. 1+1/2+1/3+. . . + n
c. 1.2+3.4+5.6+. . .+(n-1).n
d. 1.2.3+2.3.4+3.4.5+. . .+n.(n+1).(n+2)
e. 1+2+4+7+11+ . . . (up to `n` numbers)
Exercise 5: Write a program that reads two positive integers corresponding to two year values,
ensures that the first year value is less than the second, and then determines and outputs all year
values for leap years. A leap year is one that can be evenly divided by 4, unless it is a centennial,
in which case it must be evenly divided by 400. For example, 1600 and 1992 are leap years,
whereas 1700 and 1998 are not. Your program should output all the leap years between this two
input years.
Exercise 6: Here, we will solve the Grading problem (LAB 2) in a more specific way. Now, the
grading of each course is based on the following weighted scale:
- Term 1 – 20%
- Term 2 – 20%
- Final – 30%
- Attendance – 10%
- Class Tests – 20%
The letter grades are given based on the total marks obtained by a student and is shown below:
• A >= 90%
• B >= 80% &< 90%
• C >= 70% &< 80%
• D >= 60% &< 70%
• F < 60%
Term 1 and Term 2 exams are out of 20 each, Final is out of 30 and Attendance givenis out of 10.
Three class tests are taken per semester and the average of best two iscounted towards the final
grade. Every class test is out of 20. Example: Say Tara obtained marks of 15, 18, 25 and 8 in Term
1, Term 2, Final and Attendance respectively. Her 3 class test marks are 15, 12 and 17. Since
average of best 2 will be counted, her class test mark will be equal to (15 + 17) / 2 = 16. Therefore,
total marks = 15 + 18 + 25 + 8 + 16 = 82 and she will be getting a B.
Input: The first line of input is an integer T(T<100) that indicates the number of test cases.Each
case contains 7 integers on a line in the order Term1 Term2 Final Attendance Class_Test1
Class_Test2 Class_Test3. All these integers will be in the range [0, total marks possible for that
test].
Output: For each case, output the case number first followed by the letter grade {A B C D
F}.Follow the sample for exact format.
Page 4 of 5
Sample input Sample Output
3 Case 1: B
Case 2: A
15 18 25 8 15 17 12 Case 3: B
20 20 30 10 20 20 20
20 20 30 10 18 0 0
Home Works :
1. Write a program that will take a number `n` as input and print `n` lines of output
according to the following sample output.
2. Write a program to read an integer number `n` and print the value of 3^n(using loop).
Page 5 of 5
East West University
Department of Electrical & Electronic Engineering
Plot No-A/2, Main Road, Jahurul Islam City, Aftabnagar, Dhaka-1219
Lab Manual
Course : EEE 105
Credit Title : Computer Programming
Instructor : Nusrat Jahan Ananna, Lecturer, EEE Department
Lab- 5:Array
Exercise 1:
Type the following code and match your output with the given output.
#include <stdio.h>
main()
{
float expenses[13];
int count;
for (count = 1; count < 13; count++)
{
printf("Enter expenses for month %d: ", count);
scanf("%f", &expenses[count]);
}
for (count = 1; count < 13; count++)
{
printf("Month %d = $%.2f\n", count, expenses[count]);
}
return 0;
}
Output:
Enter expenses for month 1: 100
Enter expenses for month 2: 200.12
Enter expenses for month 3: 150.50
Enter expenses for month 4: 300
Enter expenses for month 5: 100.50
Enter expenses for month 6: 34.25
Enter expenses for month 7: 45.75
Enter expenses for month 8: 195.00
Enter expenses for month 9: 123.45
Enter expenses for month 10: 111.11
Enter expenses for month 11: 222.20
Enter expenses for month 12: 120.00
Month 1 = $100.00
Month 2 = $200.12
Month 3 = $150.50
Month 4 = $300.00
Month 5 = $100.50
Month 6 = $34.25
Month 7 = $45.75
Month 8 = $195.00
Month 9 = $123.45
Page 1 of 3
Month 10 = $111.11
Month 11 = $222.20
Month 12 = $120.00
Exercise 2:
In this program youhave to average 10 persons’ individual score using a single array and a single for
loop. Your output should be as follows:
Output:
Enter Person 1's score: 95
Enter Person 2's score: 100
Enter Person 3's score: 60
Enter person 4’sscore: 100
Enter Person 5's score: 25
Enter Person 6's score: 0
Enter Person 7's score: 85
Enter Person 8's score: 85
Enter Person 9's score: 95
Enter Person 10's score: 85
The average score is 73
Exercise 3:
This program declares and initialize an array a, in which each element has the same value as its
subscript. i.e. a[i] = i. Add another loop that will multiply all the even numbers in the array by 3.
Add a third loop to display all the values in the array on one line with a single space between each
value.
Output:
0 1 6 3 12 5 18 7 24 9
Exercise 4:
Write a program that declares an array A and inputs n integer values in A. Then the contents of array
A is copied to another array B in reversed order. Finally print the elements of array B.
Output:
Input the size of the array A: 10
Input the values of A: 10 20 30 5 22 11 44 78 55 66
Array B: 66 55 78 44 11 22 5 30 20 10
Exercise 5:
The Fibonacci numbers are numbers of an interesting sequence in which each number is equal to the
sum of the previous two numbers. In other words,
Fi = Fi - 1 + Fi – 2
where Fi refers to the ith Fibonacci number. By definition 1st Fibonacci number is 0 and 2nd Fibonacci
number is 1.
F1 =0 and F2= 1
Hence,
F3 = F2 + F1 = 0 + 1 = 1
F4 = F3 + F2 = 1 + 1 = 2
F5 = F4 + F3 = 1 + 2 = 3 and so on.
Write a C program that will generate first n Fibonacci numbers. Do the following steps:
1. Declare an array of 100 integer values without initializing the elements.
2. Use two assignment statements to set the first two elements to be equal to 0 and 1
respectively.
Page 2 of 3
3. Use a loop to set the values of the remaining elements, so that (apart from the first two
elements) each element in the array is equal to the sum of the previous two elements in the
array.
Output:
How many Fibonacci numbers you want to generate: 15
The series is: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Exercise 6:
Write a program that will find the smallest number from a list of unsorted numbers. Do the following
steps:
1. Declare an array A of 50 integers
2. Input n number of integer values in A
3. Search for the smallest value among these n values in array A.
4. Display the smallest value
Output:
Input number of elements in the array: 11
Input values: 19 59 278 25 36 568 5 98 45 87 96
The SMALLEST value is: 5
Page 3 of 3
East West University
Department of Electrical & Electronic Engineering
Plot No-A/2, Main Road, Jahurul Islam City, Aftabnagar, Dhaka-1219
Lab Manual
Course : EEE 105
Credit Title : Computer Programming
Instructor : Nusrat Jahan Ananna, Lecturer, EEE Department
Lab- 8:String
Exercise 1:
Write a program that will take two strings as input and print them.
Exercise 2:
Write a program that will take one string and copy it into other string without using strcpy().
Exercise 3:
Write a program that will take two strings as input and concatenate them without using strcat().
Exercise 4:
Write a program that will take two strings as input and find out their length without using
strlen().
Exercise 5:
Find the frequency of a particular character in a string.
Exercise 6:
Input a string and print the reverse of it.
Assignments:
Problem 1:
Remove characters in a string except alphabets.
Problem 2:
Take a string input in lower case and convert it into upper case and print it.
Problem 3:
Take a string input in lower case and convert it into upper case and print it.
Page 1 of 1
East West University
Department of Electrical & Electronic Engineering
Plot No-A/2, Main Road, Jahurul Islam City, Aftabnagar, Dhaka-1219
Lab Manual
Course : EEE 105
Credit Title : Computer Programming
Instructor : Nusrat Jahan Ananna, Lecturer, EEE Department
Lab-6:C Functions
Exercise 1: A prime number is a natural number greater than 1 that has no positive divisors other
than 1 and itself. Write a user define function to check a number whether it is prime or not. If the
number is prime print “Prime” else print “Not Prime” and the minimum factor of it. Include your
function in a working C program.
OUTPUT
Enter the number: 5
Prime
Enter the number: 9
Not Prime
Minimum factor is 3
Exercise 2: Write a C program to find the factorial of a given number with recursion. Write a
user defined function for finding the factorial of n, using the formula n*fact(n-1).
OUTPUT:
Enter the number to find the factorial
5
The factorial of a number 5 using recursion is 120
Exercise 3:The Fibonacci numbers are numbers of an interesting sequence in which each
number is equal to the sum of the previous two numbers. In other words,
Fi = Fi - 1 + Fi – 2
where Fi refers to the ith Fibonacci number. By definition first two Fibonacci numbers is equal
to 1; i.e.,
F1 = 0
F2 = 1
Hence,
F3 = F2 + F1 = 0 + 1 = 1
F4 = F3 + F2 = 1 + 1 = 2
F5 = F4 + F3 = 2 + 1 = 3
and so on.
Write a C function that will generate first n Fibonacci numbers. Include your function in a
working C program.
OUTPUT:
Page 1 of 2
Enter the number: 7
First 7 Fibonacci numbers are: 1, 1, 2, 3, 5, 7 and 12
Exercise 4:Write a C function that calculates the value of weight z subject to the following
conditions:
x 2 4 y if x y
z = x 2 4 y if x y
( x y ) 3/ 4 if x y
Then write a C main program that reads the values for x and y and calls the developed function
for calculating the value of weight z.
OUTPUT:
Enter x: 3
Enter y: 5
Value of z is: 29
Enter x: 5
Enter y: 2
Value of z is: 17
Page 2 of 2
East West University
Department of Electrical & Electronic Engineering
Plot No-A/2, Main Road, Jahurul Islam City, Aftabnagar, Dhaka-1219
Lab Manual
Course : EEE 105
Credit Title : Computer Programming
Instructor : Nusrat Jahan Ananna, Lecturer, EEE Department
1. Write a short C program that declares and initializes (to any value you like) a double, an int, and a
string. Your program should then print the address of, and value stored in, each of the variables. Use
the format string "%p" to print the addresses in hexadecimal notation (base 16). You should see
addresses that look something like this: "0xbfe55918". The initial characters "0x" tell you that
hexadecimal notation is being used; the remainder of the digits give the address itself.
2. Write a C program to swap the values stored in two different variables. Write a function swap() to
swap the elements.
3. Write a function with sample program which will take an array of integer, and the size of the
array, and print the elements of array.
4. Write a C program that displays the average of the array values and a table of differences between
each array element and the mean. Use a C function to find the average of the array elements.
Page 1 of 1
East West University
Department of Electrical & Electronic Engineering
Plot No-A/2, Main Road, Jahurul Islam City, Aftabnagar, Dhaka-1219
Lab Manual
Course : EEE 105
Credit Title : Computer Programming
Instructor : Nusrat Jahan Ananna, Lecturer, EEE Department
Lab- 9:Structure
1. Write a C program for the following: Declare a struct named company, which has
members - name of type string, income of type float, and cost of type float. Using
company declares an array of size 20 elements and write a program to read the
information about all 20 companies. Finally print the profit made by each company in the
array using the following formula
profit = income – cost
2. Write a C program for the following: Declare a struct named rectangle which has
threemembers: length, width and area of type float. Then create an arrayof ten rectangles
prompting from the user the length and width of each rectangle. Next,compute the area of
each rectangle in the list by calling a function named area. Finally print the length, width
and area of each rectangle.
Page 1 of 1
East West University
Department of Electrical & Electronic Engineering
Plot No-A/2, Main Road, Jahurul Islam City, Aftabnagar, Dhaka-1219
Lab Manual
Course : EEE 105
Credit Title : Computer Programming
Instructor : Nusrat Jahan Ananna, Lecturer, EEE Department
Lab-10:Files
Exercise 1:Write a C program to copy one file into another. Create two text files named as input
and output, where input file acts as a source file where the data to be copied is stored and the
output file is the destination file for storing the copied data. Get the data from source file (opened
in read mode) using gets function and copy the data to the destination file.
OUTPUT:
Enter the source file name to be copied:input.txt
Enter the destination file name:output.txt
Copy completed
Input.txt:
Good morning
Output.txt: (after copying)
Good Morning
Exercise 2: Write a C program that reads 10 integer values from an input file input.txt one by
one and check whether the number is even or odd. If the number is even it stores it in a file called
even.txt, otherwise if the number is odd it stores it in a file called odd.txt.
OUTPUT:
Data items in input file
2 6 4 8 10 12 89 68 45 37
Data items in even.txt file
2 6 4 8 10 12 68
Data items in odd.txt file
89 45 37
Page 1 of 2
Home Assignment
Problem 1:
Write a C program that reads the values in the elements of the 10-element array A and sorts the
values into descending order. Use input and output files.
OUTPUT:
Data items in input file
2 6 4 8 10 12 89 68 45 37
Data items in output file
89 68 45 37 12 10 8 6 4 2
Problem 2:
One student has sit for seven tests for a particular course. The final mark for that course is the
average of six tests' marks (the lowest mark will be thrown out). Using two-dimensional array,
input and output files, write a C program that calculates the final marks for 15 students who took
that course.
Page 2 of 2