Mirpur University College
Assignment
Topic Previous Question from 2017 to 2020
Course Name Structured Programming Language
Course Code 510201
Submitted To Ohedul Islam
Lecturer
Dept of CSE
Submitted By Md Faisal Mahmud
Roll: 5373
Reg No: 20502004242
1st Year 1st Semester
Session: 2020-21
Submission Date 31 May, 2022
Board Question 2017
Q: What do you mean by variable?
Ans: C variable is named location in a memory where a program can manipulate the data. This
location is used to hold the value of the variable. The variable may get change in the program. C
variable might be belonging to any of the data type like int, float, char etc.
Example: int I, j, k.
Q: How can you declare and initialize a variable using C program?
Ans: Declaring and initializing C variable
• Variables should be declared in the C program before to use
• Memory space is not allocated for a variable while declaration. It happens only on variable
definitions.
• Variables initialization means assigning a value to the variable.
Here is an example of declaring an integer, which we’ve called some_number.
int some_number;
This statement means we’re declaring some space for a variable called some_number, which will be
used to store integer data. Note that we must specify the type of data that a variable will store.
int some_number = 3;
This is called initialization. We can also assign a variable and the value of the variable.
Q: Write a program that will convert the given temperature in Fahrenheit to Celsius using the
following formula c=(f-32)/8
Ans: Here is the program-
#include <stdio.h>
int main()
{
float F,C;
printf("Enter Farenhite value to convert\n");
scanf("%f",&F);
C=(F-32)/8;
printf("%f to celsius is %f",F,C);
return 0;
}
Q: Write a program in C to calculate the area and circumference of a circle
Ans: Here is the program-
#include<stdio.h>
int main()
{
int circle_radius=7;
float PI=3.1416,circle_circumference,area;
circle_circumference=2*PI*circle_radius;
area=PI*circle_radius*circle_radius;
printf("Area of the circle is %.2f\n",area);
printf("Circumference of the circle is %f",circle_circumference);
return 0;
}
Q: What are keywords and identifiers ? Define with proper example
Ans: Keywords are predefined, reserved words used in programming that have special meanings to
the compiler. Keywords are part of the syntax, and the can not be used as an identifier. For example:
int money;
Here , int is a keyword that indicates ‘money’ is a variable type of integer.
Identifiers refers to name given to entities such as variables, functions, structures etc.
Identifiers must be unique. They are created to give unique name to a entity to identify it during the
execution of the program. For example:
int money;
double account;
here, money and account are identifiers. Also remember identifier name must be different from
keywords. You can not use int as an identifier because int is a keyword.
Q: Write down the general form and flow chart of nested if else statement
Ans: The if...else statement executes two different codes depending upon whether the test
expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The
nested if...else statement allows you to check for multiple test expression and execute different
codes for more than two conditions.
Here is the flowchart of if-else statement:
Q: Write down the general form of if and if else statement
Ans: If: The if statement evaluates the test expression inside the parenthesis. If the test expression is
evaluated to true (non zero), statements inside the body of if is executed. If the test expression is
evaluated to false (0), statement inside the body of it is skipped from execution.
Here is the flowchart of if statement:
If-else: If...else statement is a two way branching statement. It consists of two blocks of statements
each enclosed inside if block and else block respectively. If the condition inside if statement is true,
statements inside if block are executed , otherwise statements inside else block are executed. Else
block is optional , and it may be absent in a program.
Here is the flowchart of if-else statement:
Q: Explain entry controlled and exit controlled loop
Ans: Entry controlled loop: Loop, where test condition is checked before entering the loop body is
known as entry controlled loop. Entry controlled loop are used when checking of test condition is
mandatory before executing loop body.
Example: While loop, for loop.
Exit controlled loop: Loop, where test condition is checked after executing the loop body , known
as exit controlled loop. Exit controlled loop is used when checking of test condition is mandatory
after executing the loop body.
Example: do while loop.
Q: Write the format of for loop
Ans: For loop: The syntax of for loop is:
for(initialization statement ; test expression ; update statement)
{
codes
}
The initialization statement is executed only once. Then , the test expression is evaluated . If the test
expression is false , for loop is terminated . But if the test expression is true , codes inside the body
of for loop is executed and the update expression is updated. This process repeats until the test
expression is false. The for loop is commonly used when the number of iteration is known.
Flowchart of for loop is here
Q: What are the different type of loop C has?
Ans: Loops are used in programming to repeat a specific block until some end condition is met.
There are three loops in C programming.
1. for loop
2. while loop
3. do while loop
Q: What is an Array?
Ans: Arrays are set of elements having same data type or we can say that arrays are collection of
elements having same name and same data type but always remember arrays are always start from
its index value and the index of arrays is start from 0 to n-1.
Q: With example show how can declare and initialize two-dimensional array
Ans: The two-dimensional array is also called a matrix. Here is a sample program that stores roll
number and marks obtained by a student side by side in a matrix.
#include <stdio.h>
int main()
{
int student[4][2];
int i;
for(i=0;i<=3;i++){
printf("Enter roll and marks");
scanf("%d%d",&student[i][0],&student[i][1]);
}
for(i=0;i<=3;i++){
printf("\n\nRoll:%d\nMark:%d",student[i][0],student[i][1]);
}
return 0;
}
There are two parts to the program, in the first first part through a for loop we read in the values of
roll no. and marks, whereas , in second part through another for loop we print out these values.
Look at the scanf() statement used in the first for loop:
scanf("%d%d",&student[i][0],&student[i][1]);
in stud[i][0] and stud[i][1] the first subscript of the variable stud, is row number which change for
every student. The second subscript tells which of the two columns are we talking about – the zeroth
column which contains the roll no or the first column which contains the marks. Remember the
counting of rows and columns begin with zero.
Board Question 2018
Q: What is structured programming language?
Ans: Structured programming is a logical programming method that is considered a precursor to
object oriented programming (OOP). Structured programming facilitates program understanding
and modification and has a top-down design approach, where a system is divided into compositional
subsystems.
Q: Difference between algorithm and flow chart?
Ans: The difference between algorithm and flowchart is here-
Basis for comparison Algorithm Flow chart
Comprehensibility Includes sequence of steps An information diagram made
which depicts the procedure od up of different shapes shows the
the solution. data flow.
Usability Hard to understand Easily interpreted
Uses Text Symbols
Implements No rules are employed Predefined rules are employed
Debugging Easier Predefined rules are employed
Ease of construction Perplexing Predefined rules are employed
Q: State the names of different storage class specifier. Write down the scope and lifetime of
each of them.
Ans: in C language each variable has a storage class which decides the following things:
• scope i.e in which all functions, the value of the variable would be available.
• Default initial value i.e if we do not explicitly initialize that variable, what will be its default
initial value.
• Lifetime of that variable i.e for how long that variable will exist.
The following storage classes are most often used in C programming,
1. Automatic variables
2. External variables
3. Static variables
4. Register variables
Automatic variables:
Scope: Local to the method block in which the variable is defined.
Lifetime: Till the end of method block where the variable is defined.
External or Global; variables:
Scope: Global i.e everywhere in the program.
Lifetime: Till the whole program doesn’t finish it’s execution.
Static variables:
Scope: Local to the block in which the variable is defined.
Lifetime: Till the whole program doesn’t finish it’s execution.
Register variables:
Scope: Local to the function which is declared.
Lifetime: Till the end of method block, where the variables is defined.
Q: Describe the four basic data types?
Ans: C has concept of data types which are used to define a variable before it’s use. The definition
of a variable will assign storage for the variable and define the type of data that will be held in the
location. The value of a variable can be changed anytime. C has the following basic built-in data
types :-
• int
• float
• double
• char
Integer data type: Integer data type is used to store a value of numeric type. Memory size of
integer data type is in 32 bit computer is 4 bytes where in 16 bit it is 2 bytes. Keyword int is used to
declare variables of type integer.
Example: int i= 2015;
Floating point data type: Floating point data type in C is used to store a value of decimal values.
Memory size of floating point data type is in 32 bit is 8 bytes where in 16 bit it is 4 bytes. Keyword
float is used to declare floating point variables.
Example: float i= 3.1345;
Double: Double point data type is similar to float data type except it provides up-to 10 digit of
precision and occupies 8 bytes of memory.
Example: double i= 1167.23467885432;
Char data type: Smallest addressable unit of the machine that can contain basic character set. It is
an integer type. Actual type can be either signed or unsigned.
Example: char name=’A’;
Q: Distinguish between local and global variables with example?
Ans: Distinguish between local and global variables:
Basis comparison Local variables Global variables
Declaration Variables are declared inside a Variables are declared outside
function any function.
Scope Within a function, inside which Throughout the program.
they are declared.
Access Accessed only by the statement, Accessed by any statement in
inside a function in which they the entire program.
are declared.
Life Created when the function Remain n existence for the
block is entered and destroyed entire time your program is
upon exit. executing.
Storage Local variables are stored on Stored on a fixed location
the stack, unless specified. decided by a compiler.
Q: What do you mean by library function?
Ans: Library function in C language are inbuilt functions which are grouped together and placed in
a common place called library.
Each library function in C performs specific operation. We can make use of these library functions
to get the predefined output instead of writing our own code to get those outputs. All C standard
library functions are declared in many header files which are saved as file_name.h.
Q: What are the different type of operators used in C program?
Ans: The symbols which are used to perform logical and mathematical operations in a C program
are called C operators. C language offer many types of operators. They are-
1. Arithmetic operators
2. Assignment operators
3. Relational operators
4. Logical operators
5. Bit wise operators
6. Conditional operators
7. Increment/decrement operators
8. Special operators
Q: Distinguish between a-- and –a?
Ans:
--a a--
--a is pre-decrement a-- is the post-decrement
Value of a is decremented before assigning it to Value of a is decremented after assigning it to
the variable a. variable a.
Example: Example:
#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int i=5; int i=5;
printf(“%d\n”,-i); printf(“%d\n”,i-);
return 0; return 0;
} }
output: 4 output: 5
Q: Write down the general form and flow chart of Switch statement?
Ans: The control statement that allows us to make a decision from the member of choices is called a
switch.
Form of switch statement:
switch(integer expression)
{
case constant 1:
do this;
case constant 2:
do this;
default:
do this;
}
The integer expression following the keyword switch is any C expression that will yield an integer
value. It could be an integer constant like 1,2 or 3, or an expression that evaluates to an integer. The
“do this” lines in the above form of switch represent any valid C statement.
Switch statement flow chart :-
Q: What do you mean by looping?
Ans: Execution of a statement or set of statement repeatedly is called as looping. In computer
programming, a loop is a sequence of instruction that is continually repeated until a certain
condition is reached. Typically, a certain process is done, such as getting an item of data and
changing it, and then some condition is checked such as whether a counter has reached a prescribed
number.
Q: Write a program that takes an integer as input and display it in reverse order.
Ans:
#include <stdio.h>
int main()
{
int x,y;
scanf("%d",&x);
while(x!=0){
y=x%10;
printf("%d",y);
x=x/10;
}
return 0;
}
Board Question 2019
Q: Define algorithm and flow chart.
Ans: Algorithm: An algorithm is a procedure or formula for solving a problem, based on conducting
a sequence of specified actions.
Example of algorithm:
Step 1: Start
Step 2: Read A,B
Step 3: C=A+B
Step 4: Print C
Step 5: Stop
A flowchart is the graphical or pictorial representation of an algorithm with the help of different
symbols, shapes and arrows in order to demonstrate a process or a program.
Several standard graphics are applied in a flowchart :
Example:
Q: Write down the characteristics of of structured programming language?
Ans: The main characteristics of a structured programming are:-
1. The code should be in modular nature.
2. There should be single entry and single exit for each module.
3. At least one construct each for sequence, condition and iteration.
Q: What is pre-processor directive and header file? Give example.
Ans: The C pre-processor modifies a source file before handing it over to the compiler, allowing
conditional compilation with #ifdef, defining constants with #define, including header files with
#include.
Example: #include,#define,#undef,#if,#ifdef,#error,_FILE_,_LINE_,_TIME_
Q: Explain increment and decrement operator with example.
Ans: Increment operators are used to increase the value of the variable by one and decrement
operators are used to decrease the value of the variable by one in C program.
Example:
increment operator: ++a, a++
decrement operator: --a,a--
Q: Write a C program to determine the largest value from three numbers.
Ans:
#include <stdio.h>
int main()
{
int num1, num2, num3;
printf(" Enter the number1 = ");
scanf("%d", &num1);
printf("\n Enter the number2 = ");
scanf("%d", &num2);
printf("\n Enter the number3 = ");
scanf("%d", &num3);
if (num1 > num2)
{
if (num1 > num3)
{
printf("\n Largest number = %d \n",num1);
}
else
{
printf("\n Largest number = %d \n",num3);
}
}
else if (num2 > num3)
{
printf("\n Largest number = %d \n",num2);
}
else
{
printf("\n Largest number = %d \n",num3);
}
return 0;
}
Q: What are the difference between while and do while loop?
Ans:
While Do While
Condition is at top Condition is at bottom
No necessity of bracket if there is single Bracket are compulsory even if there is a single
statement body. statement.
There is no semicolon at the end of while. The semicolon is compulsory at the end of do
while.
Computer executes the body if and only if Computer executes the body at least once even if
condition is more important. the condition is false.
This should be used when condition is most This should be used when process is most
important. important.
This loop is also referred as entry controlled This loop is also referred as exit controlled loop.
loop.
Example: Example:
while(i<10) Do
{ {
printf(“code”); printf(“code”);
} }while(i<10);
Q: Write a program in C to find the factorial of an integer.
Ans:
#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}
Q: Write the advantage and disadvantages of array.
Ans: Advantage of an array:
• It is better and convenient way of storing the data of same data type with same size.
• It allows us to store known number of element in it.
• There is no memory overflow or shortage of memory in arrays.
• Iterating the arrays using their index is faster compared to any other method.
Disadvantage of an array:
• It allows us to enter only fixed number of elements into it. We cannot alter the size of the
array one array is declared.
• Inserting and deleting the records from the array would be costly since we add/delete the
elements from the array.
Q: Write a C program to multiply two matrix.
Ans:
#include<stdio.h>
#include<conio.h>
int main()
{
int mat1[3][3], mat2[3][3], mat3[3][3], sum=0, i, j, k;
printf("Enter first 3*3 matrix element: ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d", &mat1[i][j]);
}
printf("Enter second 3*3 matrix element: ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d", &mat2[i][j]);
}
printf("\nMultiplying two matrices...");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
sum=0;
for(k=0; k<3; k++)
sum = sum + mat1[i][k] * mat2[k][j];
mat3[i][j] = sum;
}
}
printf("\nMultiplication result of the two given Matrix is: \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
printf("%d\t", mat3[i][j]);
printf("\n");
}
getch();
return 0;
}
Board Question 2020
Q: Write down important features of C language.
Ans: Features of C language:
• It is a robust language with rich set of built-in functions and operators that can be used to
write any complex program.
• The C compiler combines the compabilities of an assembly language with feature of a high
level language.
• Programs written in C are efficient and fast.
• It is more times faster than BASIC.
• C is highly portable means that program once written can be run on either machines with
little or no modification.
Q: Describe the structure of a C program.
Ans: A C program may contain one or more section for it’s structure, here are the basic structure of
a C program:
• Documentation Section: The documentation section consists of a set of comment lines
giving the name of the program, the author and other details.
• Link Section: The link section provides instruction to the compiler to link functions from
the system library.
• Definition section: The definition section defines all symbolic constants.
• Global declaration section: There are some variables that are used in more than one function.
Such variables are called global variables and are declared in the global declaration section
that is outside of all the functions.
• main() function section: Every C program must have one main function section. This
section contains two part, declaration part and executable part.
Q: Draw the flowchart to find the largest value among three numbers.
Ans:
Q: Distinguish between getchar() and scanf() functions.
Ans:
getchar() scanf()
C function to read a character only from the C functions to read from the standard input until
standard input stream(stdin) which is the encountering a white space or newline.
keyboard.
getchar function doeas not take any parameter. scanf function takes the format string and
variables with their address parameter.
Reads a single character from keyboard. Reads data according to the format specifier.
It used to read line of text. It is not used to read line of text.
Example: char ch[10]; Example: char ch[10];
gets(ch); scanf(“%s”,ch);;
Q: Write down the algorithm to find the area and perimeter of a rectangle.
Ans: Algorithm:
START
in function int area(int a, int b)
Step 1: Declare an integer ‘area’ and store a*b in it
Step 2: Return area
in function int perimeter(int a, int b)
Step 1: Declare an integer ‘perimeter’ and store 2*(a+b) in it
Step 2: return perimeter
in int main()
Step 1: Declare two integers ‘length’,’breadth’
Step 2: Print area(length, breadth)
Step 3: Print perimeter(length, breadth)
Q: Describe infinite loop and time delay loop with example.
Ans: Infinite loop: An infinite loop is a piece of coding that lacks a functional exit so that it repeats
indefinitely.
Example: unsigned I;
for(i=1;i>0;i++){
loop code
}
Time delay loop: Time delay loops are often used in program, these are loops that have no other
functions than to kill time.
Example: for(x=0;x<10000;x++)
Q: Write a C program to calculate the sum of following series :-
1+2+3+4+……...+50
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
int num=50, i, sum = 0; // declare local variables
for (i = 0; i <= num; i++)
{
sum = sum + i;
}
printf("\n Sum of the first %d number is: %d", num, sum);
getch();}