1.Write a C program for addition of two 3 x 3 matrices.
(4 marks)
ans:
#include <stdio.h>
#include <conio.h>
int main()
int a[3][3], b[3][3], sum[3][3], i, j;
clrscr();
printf("Enter elements of first matrix:\n");
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
scanf("%d", &a[i][j]);
printf("Enter elements of second matrix:\n");
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
scanf("%d", &b[i][j]);
printf("Sum of matrices:\n");
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
sum[i][j] = a[i][j] + b[i][j];
printf("%d ", sum[i][j]);
printf("\n");
getch();
return 0;
2.Differentiate between character array and integer array with respect to size and initialization.
Ans:
Character Array Integer Array
Store individual characters, Store individual characters, each occupying one
each occupying one byte of memory. byte of memory.
The size of a character array Is determined The size of a character array Is determined by
by the number of characters it can hold. the number of characters it can hold. For
For example a character array of size can example a character size of integer array is also
accommodate five characters. determined by the number of integer values it
can accommodate.
Initialization can be done like: Initialization can be done like:
char message[10]=”Hello”; int arr|4]={10,20,30,40};
3.Write a 'C' program to declare a structure employee having data members name,age,street and
city. Accept data for two employees and display it.
ans:
#include <stdio.h>
#include <conio.h>
struct employee
char name[20];
int age;
char street[30];
char city[20];
};
int main()
struct employee e[2];
int i;
clrscr();
for(i = 0; i < 2; i++)
printf("Enter details for employee %d:\n", i+1);
printf("Name: ");
scanf("%s", e[i].name);
printf("Age: ");
scanf("%d", &e[i].age);
printf("Street: ");
scanf("%s", e[i].street);
printf("City: ");
scanf("%s", e[i].city);
for(i = 0; i < 2; i++)
printf("\nEmployee %d:\n", i+1);
printf("Name: %s\n", e[i].name);
printf("Age: %d\n", e[i].age);
printf("Street: %s\n", e[i].street);
printf("City: %s\n", e[i].city);
getch();
return 0;
4.Explain any Four string handling functions with syntax. (2 marks)
Ans:
1,strlen() – it gives the length of the given string.
Syntax: strlen(stringname);
2. strcat() – it concatenates(joins)two strings.
Syntax: strcat(destination string,source string);
3. strcpy() – it copies contents of one string into another string.
Syntax: strcpy(destination string,source string);
4. strcmp() – it compares the strings and returns an integer value.
Syntax: strcmp(str1, str2);
5.Explain how to initialize and define structure in 'C'. (4 marks)
ans:
Structure is a user defined data type. It is a collection of different types combined together to
create a new type.
struct keyword can be used to declare a structure.
SYntax:
struct<name of structure>
//struct members
} variables;
Example:
struct car
char name[100];
int price;
}car1;
Initializing structure:
a)
Initialization the structure members directly like
below : struct car car1 ={"xyz", 987432};
b) To initialize or access a structure, dot. operator can be used as struct car car1
car1.name="xyz";
car1.price-"987432";
6.Write a C program to find the largest and smallest in an given array. (4 marks)
ans:
#include <stdio.h>
#include <conio.h>
int main()
int a[10], i, large, small;
clrscr();
printf("Enter 10 elements:\n");
for(i = 0; i < 10; i++)
scanf("%d", &a[i]);
large = small = a[0];
for(i = 1; i < 10; i++)
if(a[i] > large)
large = a[i];
if(a[i] < small)
small = a[i];
printf("Largest = %d\n", large);
printf("Smallest = %d\n", small);
getch();
return 0;
7.Write a C program to find a factorial of a number using recursion. (4 marks)
ans:
#include <stdio.h>
#include <conio.h>
int factorial(int n)
if(n == 0)
return 1;
Else
return n * factorial(n - 1);
int main()
int n;
clrscr();
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial of %d is %d\n", n, factorial(n));
getch();
return 0;
8.Differentiate between break and continue statements with examples. (2 marks)
Ans:
Break Continue
Exits loop or switch statement immediately Skips the current iteration and continues with
next iteration
For(int i=0;i<10;i++) For(int i=0;i<10;i++)
{ {
If(i==5) If(i==5)
Break; continue;
} }
9.Explain any two math function with syntax and give example of each. (2 marks)
ans:
Math Functions in C:
1.sqrt()
sqrt() is used to find square root of a number.
Syntax:
sqrt(no);
Example: printf("%f", sqrt(16)):
2.ceil()
ceil() function rounds a number upwards to its nearest integer.
Syntax:
ceil(no);
Example: printf("%f", ceil(1.4));
3.floor()
floor() method rounds a number downwards to its nearest integer, and returns the result,
Syntax: floor(no);
Example: printf("%f", floor(1.4));
10.Write a C program to sort elements of an array in ascending order. (4 marks)
ans:
#include <stdio.h>
#include <conio.h>
int main()
int a[5], i, j, temp;
clrscr();
printf("Enter 5 elements:\n");
for(i = 0; i < 5; i++)
scanf("%d", &a[i]);
for(i = 0; i < 5; i++)
{
for(j = i + 1; j < 5; j++)
if(a[i] > a[j])
temp = a[i];
a[i] = a[j];
a[j] = temp;
printf("Sorted array: ");
for(i = 0; i < 5; i++)
printf("%d ", a[i]);
getch();
return 0;
11.Write a C program to demonstrate concept of pointer to functions. (2 marks)
ans:
#include <stdio.h>
#include <conio.h>
void show()
printf("This is a function call via pointer\n");
}
int main()
void (*fptr);
clrscr();
fptr = show;
fptr();
getch();
return 0;
12.Define pointer. write syntax for pointer declaration. (2 marks)
ans: Pointer is a variable that stores address of another variable of similar data type.
Syntax:
datatype *pointer_variable_name;
ex: int *ptr;
13.Write a C program to compute the sum of all elements stored in an array using pointer. (4
marks)
ans:
#include <stdio.h>
#include <conio.h>
int main()
int a[5], i, sum = 0;
int *ptr;
clrscr();
printf("Enter 5 elements:\n");
for(i = 0; i < 5; i++)
scanf("%d", &a[i]);
ptr = a;
for(i = 0; i < 5; i++)
sum += *(ptr + i);
printf("Sum = %d\n", sum);
getch();
return 0;
14.Write output of the following programming code: (2 marks)
#include<stdio.h>
void main()
int x,y,a,b,*p1,*p2;
x=10;
y=20;
P1=&x;
P2=&y;
a=*p1**p2+20;
a=*p1**p2-20;
printf("x=%d,y=%d",x,y);
printf("a=%d,b=%d",a,b);
ans:
Output: x = 10, y = 20
a = 180, b = 0(garbage value)