Model PCI-1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Sharad Institute of Technology Polytechnic, Yadrav.

IT Department
Model Answer Sheet of Unit Test- I
(AY-2023-2024)Summer 2024
Course & Year code:IH2K Sem:II
Class: FYCSIT Div: -H
Subject Name: PIC Sub. Code:312303
Faculty Name:Ms.S.P.Phadtare Marks:30

Que Sub
No. Que. Answer Marks

Q.1.AAttempt any one of the following


a.List any four Keywords used in C.
Keywords are predefined, reserved words used in programming that have special
meanings to the compiler.
List of Keywords:
Auto
Double
Switch
Case
int

b.Define Operator and list different types of Operator.


Operator:Operator is used to perform an operation on operand.
Types of operator:
1.Unary: It includes increment /decrement,sizeof operator
2.Binary: It includes Arithmetic ,Logical,Relational,Bitwise
3.Ternary/Conditional Operator

Q.1.B.Attempt any three of the following


a.List different relational operators in c.
Relational operators in C,
1.less than(<)
2.greater than(>)
3.less than or equal to(<=)
4.greater than or equal to(>=)
5.equal to (==)
6.not equal to(!=)

b.Differentiate between while and do….while loop.

Sr.No. While Do…While

1 Statement(s) is executed atleast


Condition is checked first
once, thereafter condition is
then statement(s) is executed.
checked.
2 It might occur statement(s) is
At least once the statement(s) is
executed zero times, If
executed.
condition is false.
3 Semicolon at the end of while.
No semicolon at the end of
while(condition);
while. while(condition)
4 If there is a single statement, Brackets are always required.
brackets are not required.
5 Variable in condition is
variable may be initialized before
initialized before the
or within the loop.
execution of loop.
6 while loop is entry controlled do-while loop is exit controlled
loop. loop.
7 while(condition) do {
{ statement(s); statement(s);
} } while(condition);

c.Explain the use of break and continue statement.

1.Break:The Break statement is used to exit from the loop constructs. The break
statement is usually used with the switch statement, and it can also use it within
the while loop, do-while loop, or the for-loop. When a break statement is
encountered then the control is exited from the loop construct immediately.
Syntax:
break;

2.Continue: The continue statement is not used to exit from the loop constructs.
The continue statement is not used with the switch statement, but it can be used
within the while loop, do-while loop, or for-loop. When the continue statement is
encountered then the control automatically passed from the beginning of the loop
statement.
Syntax:
continue;

d.Explain for loop and write c program to print 1 to 100 numbers using for
loop
for Loop in C
The for loop in C Language provides a functionality/feature to repeat a set of
statements a defined number of times. The for loop is in itself a form of an entry-
controlled loop.
Syntax of for Loop
for(initialization; check/test expression;increment/decrement)
{
// body consisting of multiple statements
}

#include<stdio.h>

int main()
{
int i;
for(i=1;i<=100;i++)
{
printf("%d\n",i);
}
}
Q.1.C Attempt the following
a.Define Array. List it’s types.
Array:An array is defined as the collection of similar type of data items stored at
contiguous memory locations. Arrays are the derived data type in C programming
language which can store the primitive type of data such as int, char, double, float,
etc.
Types of Array:
1.Single dimensional array
2.Two dimensional Array
3.Multi-dimensional array

Q.2.A.Attempt any two of the following

a.Explain formatted input output function with example.


Following are the formatted I/O functions -
1. printf()
2. scanf()

printf():
printf() function is used in a C program to display any value like float, integer,
character, string, etc on the console screen. It is a pre-defined function that is
already declared in the stdio.h(header file).
Syntax :
printf(“Format Specifier”, var1, var2, …., varn);

//Example of printf() function


#include <stdio.h>

int main()
{

int a;
a = 20;
// Printing the value of a variable
printf("%d", a);
return 0;
}

2.scanf():
scanf() function is used in the C program for reading or taking any value from the
keyboard by the user, these values can be of any data type like integer, float,
character, string, and many more. This function is declared in stdio.h(header file),
that’s why it is also a pre-defined function. In scanf() function we use &(address-
of operator) which is used to store the variable value on the memory location of
that variable.
Syntax:
scanf(“Format Specifier”, &var1, &var2, …., &varn);
// C program to implement scanf() function
#include <stdio.h>
int main()
{
int num1;
printf("Enter a integer number: ");
scanf("%d", &num1);
printf("You have entered %d", num1);
return 0;
}

b.Develop algorithm and flowchart to determine whether the given number is


even or odd.

Algorithm: whether the given number is even or odd.


Step 1 : Start
2Step 2 : Read number "n"
3Step 3 : If n % 2==0
4Step 4 : print Even
5Step 5 : Else print Odd.
6Step 6 : End

c.Describe general structure of C.


