0% found this document useful (0 votes)
2 views9 pages

Programming in c

The document provides an overview of programming in C, covering data types, variables, operators, and control statements. It includes explanations of arrays, searching algorithms, sorting algorithms, string operations, and memory management concepts like pointers and call by reference. Additionally, it contains example C programs for tasks such as matrix multiplication and calculating factorial using recursion.

Uploaded by

kathiravan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Programming in c

The document provides an overview of programming in C, covering data types, variables, operators, and control statements. It includes explanations of arrays, searching algorithms, sorting algorithms, string operations, and memory management concepts like pointers and call by reference. Additionally, it contains example C programs for tasks such as matrix multiplication and calculating factorial using recursion.

Uploaded by

kathiravan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

PROGRAMMING IN C

1)What are the different data types available in “C”?


char, int, float and double

2) What is a Variable? Illustrate it with an example


A variable is a symbolic name used to represent data ,Example: It acts as a container for storing values,
which can be numbers, text, or other types of data.

3) Bitwise Operators:

These operators work on bits and perform operations bit by bit.

Operator
&,
`
^
~
<<
>>

Logical Operators:

These operators are used to combine conditional statements.

Ex: &&, !.

4) What is the difference between ++a and a++?

++a (Pre-increment):

 Definition: Increments the value of a before using it in the expression.

a++ (Post-increment):

 Definition: Uses the current value of a before incrementing it.

5) Define Array
the same data type in contiguous memory locations.

6)what is getchar()

getchar() is a standard library function

reads a single character from standard input

7)Name any two library functions for handling string.

 strlen() – Returns the length of a string.

 strcpy() – Copies one string to another.

8)Define Searching

Searching is the process of looking for a specific item

 linear Search  Binary Search .

9)What are * and & operators means?

The * (asterisk)

Used to access the value stored at a memory address pointed to by a pointer.

and & (ampersand)

Retrieves the memory address of a variable.

10. What is the difference between an array and pointer?

 Array: A collection of elements of the same type stored in contiguous memory locations.

 Pointer: A variable that stores the memory address of another variable (including an array).

PART B
11. A Explain about the various decision making statements in “C” language.

if Statement

if-else Statement

if-else if-else Ladder

Nested if Statement

switch Statement
11. B (i) Discuss about the various data types in “C”.

a) Integer (int)

b) Floating Point (float and double)

c) Character (char)

11. B (ii) Explain various operators in c with example?

Arithmetic Operators

Relational (Comparison) Operators

Logical Operators

Bitwise Operators

Assignment Operators

Increment and Decrement Operators

12. A Explain the different looping statement with example?

1. For Loop
2. While Loop
3. Do-While Loop

12. B Write a C program to find the sum of 10 non-negative numbers entered by the user.

#include <stdio.h>

int main() {

int numbers[10];

int sum = 0;

printf("Enter 10 non-negative numbers:\n");

for (int i = 0; i < 10; i++) {

do {

printf("Number %d: ", i + 1);


scanf("%d", &numbers[i]);

if (numbers[i] < 0) {

printf("Please enter a non-negative number.\n");

} while (numbers[i] < 0);

sum += numbers[i];

} printf("The sum of the entered numbers is: %d\n", sum);

return 0;

13. A Explain in detail about One-dimensional and Two-dimensional Array.

One-Dimensional Array

A one-dimensional array is a collection of elements stored in a linear sequence, where each


element is accessible by an index.

Declaration and Initialization


int arr[5]; // Declares an integer array of size 5
int arr2[5] = {1, 2, 3, 4, 5}; // Declares and initializes an array

Memory Representation

For int arr[5] = {1, 2, 3, 4, 5};, memory is allocated as follows:

Two-Dimensional Array

A two-dimensional array is an array of arrays, meaning it stores elements in a matrix format (rows and
columns).

Declaration and Initialization


int matrix[3][3]; // Declares a 3x3 integer array
int matrix2[2][3] = { {1, 2, 3}, {4, 5, 6} }; // Declares and initial

13. B (i) Explain Binary Search in C


Binary Search in C

Binary Search is an efficient searching algorithm that finds an element in a sorted array by
repeatedly dividing the search range in half. It follows the divide and conquer approach.

How Binary Search Works

1. Find the middle element of the sorted array.


2. Compare the middle element with the target:
o If they are equal, return the index.
o If the target is less than the middle element, search the left half.
o If the target is greater, search the right half.
3. Repeat the process until the element is found or the search range is empty.

13 B (ii) Explain Selection Sort in C

Selection Sort in C

Selection Sort is a simple and efficient sorting algorithm that repeatedly selects the smallest
element from the unsorted part of the array and swaps it with the first unsorted element.

How Selection Sort Works

1. Find the smallest element in the unsorted part of the array.


2. Swap it with the first unsorted element.
3. Move the boundary between the sorted and unsorted parts forward.
4. Repeat the process until the entire array is sorted.

14. A Explain Linear Search in C

Linear Search in C

Linear Search is a simple searching algorithm used to find the position of a target element in a
list or array.

 Start from the first element of the array.

 Compare each element with the target element.

 If a match is found, return the index of the element.

 If the element is not found, return -1 indicating the element is not present in the array.
14. B Explain in detail about String Operations

String Operations in Programming

A string is a sequence of characters used to represent text in programming. String operations


allow manipulation, transformation, and retrieval of specific data from strings.

1. String Creation

Strings can be created using single, double, or triple quotes.

Concatenation is the process of joining two or more strings.

Repeating a string multiple times.

Getting the number of characters in a string.

15. A What do you mean by Call by Reference? Explain with an example.

Call by Reference

Call by Reference is a method of passing arguments to a function where the actual memory
address of the variable is passed. In Call by Reference, the function operates on the actual data,
rather than a copy, making it efficient for large data structures.

Key Points:
 Memory Efficiency: No extra memory is used since no copies are made.
 Faster Execution: Since original variables are modified directly.

 Use Cases: Useful when dealing with large data structures like arrays or objects.

15. B Can you subtract pointers from each other? Why would you?

 Finding the index difference – Subtracting two pointers that point to elements of the same
array gives the number of elements between them.

 Iterating over an array efficiently – This is useful in pointer arithmetic, especially for
determining how far one pointer is from another.

 Checking the range of pointers – Ensures that a pointer stays within the bounds of an array.

Rules for Pointer Subtraction

 Both pointers must point to elements within the same array.


 The result is the number of elements between the two pointers, not the byte difference.
 The result is an ptrdiff_t type (from <stddef.h>), which represents signed integer
difference.

16. A Write a C Program for 3×3 Matrix Multiplication.

#include <stdio.h>

#define SIZE 3

void multiplyMatrices(int firstMatrix[SIZE][SIZE], int secondMatrix[SIZE][SIZE], int resultMatrix[SIZE]


[SIZE]) {

for (int i = 0; i < SIZE; i++) {

for (int j = 0; j < SIZE; j++) {

resultMatrix[i][j] = 0;

for (int k = 0; k < SIZE; k++) {

resultMatrix[i][j] += firstMatrix[i][k] * secondMatrix[k][j];

void displayMatrix(int matrix[SIZE][SIZE]) {

for (int i = 0; i < SIZE; i++) {

for (int j = 0; j < SIZE; j++) {

printf("%d\t", matrix[i][j]);

printf("\n");

}
}

int main() {

int firstMatrix[SIZE][SIZE], secondMatrix[SIZE][SIZE], resultMatrix[SIZE][SIZE];

printf("Enter elements of first 3x3 matrix:\n");

for (int i = 0; i < SIZE; i++) {

for (int j = 0; j < SIZE; j++) {

scanf("%d", &firstMatrix[i][j]);

} printf("Enter elements of second 3x3 matrix:\n");

for (int i = 0; i < SIZE; i++) {

for (int j = 0; j < SIZE; j++) {

scanf("%d", &secondMatrix[i][j]);

multiplyMatrices(firstMatrix, secondMatrix, resultMatrix);

printf("Resultant Matrix after multiplication:\n");

displayMatrix(resultMatrix);

return 0;

16. B Write a C Program to find factorial using recursion

#include <stdio.h>

// Function to calculate factorial using recursion


long long factorial(int n) {

if (n == 0 || n == 1) {

return 1;

return n * factorial(n - 1);

}int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num < 0) {

printf("Factorial is not defined for negative numbers.\n");

} else {

printf("Factorial of %d is %lld\n", num, factorial(num));

return 0;

You might also like