Pop Assign2
Pop Assign2
Pop Assign2
A function definition, on the other hand, contains the actual implementation or body of the
function.
// Function declaration
int main()
{
// Function call
return 0;
// Function definition
return a + b;
OUTPUT :
Sum: 8
2. Define an array. How do you declare and initialize
2D array. Explain with an example code.
In C,
an array is a collection of elements of the same data type stored in
contiguous memory locations.
A 2D array is essentially an array of arrays, forming a grid or matrix.
To declare and initialize a 2D array in C, you specify the number of
rows and columns.
#include <stdio.h>
int main()
{
// Declare and initialize a 2D array
int matrix[3][4];
// Access and print elements of the 2D array
printf("Matrix elements:\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
return 0;
}
3 a) Write a C program to swap two numbers using call by
reference method along with an output.
#include <stdio.h>
void swapNumbers(int a, intb);
int main()
{
int num1, num2;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Display original numbers
printf("Original numbers: num1 = %d, num2 = %d\n", num1, num2);
// Call the function to swap numbers
swapNumbers(&num1, &num2);
// Display swapped numbers
printf("Swapped numbers: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
// Function definition to swap numbers using call by reference
void swapNumbers(int a, int b)
{
int temp = a;
a =b;
b = temp;
}
3. b) Write a C program to search an element using binary search with appropriate
messages along with an output.
#include<stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &a[i]);
scanf("%d", &key);
low = 0;
high = n - 1;
if (key == a[mid])
found = 1;
break;
low = mid + 1;
else
high = mid - 1;
if (found == 1)
else
return 0;
}
4.b) Write a C program to sort the elements using bubble sort
by passing array as function argument along with an output.
#include<stdio.h>
int main()
{
int a[100],n,i,j,temp;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the %d elements of array\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("The Input array is\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
If(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nThe sorted array is\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
5. a) Write a C program to find nCr along with an output
#include <stdio.h>
int main()
{
int n, r;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Enter the value of r: ");
scanf("%d", &r);
if (n < 0 || r < 0)
printf("n and r must be non-negative integers.\n");
else if (r > n)
printf("r must be less than or equal to n.\n");
else
{
// Calculate nCr using a single function
int nCr = 1;
for (int i = 1; i <= r; i++) {
nCr *= (n - i + 1);
nCr /= i;
}
printf("C(%d, %d) = %d\n", n, r, nCr)
return 0;
}
b) Define a string. List the string manipulation functions. Explain any
two with examples.
#include <stdio.h>
#include <string.h>
int main()
{
char myString[] = "Hello, World!";
int length = strlen(myString);
printf("Length of the string: %zu\n", length);
return 0;
}
Pointer in C:
A pointer in C is a variable that stores the memory address of another variable. It "points to" the
memory location where the value of another variable is stored. Pointers are powerful and widely
used in C for tasks such as dynamic memory allocation, array manipulation, and accessing
functions through pointers.
To declare a pointer in C, you use the asterisk (*) symbol followed by the data type of the variable
it will point to. For example, int* declares a pointer to an integer. You can also initialize a pointer
with the memory address of a variable.
#include <stdio.h>
int main()
{
int num = 42; // Declare and initialize an integer variable
int *ptr = # // Declare and initialize a pointer to an integer with the address of 'num'
#include<stdio.h>
#include<math.h>
int main()
int n, i;
scanf("%d", &n);
scanf("%f", &a[i]);
ptr = a;
ptr++;
mean = sum / n;
ptr = a;
ptr++;
printf("Sum=%.3f\t", sum);
printf("Mean=%.3f\t", mean);
return 0;
}
8. a) What is a structure? Explain the declaration and initialization with an
example code.
Structure in C:
In C, a structure is a user-defined data type that allows you to group together variables of different data
types under a single name. It enables you to create a composite data type that can represent a collection of
related information.
Declaration:
The syntax for declaring a structure in C is as follows:
struct structure_name {
data_type1 member1;
data_type2 member2;
};
EXAMPLE
#include <stdio.h>
struct Student {
char name[50];
int age;
float grade;
};
int main() {
printf("Student Information:\n");
return 0;
}
b) Differentiate between structure and union.
9. Write a C program to read and display the details of employees (eNo, eName
and eSalary) using structure along with a neat output
#include <stdio.h>
struct Employee {
int eNo;
char eName[50];
float eSalary;
};
int main()
scanf("%d", &employees[i].eNo);
scanf("%s", employees[i].eName);
scanf("%f", &employees[i].eSalary);
printf("\nDetails of employees:\n");
return 0;
}
10. What is file? Explain the streams associated with a file with examples.
11 Illustrate the passing of structures through pointers using an example program with an output
12 Write note enumerated data type.
Enumerated Data Type in C:
An enumerated data type, often referred to as an enum, is a user-defined data type in C that consists of a set of
named integer constants. Enumerations provide a way to create symbolic names (enumerators) for integer values,
making the code more readable and less error-prone.
SYNTAX :
enum enum_name {
enumerator1,
enumerator2,
Key Points:
Enumerated data types are a powerful feature in C for creating clear and expressive code, especially when
dealing with a set of related constant values.
EXAMPLE
#include <stdio.h>
enum Color {
RED,
GREEN,
BLUE
};
int main()
switch (myColor) {
case RED:
break;
case GREEN:
break;
case BLUE:
break;
default:
printf("Invalid color.\n");
return 0;