Documentation section: The documentation section consists of a set of comment
lines giving the name of the program, the author and other details, which the
programmer would like to use later.
Link section: The link section provides instructions to the compiler to link
functions from the system library such as using the #include directive.
Definition section: The definition section defines all symbolic constants such
using the #define directive.
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.
Declaration part: The declaration part declares all the variables used in the
executable part.
Subprogram section: If the program is a multi-function program then the
subprogram section contains all the user-defined functions that are called in the
main () function. User-defined functions are generally placed immediately after the
main () function, although they may appear in any order.
Header files A header file is a file with extension .h which contains C function
declarations and macro definitions to be shared between several source files.
Include Syntax Both the user and the system header files are included using the
preprocessing directive #include. main() function is the entry point of any C
program. It is the point at which execution of program is started. Every C program
have a main() function.

Q. 02.B.Attempt any two of the following

a.Explain Switch statement with example.


Switch case statement evaluates a given expression and based on the evaluated
value(matching a certain condition), it executes the statements associated with it.
Basically, it is used to perform different actions based on different
conditions(cases).

State the syntax of Switch statement.


switch(expression) {

case constant-expression :
statement(s);
break; /* optional */

case constant-expression :
statement(s);
break; /* optional */

/* you can have any number of case statements */


default :
statement(s);
}

Rules of the switch case statement


Following are some of the rules that we need to follow while using the switch
statement:
1. In a switch statement, the “case value” must be of “char” and “int” type.
2. There can be one or N number of cases.
3. The values in the case must be unique.
4. Each statement of the case can have a break statement. It is optional.
5. The default Statement is also optional.

// C program to Demonstrate returning of day based numeric value


#include <stdio.h>
int main()
{
// switch variable
int var = 1;
// switch statement
switch (var) {
case 1:
printf("Case 1 is Matched.");
break;
case 2:
printf("Case 2 is Matched.");
break;
case 3:
printf("Case 3 is Matched.");
break;
default:
printf("Default case is Matched.");
break;
}
return 0;
}
How switch Statement Work?
The working of the switch statement in C is as follows:
1. Step 1: The switch variable is evaluated.
2. Step 2: The evaluated value is matched against all the present cases.
3. Step 3A: If the matching case value is found, the associated code is
executed.
4. Step 3B: If the matching code is not found, then the default case is executed
if present.
5. Step 4A: If the break keyword is present in the case, then program control
breaks out of the switch statement.
6. Step 4B: If the break keyword is not present, then all the cases after the
matching case are executed.
7. Step 5: Statements after the switch statement are executed.
Flowchart of Switch Statement
b.Develop a c program to find largest of three numbers using nested…if else
statement.

// C program to find the largest number among three number


// using nested if-else
#include <stdio.h>
int main()
{
int A, B, C;

printf("Enter three numbers: ");


scanf("%d %d %d", &A, &B, &C);

if (A >= B) {
if (A >= C)
printf("%d is the largest number.", A);
else
printf("%d is the largest number.", C);
}
else {
if (B >= C)
printf("%d is the largest number.", B);
else
printf("%d is the largest number.", C);
}

return 0;
}

c.Develop a c program to print even numbers between 1 to 100 using while loop
and draw flowchart.

* C program to all even numbers between 1 to 100 using while loop */


#include <stdio.h>
int main()
{
int number = 1;
while( number <= 100 )
{
if( (number % 2) == 0 )
{
printf("%d\n", number);
}
number++;
}

return 0;
}
Algorithm
1. Start
2. Initialize the variable i to 1.
3. while i<=100
4. if i%2==0
5. print the number
6. increment value of i
7. stop

Flowchart

Q. 02.C Attempt the following

a.Explain how to initialize and declare of one dimensional and two dimensional
arrays.

One dimensional array in C


Arrays are a fundamental concept in programming, and they come in different
dimensions. One-dimensional arrays, also known as single arrays, are arrays with
only one dimension or a single row.
Syntax of One-Dimensional Array in C/Declare 1D array:
The syntax of a one-dimensional array in C programming language is as follows:
1. dataType arrayName[arraySize];
2.
Where
dataType specifies the data type of the array. It can be any valid data type in C
programming language, such as int, float, char, double, etc.
arrayName is the name of the array, which is used to refer to the array in the
program.
arraySize specifies the number of elements in the array. It must be a positive
integer value.
Example:
int a[5];
Initializing One Dimensional Array in C
In C programming language, we can initialize a one-dimensional array while
declaring it or later in the program. We can initialize a one-dimensional array while
declaring it by using the following syntax:
1. dataType arrayName[arraySize] = {element1, element2, ..., elementN};
2. Example:
3. int numbers[5] = {10, 20, 30, 40, 50};

Two dimensional array in C

An array consisting of two subscripts is known as two-dimensional array.


These are often known as array of the array.
In two dimensional arrays the array is divided into rows and columns.
These are well suited to handle a table of data.
In 2-D array we can declare an array as :
Declaration:- Syntax: data_type array_name[row_size][column_size];
Ex:- int arr[3][3];
where first index value shows the number of the rows and second index value shows
the number of the columns in the array.
Initializing two-dimensional arrays:
Like the one-dimensional arrays, two-dimensional arrays may be initialized by
following their declaration with a list of initial values enclosed in braces.
Ex: int a[2][3]={0,0,0,1,1,1};
initializes the elements of the first row to zero and the second row to one. The
initialization is done row by row.

You might also like