Q -1 write a program to print pattern
23
456
7 8 9 10
11 12 13 14 15
Ans :
#include<stdio.h> // Header file for i/o.
int main(){ // entry point of a program.
int r,c,cnt=1; // variable declaration + initialization
for (r=1;r<=5;r++){
for(c=4;c>=r;c--) // for loop to print space
printf(" ");
for(c=1;c<=r;c++) // for loop to print numbers
printf("%d ",cnt++);
printf("\n");
return 0;
}
Q-2 write a program to swap two number without using third
variable.
Ans :
#include<stdio.h> // Header file for i/o.
int main(){ // entry point of a program.
int a,b; // variable declartion
// input
printf("Enter value of a and b : ");
scanf("%d%d",&a,&b);
// calculation
a^=b;
b^=a;
a^=b;
// output
printf("value of a : %d\n value of b : %d\n",a,b);
return 0;
}
Q-3 Explain C data types in detail ?
Ans : Data Types
Data types in C determine the type and size of data associated with
variables.
Types of Data Types in C
1. Primary / Primitive / Base / Fundamental Data types :
I. int: For whole numbers (e.g., 0, -5, 10).
II. float : For real numbers (with decimal points).(e.g., 3.14)
III. double : For real number (with decimal point).
IV. char: For individual characters (e.g., ‘h’, ‘A’, ‘$’).
V. void: An incomplete type (used for functions that don’t return
anything).
Float Double
1. It has 7 decimal 1. It has 15 decimal
digits precision. digits precision.
2. We have to define a 2. All values with
float value by using f decimal points are
suffix. considered as double
in c by default.
3. For float data type, 3. For double data type,
the format specifier the format specifier
is %f. is %lf.
4. It uses 4 bytes 4. It uses 8 bytes of
memory. memory.
Modifiers
Modifiers in C are keywords that alter the behavior of variables,
functions, or data types.
1. signed Modifier
Signed variables can store positive, negative integers, and zero.
2. unsigned Modifier
Unsigned variables can store only non-negative integer values.
3. short Modifier
The short keyword modifies the minimum values that a data type can
hold. It is used for small integers that lie in the range of −32,767 to
+32,767.
4. long Modifier
The long keyword modifies the maximum values that a data type can
hold.
2. Secondary / Derived data type
Array - An array is a linear data structure that holds a group of
elements having the same data types.
or
An array in C is a fixed-size collection of similar data items stored
in contiguous memory locations. It can be used to store the
collection of primitive data types such as int, char, float, etc., and
also derived and user-defined data types such as pointers,
structures, etc
Strings - It's a group of zero or more characters must be
represented within double quotation mark.
In c programming it is represented by character array.
Character array is nothing but a null terminating array where string
end identified by null.
Null -> '\0'
Declaration of character array
char nameOfString [ Size + 1 ]
3. User Defined Data Types
Enum , Struct , Union
Q-4 Explain nested if with practical example.
Ans : Nested If
Nested if statements refer to the placement of one or more if
statements within another if statement. This allows for more
complex decision-making processes within your C programs.
Example : WAP to find the biggest number among 3 numbers.
#include<stdio.h> // Header file for i/o.
int main(){ // entry point of a program.
int n1,n2,n3; // variable declartion
// input
printf("Enter three numbers : ");
scanf("%d%d%d",&n1,&n2,&n3);
// comparison and output
if(n1>n2){
if(n1>n3)
printf("%d is the largest number.",n1);
else
printf("%d is the largest number.",n3);
else if(n2>n3){
if(n2>n1)
printf("%d is the largest number",n2);
else
printf("%d is the largest number.",n3);
else
printf("%d is the largest number.",n3);
return 0;
Q-5 Explain operator and its precedence in detail.
Operators
Operators in C are symbols that perform mathematical, relational,
bitwise, conditional, or logical operations on operands in a program
Arithmetic operators
Used to perform mathematical operations on operands, such as
addition, subtraction, multiplication, division, and modulus.
Relational operators
Also known as conditional operators, these operators are used to
compare values and return a true or false value based on the
comparison.
Logical operators
Boolean operators that combine two expressions to produce a true
or false output.
Bitwise operators
The bitwise operators are the operators used to perform the
operations on the data at the bit-level.
Increment / Decrement Operator
The increment operator ( ++ ) is used to increment the value of a
variable in an expression by 1. It can be used on variables of the
numeric type such as integer, float, character, pointers, etc.
The decrement operator ( -- ) is used to decrement the value of the
variable in an expression by 1.
Associativity and Precedence
Associativity and Precedence of Operators are fundamental
concepts in programming that determine how expressions with
multiple operators are evaluated.
1. Operator Precedence:
• Defines the order in which operators are evaluated in an
expression.
• Operators with higher precedence are evaluated before
operators with lower precedence.
o Example: In 3 + 4 * 2, the multiplication (*) has higher
precedence than addition (+), so the result is 3 + (4 * 2) =
11.
2. Operator Associativity:
• Determines the direction in which operators of the same
precedence are evaluated.
• Types of Associativity:
o Left-to-Right (Left Associative): Operators are
evaluated from left to right.
• Example: In 5 - 3 - 2, subtraction is left associative,
so it's ((5 - 3) - 2) = 0.
o Right-to-Left (Right Associative): Operators are
evaluated from right to left.
• Example: In 2 ** 3 ** 2 (exponentiation in Python),
it's 2 ** (3 ** 2) = 2 ** 9 = 512.
Key Points to Remember
• Operators with higher precedence are evaluated first.
• When operators have the same precedence, associativity
determines the evaluation order.
• Parentheses can override precedence and associativity to
explicitly specify the desired evaluation order.
Q-6 Explain functions and its types ? Write a program
demonstrating user defined function in detail.
A function is a set of statements that when called perform some
specific tasks. It is the basic building block of a program that
provides organization and code reusability. The programming
statements of a function are enclosed within { } braces, having
certain meanings and performing certain operations.
Types of Functions
There are two types of functions in C:
1. Library Functions
2. User Defined Functions
Library Functions
A library function is also referred to as a “built-in function”. A
compiler package already exists that contains these functions, each
of which has a specific meaning and is included in the package. Built-
in functions have the advantage of being directly usable without
being defined.
Examples :
pow() -> math.h
strlen() -> string.h
printf() , scanf() -> stdio.h
User Defined functions
Functions that the programmer creates are known as User-Defined
functions or “tailor-made functions”. User-defined functions can be
improved and modified according to the need of the programmer.
Whenever we write a function that is case-specific and is not
defined in any header file, we need to declare and define our own
functions according to the syntax.
Syntax
return_type name_of_the_function (parameter_1, parameter_2,….);
example :
Conditions of Return Types and Arguments
In C programming language, functions can be called either with or
without arguments and might return values. They may or might not
return values to the calling functions.
1. Function with no arguments and no return value
void greet()
2. Function with no arguments and with return value
int get_random_number()
3. Function with argument and with no return value
void printBigNum(int a, int b)
4. Function with arguments and with return value
int add(int a, int b)
// write a program to demonstrate user defined function
#include <stdio.h> // header file for i/o
// user defined function to add two numbers.
int add(int a, int b) {
return a + b;
int main() { // entry point of program
int sum = add(5, 3); // call add function and store result in
// sum
printf("Sum: %d\n", sum); // output
return 0;
Q-7 Write a program to check whether a number is a prime or not
using a function ?
Ans :
#include <stdio.h> // header file for i/o
#include <math.h> // header file for math functions
// function to check whether a number is prime or not
void checkPrime(int n)
if (n <= 1) // checking for negetive numbers
printf("number is not prime.");
return;
int i, flag = 0; // variable declaration + initialization
// working
for (i = 2; i <= sqrt(n); i++)
if (n % i == 0)
flag = 1;
break;
//output
if (flag == 0)
printf("number is prime.");
else
printf("number is not prime.");
int main()
int n = 1;
checkPrime(n);
return 0;
Q-8 what is recursion ? explain stack overflow ?
Ans :
Recursion :
A function is said to be recursively defined if the function definition
refers to itself.
Or
Recursion is the process by which a function calls itself.
Recursion has some important properties. Some of which are
mentioned below:
• The primary property of recursion is the ability to solve a
problem by breaking it down into smaller sub-problems, each of
which can be solved in the same way.
• A recursive function must have a base case or stopping criteria
to avoid infinite recursion.
• Recursion involves calling the same function within itself, which
leads to a call stack.
• Recursive functions may be less efficient than iterative
solutions in terms of memory and performance.
#include <stdio.h> // header file for i/o
// function to find factorial of a number using function
long long int fact(int n){
if(n<=1) // base case
return 1;
else
return fact(n-1)*n; // recursive call
int main() // entry point of program
int n = 7; // variable declartion + initialization
long long int res = fact(n); // call fact function
printf("%ld",res); // output
return 0;
Stack Overflow:
Stack is a special region of our process’s memory which is used to
store local variables used inside the function, parameters passed
through a function and their return addresses. Whenever a new local
variable is declared it is pushed onto the stack. All the variables
associated with a function are deleted and memory they use is freed
up, after the function finishes running. The user does not have any
need to free up stack space manually. Stack is Last-In-First-Out
data structure. In our computer’s memory, stack size is limited. If a
program uses more memory space than the stack size then stack
overflow will occur and can result in a program crash. There are two
cases in which stack overflow can occur:
1. If we declare large number of local variables or declare an
array or matrix or any higher dimensional array of large size
can result in overflow of stack.
2. If function recursively call itself infinite times then the stack
is unable to store large number of local variables used by every
function call and will result in overflow of stack.
Q-9 Write a program to print values of array in ascending and
descending order.
// program to print array in ascending order.
#include<stdio.h> // Header file for i/o.
int main(){ // entry point of a program.
int a[8] = {34,22,56,42,67,88,43,11},i,j;
// sorting array in ascedning order
for(i=0;i<8;i++){
for(j=i+1;j<8;j++){
if(a[i]>a[j]){
int t = a[i];
a[i] = a[j];
a[j] = t;
// output
for(i=0;i<8;i++)
printf("%d ",a[i]);
return 0;
// program to print array in descending order
#include<stdio.h> // Header file for i/o.
int main(){ // entry point of a program.
int a[8] = {34,22,56,42,67,88,43,11},i,j;
// Descending Order
for(i=0;i<8;i++){
for(j=i+1;j<8;j++){
if(a[i]<a[j]){
int t = a[i];
a[i] = a[j];
a[j] = t;
}
// output
for(i=0;i<8;i++)
printf("%d ",a[i]);
return 0;
Q-10 what is the difference between call by value and call by
address with suitable example?
Ans :
Call By Value in C
In call by value method of parameter passing, the values of actual
parameters are copied to the function’s formal parameters.
• There are two copies of parameters stored in different
memory locations.
• One is the original copy and the other is the function copy.
• Any changes made inside functions are not reflected in the
actual parameters of the caller.
Call by Reference in C
In call by reference method of parameter passing, the address of
the actual parameters is passed to the function as the formal
parameters. In C, we use pointers to achieve call-by-reference.
• Both the actual and formal parameters refer to the same
locations.
• Any changes made inside the function are actually reflected in
the actual parameters of the caller.
Difference between call by value and call by address
Call By Value Call By Reference
While calling a function, instead
While calling a function, we of passing the values of variables,
pass the values of variables we pass the address of
to it. Such functions are variables(location of variables) to
known as “Call By Values”. the function known as “Call By
References.
In this method, the value of
each variable in the calling In this method, the address of
function is copied into actual variables in the calling
corresponding dummy function is copied into the dummy
variables of the called variables of the called function.
function.
With this method, using
With this method, the addresses we would have access
changes made to the dummy to the actual variables and hence
variables in the called we would be able to manipulate
function have no effect on them.
Call By Value Call By Reference
the values of actual variables
in the calling function.
In call-by-values, we cannot
In call by reference, we can alter
alter the values of actual
the values of variables through
variables through function
function calls.
calls.
Values of variables are Pointer variables are necessary to
passed by the Simple define to store the address values
technique. of variables.
This method is preferred
This method is preferred when we
when we have to pass some
have to pass a large amount of
small values that should not
data to the function.
change.
Call by value is considered Call by reference is risky as it
safer as original data is allows direct modification in
preserved original data
Example:
#include <stdio.h>
// Function prototypes
void callByValue(int a);
void callByAddress(int *b);
int main() {
int x = 10, y = 20;
// Call by Value
printf("Before callByValue: x = %d\n", x);
callByValue(x);
printf("After callByValue: x = %d\n", x);
// Call by Address
printf("Before callByAddress: y = %d\n", y);
callByAddress(&y);
printf("After callByAddress: y = %d\n", y);
return 0;
// Function to demonstrate Call by Value
void callByValue(int a) {
a = a + 10; // Changes local to the function
printf("Inside callByValue: a = %d\n", a);
}
// Function to demonstrate Call by Address
void callByAddress(int *b) {
*b = *b + 10; // Changes reflected outside the function
printf("Inside callByAddress: *b = %d\n", *b);