BSc CS FY_2_C Programming Part-I
BSc CS FY_2_C Programming Part-I
a) Explain Keywords.
b) Explain Algorithms.
c) What is if statements.
d) What is array.
g) Explain history of C.
b) Explain Operators
a) Variables
b) Data types:
a) Explain Keywords.
Answer:
1. Definition: “Keywords are the words whose meaning has already been explained to the C
compiler and their meanings cannot be changed”. Hence keywords are also called ‘Reserved
words’.
2. Keywords can be used only for their intended purpose.
3. Keywords serve as basic building blocks for program statements.
4. Keywords can’t be used as programmer defined identifier.
5. The keywords can’t be used as names for variables.
6. All keywords must be written in lowercase.
7. 32 keywords available in C.
8. auto, break, case, char, const, continue, default, int, float, const, double, char etc.
9. For example:
10. double and float: Both keywords double, as well as float, are needed for declaration of
floating type variables.
b) Explain Algorithms.
Answer:
Answer:
d) What is array.
Answer:
1. Definition: An array is a collection of data items, all of the same type, accessed using a common
name.
2. Explanation: All elements of array are stored in the contiguous memory locations.
3. The size of array must be a constant integral value.
4. Individual elements in an array can be accessed by the name of the array and an integer
enclosed in square bracket called subscript/index variable like employee Salary [5].
5. Array is a random access data structure. you can access any element of array in just one
statement.
6. The first element in an array is at index 0, whereas the last element is at index
(size_of_array - 1).
7. Declaration of Array:
a) Array Declaration by Specifying the Size
// declare an array by specifying size in [].
int my_array1[20];
b) array Declaration by Initializing Elements:
// initialize an array at the time of declaration.
int my_array[] = {100, 200, 300, 400, 500}
Answer:
1. Machine language is a low-level language made up of binary numbers or bits that a computer
can understand.
2. The only language that the computer understands is machine language
3. When a specific task, even the smallest process executes, machine language is transported to
the system processor. Computers are only able to understand binary data as they are digital
devices.
4. In the computer, all data like videos, programs, pictures are represented in binary.
5. Machine language is a low-level language that machines understand but that humans can
decipher using an assembler.
6. A compiler plays an important role between humans and computers as it converts machine
language into other code or language that is understandable by humans.
7. Advantages:
8. Disadvantages:
a) Machine dependent
b) Programming is very difficult
c) Difficult to understand
d) Difficult to write bug free programs
e) Difficult to isolate an error
Answer:
1. Definition: The break statement terminates the execution of the nearest enclosing do, for, switch, or
while statement in which it appears. Control passes to the statement that follows the terminated
statement.
2. The break is a keyword in C which is used to bring the program control out of the loop.
3. The break statement is used inside loops or switch statement.
4. The break statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner
loop first and then proceeds to outer loops
5.
g) Explain history of C.
Answer:
1. C is a general-purpose language which has been closely associated with the UNIX
operating system for which it was developed - since the system and most of the programs
that run it are written in C.
2. Many of the important ideas of C stem from the language BCPL, developed by Martin
Richards.
3. The influence of BCPL on C proceeded indirectly through the language B, which was
written by Ken Thompson in 1970 at Bell Labs, for the first UNIX system on a DEC
PDP7.
4. BCPL and B are "type less" languages whereas C provides a variety of data types.
5. In 1972 Dennis Ritchie at Bell Labs writes C and in 1978 the publication of The C
Programming Language by Kernighan & Ritchie caused a revolution in the computing
world. In 1983, the American National Standards Institute (ANSI) established a
committee to provide a modern, comprehensive definition of C.
6. The resulting definition, the ANSI standard, or "ANSI C", was completed late 1988.
7.
Q. 2 Attempt any three of the following (5 Marks each) 15
Answer:
#include<stdio.h>
#include<conio.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;
}
Output:
Enter a number: 5
b) Explain Operators
Answer:
1. Operators: A sign or Symbol used to perform arithmetic or logical operation is called as operators.
2. There are 8 types of operators:
a. Arithmetic operators
b) Assignment operators
c) Relational operators
d) Logical operators
e) Bit wise operators
f) Conditional operators (ternary operators)
g) Increment/decrement operators
h) Special operators
a) ARITHMETIC OPERATORS IN C
1 + Addition A+B
2 – Subtraction A-B
3 * multiplication A*B
4 / Division A/B
b) ASSIGNMENT OPERATORS IN C
In C programs, values for the variables are assigned using assignment operators.
For example, if the value ―10‖ is to be assigned for the variable ―sum‖, it can be assigned as
―sum = 10;‖
sum += 10
c) RELATIONAL OPERATORS IN C
Relational operators are used to find the relation between two variables. i.e. to compare the
In this program, relational operator (==) is used to compare 2 values whether they are equal
are not.
If both values are equal, output is displayed as ‖ values are equal‖. Else, output is displayed
Note: double equal sign (==) should be used to compare 2 values. We should not single
d) Logical Operators
-These operators are used to perform logical operations on the given expressions.
-There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||)
and logical NOT (!).
2 || logical OR (x>=10)||(y>=10) It returns true when at-least one of the condition is true.
These operators are used to perform bit operations. Decimal values are converted into binary
values which are the sequence of bits and bit wise operators work on these bits.
Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise OR), ^ (XOR),
Conditional operators return one value if condition is true and returns another value is condition
is false.
g) Increment/decrement Operators
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 programs.
Syntax:
Example:
Increment operator : ++ i ; i ++ ;
Decrement operator : – – i ; i – – ;
1. Unformatted console input/output functions are used to read a single input from the user at console and it
also allows us to display the value in the output to the user at the console.
2 While calling any of the unformatted console input/output functions,” we do not have to use any format
specifiers in them, to read or display a value”. Hence, these functions are named unformatted console
I/O functions.
3. Some of the most important formatted console input/output functions are –
a) getchar() : - It reads a character from the keyboard.
The syntax of getchar() function is as follows
Variablename=getchar();
For example,
Char a;
a = getchar();
b) gets() : It reads a string from the keyboard.
For example,
Putchar(‘a’);
For example,
puts("tutorial");
e) getche() : Reads a single character from the user at the console, and echoing it.
f) getch() : Reads a single character from the user at the console, without echoing it.
g) putch() : Displays a single character value at the console.
Answer:
#include <stdio.h>
int main()
scanf("%d", &rowCount);
scanf("%d", &columnCount);
scanf("%d", &secondMatrix[i][j]);}
printf("%d\t",resultMatrix[i][j]);
printf("\n");
return 0;
Sample Output:
1
1
6 6
6 6
Answer:
1. While loop is also known as a pre-tested loop. In general, a while loop allows a part of the
code to be executed multiple times depending upon a given condition.
2. It can be viewed as a repeating if statement. The while loop is mostly used in the case
where the number of iterations is not known in advance.
3. While-loop is entry control loop.
4. Syntax of while loop in C language
while(condition){
//code to be executed
Let's see the simple program of while loop that prints table of 1.
#include<stdio.h>
int main(){
int i=1;
while(i<=10){
printf("%d \n",i);
i++;
return 0;
Output
10
Q. 3 Attempt any three of the following (5 Marks each) 15
Answer:
6. In programming, if-else-if statement is also known as if-else-if ladder. It is used when there are
more than two possible action based on different conditions.
7. Syntax
if (Condition1)
Statement1;
else if(Condition2)
Statement2;
else if(ConditionN)
StatementN;
else
Default_Statement;
3. Working
In the above syntax of if-else-if, if the Condition1 is TRUE then the Statement1 will be executed and
control goes to next statement in the program following if-else-if ladder.
4. If Condition1 is FALSE then Condition2 will be checked, if Condition2 is TRUE then Statement2 will be
executed and control goes to next statement in the program following if-else-if ladder.
5. Similarly, if Condition2 is FALSE then next condition will be checked and the process continues.
6. If all the conditions in the if-else-if ladder are evaluated to FALSE, then Default_Statement will be
executed.
7. The working of if-else-if ladder can be illustrated by following flowchart:
8. Flowchart
9. Programming Examples
Example 1: C program to find largest from three numbers given by user to explain working of if-else-if
statement or ladder
#include<stdio.h>
int main()
{ int a,b,c;
else
return(0);
Output
33
-17
Largest = 33
Answer:
#include <stdio.h>
int main() {
int num;
scanf("%d", &num);
if(num % 2 == 0)
else
return 0;
Output
Enter an integer: 9
9 is odd.
Answer:
1. Passing arrays to functions in C programming: Like other values of variables, arrays can be passed to a
function.. Both one-dimensional arrays and multidimensional arrays can be passed as function arguments.
2. Passing one-dimensional array to the function : While passing one-dimensional array to the function
name of the array is passed as actual arguments and array variable with subscript is passed as formal
arguments.
3. Ways of passing arrays as formal arguments.
{ .......
{ .......
4. Example: C program to pass an array to the function that contains marks obtained by the student. Then
display the total marks obtained by the student
#include <stdio.h>
int main()
return 0;
int sum=0,i;
for (i=0;i<5;i++)
sum=sum+a[i];
return sum;
Output
5. Explanation:
Here, the total_marks( ) function is used to calculate total marks. The for loop is used to access the array
elements.
6. Passing Multidimensional array to the function
Like simple arrays, multidimensional arrays can be passed to the function. Two-dimensional array is
indicated by two sets of brackets with array variable. The size of second dimension must be specified.
#include <stdio.h>
int i,j;
printf("Matrix is :\n");
for ( i=0;i<3;i++ )
for ( j=0;j<3;j++)
printf("%d",m[i][j]);
printf("\n");
int main()
matrix(m);
return 0;
Output
Matrix is :
123
456
789
d) Explain structure of c program:
Answer:
1. 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.
2. Link section: The link section provides instructions to the compiler to link functions
from the system library such as using the #include directive.
3. Definition section: The definition section defines all symbolic constants such using
the #define directive.
4. 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. This section also declares all the user-defined functions.
5. main () function section: Every C program must have one main function section. This section
contains two parts; declaration part and executable part
1. Declaration part: The declaration part declares all the variables used in the executable part.
2. Executable part: There is at least one statement in the executable part. These two parts must appear
between the opening and closing braces. The program execution begins at the opening brace and ends
at the closing brace. The closing brace of the main function is the logical end of the program. All
statements in the declaration and executable part end with a semicolon.
6. 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.
7.
e) What is difference between Compilers and Interpreters:
Answer:
Interpreter
Sr.no Compiler
As it scans the code in one go, the errors Considering it scans code one line at a time,
2. (if any) are shown at the end together. errors are shown line by line.
The main advantage of compilers is its Due to interpreters being slow in executing the
3. execution time. object code, it is preferred less.
It converts the source code into object It does not convert source code into object code
4. code. instead it scans it line by line
Answer:
1. Switch statement in C tests the value of a variable and compares it with multiple cases.
2. Once the case match is found, a block of statements associated with that particular case is executed.
3. Each case in a block of a switch has a different name/number which is referred to as an identifier.
4. The value provided by the user is compared with all the cases inside the switch block until the match is
found.
5. If a case match is NOT found, then the default statement is executed, and the control goes out of the
switch block.
6. Syntax:
Switch Case Syntax
A general syntax of how switch-case is implemented in a ‘C’ program is as follows:
switch( expression )
{
case value-1:
Block-1;
Break;
case value-2:
Block-2;
Break;
case value-n:
Block-n;
Break;
default:
Block-1;
Break;
}
Statement-x;
7. Flow chart:
8. Program:
#include <stdio.h>
int main() {
int num = 8;
switch (num) {
case 7:
printf("Value is 7");
break;
case 8:
printf("Value is 8");
break;
case 9:
printf("Value is 9");
break;
default:
printf("Out of range");
break;
return 0;
Output:
Value is 8
9.Explaination of program: In the given program we have explain initialized a variable num with value
A switch construct is used to compare the value stored in variable num and execute the block of
statements associated with the matched case. In this program, since the value stored in variable num is
eight, a switch will execute the case whose case-label is 8. After executing the case, the control will
fall out of the switch and program will be terminated with the successful result by printing the value
on the output screen.
b) Write a program using array to find Larges Element in an array.
Answer:
include <stdio.h>
int main()
int n;
double arr[100];
scanf("%d", &n);
scanf("%lf", &arr[i]);
arr[0] = arr[i];
return 0;
Output
Enter number1: 34
Enter number2: 2
Enter number4: 38
Enter number5: 24
Largest element = 38
c) Explain formatted I/O Statements.
Answer:
1. Definition: It is called as formatted I/O statements because it uses the format specifiers in these
functions.
2. We use the formatted input and output functions in the C language for taking single or multiple
inputs from the programmer/user at the console.
3. These functions also allow a programmer to display single or multiple values, in the form of
output, to the users present in the console.
4. Functions Used and their Description of the Function
5. printf() We use this function for displaying a single or multiple values in the form of output
for the user end at the console.
6. scanf() We use this function for reading a single or multiple values in the form of input from
the user end at the console.
7. sprintf() We use this function for reading the values that are stored in various variables, and for
storing these values into the array of characters.
Syntax: sprint(array name,”format specifier”, variable name)
8. sscanf() We use this function for reading the characters available in the string, and then storing
them into the available variables.
9. Syntax: sprint(array name,”format specifier”, &variable name)
10. scanf()
We use the scanf() function for getting the formatted inputs or standard inputs so that the
printf() function can provide the program with numerous options of conversion.
scanf (format_specifier, &data_a, &data_b,……); // Here, & refers to the address operator
11. The purpose of the scanf() function is to read the characters that we get from the standard input,
convert them according to the string of format specification, and then store the available inputs in
the memory slots that the other arguments represent.
12. Example of scanf()
When we consider the string data names in a program, the ‘&’ character does not prefix the data
name.
13. printf() : We use the printf() function for generating the formatted outputs or standard outputs
in accordance with a format specification. The output data and the format specification string act as
the parameters of the function printf().
Example of printf()
Answer:
1. The do while loop is a post tested loop. Using the do-while loop, we can repeat the execution of
several parts of the statements.
2. The do-while loop is mainly used in the case where we need to execute the loop at least once.
3. The do-while loop is mostly used in menu-driven programs where the termination condition
depends upon the end user.
4. do while loop syntax
do{
//code to be executed
}while(condition);
5. Flow chart
6. #include<stdio.h>
#include<conio.h>
int main()
{
int num=1; //initializing the variable
do //do-while loop
{
printf("%d\n",2*num);
num++; //incrementing operation
}while(num<=10);
return 0;
}
Output:
2
4
6
8
10
12
14
16
18
20
e) Write a sum of digits’ program in C.
Answer:
#include<stdio.h>
int main()
int n,sum=0,m;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
m=n%10;
sum=sum+m;
n=n/10;
printf("Sum is=%d",sum);
return 0;
Output:
Enter a number:654
Sum is=15
Q .5 Short notes on any three of the following (5 Marks each) 15
a) Variables
Answer:
Variables in C:
A variable is a name of the memory location. It is used to store data. Its value can be changed, and it
can be reused many times.
It is a way to represent memory location through symbol so that it can be easily identified.
Syntax:
type variable_list;
3. We can also provide values while declaring the variables as given below:
Answer:
3. Syntax:
d) go to statements.
Answer:
1. The goto statement is a jump statement which is sometimes also referred to as unconditional
jump statement.
2. The goto statement can be used to jump from anywhere to anywhere within a function.
3. Syntax:
Syntax1 | Syntax2
----------------------------
goto label; | label:
. | .
. | .
. | .
label: | goto label;
4. In the above syntax, the first line tells the compiler to go to or jump to the statement
marked as a label.
5. Here label is a user-defined identifier which indicates the target statement.
6. The statement immediately followed after ‘label:’ is the destination statement.
7. The ‘label:’ can also appear before the ‘goto label;’ statement in the above syntax.
8. CODE:
// even or not using goto statement
#include <stdio.h>
// function to check even or not
void checkEvenOrNot(int num)
{
if (num % 2 == 0)
// jump to even
goto even;
else
// jump to odd
goto odd;
even:
printf("%d is even", num);
// return if even
return;
odd:
printf("%d is odd", num);
}
int main() {
int num = 26;
checkEvenOrNot(num);
return 0;
}
OUTPUT:
26 is even
e) High level languages.
Answer:
High-Level Languages:
1. The symbolic languages greatly improved programming efficiency they still required
programmers to concentrate on the hardware that they were using working with symbolic
languages was also very tedious because each machine instruction had to be individually coded.
2. The desire to improve programmer efficiency and to change the focus from the computer to the
problems being solved led to the development of high-level languages.
3. High-level languages are portable to many different computer allowing the programmer to
concentrate on the application problem at hand rather than the intricacies of the computer.
4. Advantages:
a) Easy to write and understand
b) Easy to isolate an error
c) Machine independent language
d) Easy to maintain
e) Better readability
f) Low Development cost
g) Easier to document
h) Portable
5. Disadvantages:
a) Needs translator
b) Requires high execution time
c) Poor control on hardware
d) Less efficient
6. Example: C language
#include<stdio.h>
void main()
{
int a,b,c;
scanf("%d%d%",&a,&b);
c=a+b;
printf("%d",c);
}