Unit-III-PIC-QA If /co
Unit-III-PIC-QA If /co
com/programming-in-c-
for-msbte-k-scheme/
312303 - Programming In ‘C’ (Sem II)
As per MSBTE’s K Scheme
CO / CM / IF / AI / AN / DS
W-23,
S-23,
1 Define array .List its types. 2M
S-22,
W-19
Ans. An array is a collection of multiple data items, all of the same data type, accessed using
a common name.
1) A one-dimensional array consists of similar type of multiple values in it.
2) A two dimensional array consists of row and column.
2 Explain any two string handling functions with syntax and
W-23 4M
example.
Ans
a)Strlen function:
strlen( ) function in C gives the length of the given string. strlen( ) function counts the
number of characters in a given string and returns the integer value. It stops counting
the character when null character is found. Because, null character indicates the end
of the string in C.
Syntax:
strlen(stringname);
Example: consider str1=”abc”
strlen(str1); returns length of str1 as 3.
b)strcat() function:
In C programming, strcat() concatenates (joins) two strings. It
concatenates source string at the end of destination string.
Syntax:
strcat( destinationsource, source string);
Example: consider str1=”abc” and str2=”def”
c)strcpy() function
strncpy( ) function copies portion of contents of one string into
another string.
Syntax:
strncpy( destination string, source string, size );
Example: consider str1=”abc”
strcpy(str1,str2); returns abcstr2.
d)strcmp() function
The strcmp function compares two strings which are passed as
arguments to it. If the
strings are equal then function returns value 0 and if they are not
equal the function
returns some numeric value.
Syntax:
strcmp( str1, str2);
Example: consider str1=”abc” and str2=”abc”
Then strcmp(str1,str2) returns 0 as both the strings are same.
3 Illustrate initialization of one dimensional array with example. W-23 4M
Ans One dimensional array:
An array is a collection of variables of the same type that are referred through a
common
name. A specific element in an array is accessed by an index. all arrays consist of
contiguous memory locations. The lowest address corresponds to the first element
and the highest address to the last element.
Initialization:
Initialization can be done as compile time or runtime.
1. Compile time: This can be done by providing number of elements of the declared
data
type to an array at compile time.
Eg :int arr[5]={1,2,3,4,5};
2. Runtime: For this loop structures like ‘for’ can be used to iterate through the
locations
of the array. Here the index of the array starts with 0 and ends with size minus one
(size
– 1) of an array.
Eg :
int arr[5];
for(i=0;i<5;i++)
{
scanf(“%d”,&arr[i]);
}
W-23,W-
22,S-
6 Write a program to add two 3×3 matrices. Display the addition. 6M
22,W-
19,18
Ans #include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("Enter first matrix elements:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter second matrix elements:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\n\nAddition of two matrices is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
getch();
}
Write a C Program to add two distance in given kilometers using
7 S-23 4M
structure
Ans
Program:
#include <stdio.h>
struct Distance
{
int feet;
float inch;
} firstDistance, secondDistance, sum;
int main()
printf("Enter feet and inches for the first distance: \n");
scanf("%d %f", &firstDistance.feet, &firstDistance.inch);
printf("Enter feet and inches for the second distance: \n");
scanf("%d %f", &secondDistance.feet, &secondDistance.inch);
sum.feet = firstDistance.feet + secondDistance.feet;
sum.inch = firstDistance.inch + secondDistance.inch;
while (sum.inch >= 12)
{
sum.inch = sum.inch - 12;
sum.feet++;
}
printf("The Sum is %d feet, %.1f inches\n", sum.feet, sum.inch);
return 0;
}
Output:
Enter feet and inches for the first distance: 12.4
Enter feet and inches for the second distance: 4.1
The Sum is 16 feet, 0.5f inches
8 Write a c program for multiplication to two 3* 3 matrix S-23 4M
Ans
#include<stdio.h>
int main()
{
int i,j,k;
float a[3][3], b[3][3], mul[3][3];
printf("Enter elements of first matrix:\n");
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%f", &a[i][j]);
} }
printf("Enter elements of second matrix:\n");
for(i=0;i< 3;i++) {
for(j=0;j< 3;j++) {
printf("b[%d][%d]=",i,j);
scanf("%f", &b[i][j]);
} }
for(i=0;i< 3;i++) {
for(j=0;j< 3;j++) {
mul[i][j] = 0;
for(k=0;k< 3;k++) {
mul[i][j] = mul[i][j] + a[i][k]*b[k][j];
} } }
printf("Multiplied matrix is:\n");
for(i=0;i< 3;i++) {
for(j=0;j< 3;j++) {
printf("%f\t", mul[i][j]); }
printf("\n"); }
return 0;
}
Define:
9 (i) Two dimensional array S-18 2M
(ii) Multi-dimensional array
Ans
(i)Two dimensional array
Two dimensional array is a collection of similar type of data elements
arranged in the form of rows & columns.
E.g. Array can be declared as int arr[3][3];
In this therecanbe9 elements in an array with 3 row sand 3columns.
data_typemembern;
}structurevariable1, structurevariable2,..., structurevariablen;
Example:-
struct student
{
int rollno;
char name[10];
}s1;
Initialization:-
struct students={1,"abc"};
structure variable contains two members as roll no and name. the above example
initializes roll no to1and name to"abc".
Program:-
#include<stdio.h>
#include<conio.h>
struct college
{
int collegeid;
char collegename[20];
};
struct student
{
int rollno;
char studentname[10];
struct collegec;
};
void main()
{
struct students={1,"ABC",123,"Polytechnic"};
clrscr();
printf("\nRoll number=%d",s.rollno);
printf("\nStudent Name=%s",s.studentname);
printf("\nCollege id=%d",s.c.collegeid);
printf("\nCollege name=%s",s.c.collegename);
getch():
}
12 Design a programme in C to read the numbers of values in an
array and display it in reverse order. S-19 4M
(Note: Any other relevant logic shall be considered)
Ans #include<stdio.h>
#include<conio.h>
#define max 50
Void main()
{
int a[max],i,n;
clrscr();
printf("\n Enter number of elements:");
scanf("%d",&n);
printf("\n Enter array element:"); for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Array elements in reverse order:");
for(i=n-1;i>=0;i--)
printf("\t%d",a[i]);
getch();
}
13 Explain declaration and initialization of one dimensional array S-18 4M
using example.
Ans i) One dimensional array:
An array is a collection of variables of the same type that are referred through a
common name. A specific element in an array is accessed by an index. In C, all arrays
consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.
Syntax: data_type array_name[array_size];
Example:
int marks[10];
ii) Two dimensional array:
Two dimensional array is a collection of similar type of data elements arranged in
the form of rows &columns.
Example:
Array can be declared as int arr[3][3];
In this there can be 9 elements in an array with 3 rows and 3 columns.
14 Write a program to declare structure employee having data
member name, age, street and city. Accept data for two
employees and display it.
W-22 6M
Note: Two structure variables or array of structure variables
shall be considered.
Ans
#include<stdio.h>
#include<conio.h>
struct employee
{
char name[10],street[10],city[10];
int age;
};
void main()
{
int i;
struct employeee[2];
clrscr();
for(i=0;i<2;i++)
{
printf("\nEnter name:");
scanf("%s",&e[i].name);
printf("\nEnter age:");
scanf("%d",&e[i].age);
printf("\nEnter street:");
scanf("%s",&e[i].street);
printf("\nEnter city:");
scanf("%s",&e[i].city);
}
for(i=0;i<2;i++)
{
printf("\nName=%s",e[i].name);
printf("\nAge=%d",e[i].age);
printf("\nStreet=%s",e[i].street);
printf("\nCity=%s",e[i].city);
}
getch();
}
15 Write a program to accept a string as input from user and
determine its length. [Don’t use built in library function
W-18 4M
strlen()]
Ans #include<stdio.h>
#include<conio.h>
void main()
{
char str[50];
int i,len=0;
clrscr();
printf("Enter a string");
scanf("%s",&str);
for(i=0; str[i]!='\0'; i++)
{
len++;
}
printf("The length of string is %d" ,len);
getch();
}
16 State difference between array and string.
S-19 4M
(Note : Any two valid points shall be considered).
Ans
The array which is used to represent and store data in a tabular form is called as two
dimensional array. Such type of array is specially used to represent data in a matrix
form.
Declaration of two dimensional arrays:
Syntax:-
Datatype arrayname [rowsize][columnsize];
Eg:
int arr[3][4];
and 4 columns.
A two-dimensional array can be considered as a table which will have x number of
rows and y number of columns. A two-dimensional array a, which contains three
rows and four columns can be shown as follows
Thus, every element in the array a is identified by an element name of the form a[i][j],
where 'a' is the name of the array, and 'i' and 'j' are the subscripts that uniquely
identify each element in'a'.
Example:
main()
{
int a[2][2] = { {1,2} , {4,5});
int i, j;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
}
}
}
}
Explain any two string functions with example.
S-19
20 Strlen function: 4M
S-18
ANs
strlen() function in C gives the length of the given string. strlen() function counts the
number of characters in a given string and returns the integer value. It stops counting
the character when null character is found. Because, null character indicates the end
of the string in C.Syntax:
strlen(stringname);
Example:
strlen(str1);
returns length of str1 as 3
strcat()function:
In C programming, strcat() concatenates (joins) two strings. It concatenate source
string at the end of destination string.
Syntax:
strcat(destination , source string);
Example:
strcat(str1,str2);
returns abc defin str1 and str2 remains unchanged.
strcpy()function
strncpy() function copies portion of content of one string in to another string.
Syntax:
strncpy(destination string , source string, size);
Example:
strcpy(str1,str2);
returns abcstr2
strcmp()function
The strcmp function compares two strings which are passed as arguments to it. If the
stringsareequalthenfunctionreturnsvalue0andiftheyarenotequalthefunction
returnssomenumeric value.
Syntax:
strcmp(str1,str2);
Example:
for(i=0;i<10;i++)
printf("\n%d",a[i]);
getch();
}
23 Define array. List its type. S-19 2M
Ans Array is a fixed-size sequential collection of elements of the same type.
Types:
1. One dimensional
Multi dimensional
24 State the syntax & use of strlen () & strcat( ) function. S-19 2M
Ans
strlen(): calculates the length of the string
Syntax: strlen(s1);
strcat(): concatenates two strings
Syntax: strcat(s1,s2);
Ans
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
struct student{ int rollno;
charn ame[20]; int marks;
} s[3];
clrscr();
for(i=0;i<3;i++){
printf("Enter rollno, name and marks\n");
scanf("%d%s%d",&s[i].rollno,&s[i].name,&s[i].marks);
}
for(i=0; i<3;i++){
printf("\nThedetailsofstudent%d\n",i+1);
printf("Roll no %d\n",s[i].rollno);
printf("Name is %s\n",s[i].name);
printf("Marks %d\n",s[i].marks);
}
getch();
}
26 Illustrate initialization of two dimensional array with example. W-19 4M
Ans
Two dimensional array:
The array which is used to represent and store data in a tabular form
Is called as two dimensional array. Such type of array is specially
Used to represent data in a matrix form.
Initialization can be done as design time or runtime.
1.Design time:
This can be done by providing row X column‟
number of elements to the array.
Eg : for a 3 rows and 4 columns array ,3X4=12elementscan be provided as :
arr[3][4]={{2,3,4,6}, {1,4,6,3}, {6,6,4,3}, {6,7,8,9} };
2.Runtime:
For this loop structures like for can be used in a nested form, where outer loop will
increment row and inner loop will increment column.
Eg :
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
scanf(“%d”,&arr[i][j]);
}
}
Example:
main()
{
int arr[2][2]={{1,2},{4,5});
int i,j;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“%d”, arr[i][j]);
}
printf(“\n”);
}
}
Write a program to read two strings and find whether they are
27 equal or not. W-19 4M
(Note: Any other correct logic shall be considered).
Ans #include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char st1[20],st2[20];
printf(“enterstring1”);
scanf(“%s”,st1);
printf(“entersecondstring”);
scanf(“%s”,st2);
if(strcmp(st1,st2)==0)
printf(“\nbothstringsareequal”); else
printf(“\nstrings arenotequal”);
}
Write a program to sort elements of an array in ascending order.
28 W-19 4M
(Note:Any other correct logic shall be considered).
Ans
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,temp;
clrscr();
printf("\nEnter array elements:");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
{
for(j=0;j<4-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<5;i++)
printf("\n%d",a[i]);
getch();
}
Write a program to demonstrate use of strcmp( ), strcpy(),
29 S-22 6M
strlen(), strcat()
Ans :
#include<stdio.h>
#include<conio.h>
Void main(){
Char string1[25],string2[25];
int l;
Clrscr();
Printf(“***** performing string length ******\n”);
Printf(“enter only one string \n”);
Scanf(“%s”,string1);
l = strlen(string1);
printf(“the string length is %d\n\n”,l);
printf(“**** performing string concatenation ****\n”);
printf(“enter two strings\n”);
scanf(“%s%s”,string1,string2);
printf(“the concatenated string is %s\n\n”,strcat(string1,string2));
printf(“***** performing string compare *****\n”);
printf(“enter two strings \n”);
scanf(“%s%s”,string1,string2);
if(strcmp(string1,string2) = = 0)
printf(“strings are equal\n”);
else
printf(“strings are not equal\n”);
printf(“*** performing string copy ****\n”);
printf(“enter the two strings\n”);
scanf(“%d%d”,string1,string2);
printf(“the first string is %s and second string is %s\n”,string1,string2);
strcpy(string1,string2);
printf(“the first string is %s and second string is %s\n”,string1,string2);
getch();
}
Define structure. Give one example of structure
30 W-22 2M
declaration
Ans Definition of Structure:
Structure is a collection of variables of similar or different data types which is
represented by a single name.
Example:
struct bill
{
int consumer_id;
char address[50];
float amount;
};
Ans #include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,sum=0;
clrscr();
printf("Enter array elements:");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);for(i=0;i<5;i++)
sum=sum+a[i];
}
printf("\n Sum= %d",sum);getch();
}
OR
#include<stdio.h>
include<conio.h>
voidmain()
{
int a[5]={1,2,3,4,5},i,sum=0;// Array initialization at the time ofdeclaration
clrscr();
for(i=0;i<5;i++)
sum=sum+a[i];
printf("\n Sum= %d",sum);
getch();
}
Write a C program to declare structure employee having data
32 member name, age, designation and salary. Accept and display W-22 6M
information of 1 employee.
Ans
#include<stdio.h>
#include<conio.h>
struct employee
{
char name[20], designation[10];
int age;
long salary;
};
void main()
{
int i;
struct employeee;
clrscr();
printf("\nEnter name:");
scanf("%s",&e.name);
printf("\n Enter age:");
scanf("%d",&e.age);
printf("\n Enter designation:");
scanf("%s",&e.designation);
printf("\n Enter salary:");
scanf("%ld",&e.salary);
printf("\n\nEmployee'sdatais:");
printf("\n Name=%s",e.name);
printf("\nAge=%d",e.age);
printf("\nDesignation=%s",e.designation);
printf("\nSalary=%ld",e.salary);
getch();
}
Write ‘C’ program to add two distances
33 given in km using structure. S-23 4M
Ans
#include <stdio.h>
structDistance
Ans
#include <stdio.h>
void getSmallLarge(int arr[], int n)
{
int smallest, largest;
smallest = largest = arr[0];
}
int main()
{
int arr[] = {25, 40, 35, 20, 10, 80};
int len = sizeof(arr) / sizeof(arr[0]);
getSmallLarge(arr, len);
}
Output
Smallest: 10
Largest: 80
Ans ANS:
C Structure Declaration
We have to declare structure in C before using it in our program. In structure
declaration, we specify its member variables along with their datatype. We
can use the struct keyword to declare the structure in C using the following
syntax:
Syntax :
struct structure_name { data_type member_name1; data_type
member_name1;
....
....
};
The above syntax is also called a structure template or structure prototype
and no memory is allocated to the structure in the declaration.
C Structure Definition
To use structure in our program, we have to define its instance. We can do
that by creating variables of the structure type. We can define structure
variables using two methods:
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < n; ++i)
printf("%d\n", number[i]);
}
Explain any two string handling functions with syntax and
37 example. S-18 4M
(Any other relevant example should be considered.)
Ans 1.strlen():
strlen() function gives the length of the given string. strlen( ) function counts the
numberofcharactersinagivenstringandreturnstheintegervalue.Itstopscountingthech
aracterwhennullcharacteris found.
Syntax:
strlen(stringname);
Example:
#include<stdio.h>
#include<string.h>
int main()
{
charstr[]="Hello";
int length = strlen(str); // length will be
5printf("Length of the string: %d ", length);
return0;
}
Output: Length of the string : 5
2.strcat():
strcat() function concatenates (joins) twos trings.
Syntax:
strcat(destination_string , source_string);
It concatenates source string at the end of destination string.
Example:
#include<stdio.h>#include<string.h>intmain()
{
char dest[] = "Hello";charsrc[]="World";
strcat(dest,src);//destwillbecome"HelloWorld"prin
tf("Concatenated string:%s", dest);
return0;
}
Output:Concatenatedstring:HelloWorld
3. strcpy()
strcpy()functioncopiescontentsof onestringintoanotherstring.
Syntax:
strcpy(destinationstring,sourcestring);
Example:
#include<stdio.h>#include<string.h>intmain()
{
chardest[20];
charsrc[]="Welcome";
strcpy(dest,src);//destwillbecome"Welcome"pri
ntf("Copiedstring:%s", dest);
return0;
}
Output:CopiedString:Welcome
4.strcmp()
Thisfunction comparesthestringsandreturnsanintegervalue.
Syntax: strcmp( str1,
str2);Itreturns,
0 ifthestrings are equal
-1 ifstr1is lessthan str2
1 ifstr1isgreater thanstr2
Example
#include <stdio.h>
#include<string.h>
int main()
{
char str1[] =
"apple";charstr2[]="banan
a";
intresult= strcmp(str1, str2);
if(result== 0) {
printf("The strings are equal.\n");
}else if (result< 0) {
printf("String1 is less than string2.\n");
}else{
printf("String1 is greater than string 2.\n");
}
return0; }
Output: String1isless thanstring2.
5. strupr() :
The strupr() function is used to converts a given string to uppercase.
Syntax:
char*strupr(char*str);
Example:
intmain()
{
charstr[] ="hello";
//converting the given string into uppercase.
printf("%s\n",strupr (str));
return0;
}
Output:HELLO
6.strlwr():
The strlwr() function is used to convert a given string into lower case.
Syntax:
char* strlwr(char*str);
Example:
Int main()
{
Char str[]="HELLO";
//converting the given string into lower case.
printf("%s\n",strlwr (str));
return0;
}
Output:hello
7. strrev():
The strrev() function is used to reverse the given string.
Syntax:
char* strrev(char*str);
Example:
#include<string.h>
int main()
{
charstr[50]="Hello";
printf("The given string is =%s\n", str);
printf("After reversing string is = %s ", strrev(str)) ;
return0;
}
Output:
The given string is = Hello After reversing string is = olleH
Write a program to accept a string as input from user and
38 W-18 4M
determine its length
Ans. #include<stdio.h>
#include<string.h>
int main() {
char str[100];
int len;
printf("\nEnter the String : ");
scanf("%s",str);
/*
strlen() is the pre-defined function
to find the length of a string
*/
len = strlen(str);
Thank You
https://shikshamentor.com/programming-in-c-for-msbte-k-
scheme/
Visit
https://shikshamentor.com/