Programming in C Summer 2018 Answer Paper
Programming in C Summer 2018 Answer Paper
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
SUMMER– 18 EXAMINATION
Subject Name: Programming in ‘C’ Model Answer Subject Code: 22218
Sub Marking
Q. No. Answers
Q. N. Scheme
Ans.: (Note: Any four other correct data type shall be considered) 2M
Data types in C language are : (½ mark
Character (char) is used to store single character or each for
correct
number at a time.
Primary or Any four
Integer (int) is used to store only integer values with data type)
basic data
no decimal points.
types
Float (float) is used to store only floating point
numbers with decimal points are allowed.
Double (double) has double value than float
Void – void
User defined Defined by users as per their need
data types Array , structure
B) State use of continue statement. 2M
Page 1 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
SUMMER– 18 EXAMINATION
Subject Name: Programming in ‘C’ Model Answer Subject Code: 22218
Ans.: Switch statement 2 M For
Uses single expression/condition for multiple choices. Correct
Syntax of switch case statement: syntax
switch(expression or variable )
{
case value1:
{
Statement;
break;
}
case value2:
{
Statement;
break;
}
.
.
Default:
{
Statement;
}
}
D) Give syntax of declaring user defined function. Give one example. 2M
Ans.: Function declaration: 1 Mark
for
A function declaration specifies function's name, parameters and return type. It declaratio
doesn't contain function body. A function declaration gives information to the n / syntax
compiler that the function may later be used in the program. of user
defined
Syntax of function declaration: function
and one
returnType functionName(type1 argument1, type2 argument2,...); mark for
any one
For example, int addNumbers(int a, int b); is the function declaration which relevant
provides following information to the compiler: use
SUMMER– 18 EXAMINATION
Subject Name: Programming in ‘C’ Model Answer Subject Code: 22218
// main function, program starts from here
int main( )
{
float m, n ;
printf ( "\nEnter some number for finding square \n");
scanf ( "%f", &m ) ;
Ans.: Pointer is variable used to store the memory address of the variable. One mark
Variables store the values and pointers stores their addresses at which these for
variables are located. meaning
Pointer declaration & initialization: and one
In initializion statement of pointer name of variable is preceded mark for
by & (address operator) operator. one
Syntax of initialization of pointer:- relevant
Pointer_name = & variable_name; example
Example:
int *ptr ; /* declaration of pointer ptr of int type*/
int a; /* declaration of integer variable a*/
Page 3 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
SUMMER– 18 EXAMINATION
Subject Name: Programming in ‘C’ Model Answer Subject Code: 22218
ptr = &a; /* pointer ptr is pointing to variable a*/
G) Give syntax of declaring and initializing of structure. 2M
Page 4 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
SUMMER– 18 EXAMINATION
Subject Name: Programming in ‘C’ Model Answer Subject Code: 22218
Ans.: %d and %f are format specifier used to access or display integer data types
and float data types respectively using variable in printf and scanf statements
in c programming. Two
General syntax of %d : marks for
Use of %d to declare and access the integer data types. use of %d
Example: and %f
scanf(“%d”,&num1); and 2M
for
General syntax of %f : example
Use of %f to declare and access the float data types. showing
Example: use of
scanf(“%f”,&percent); these
symbols
example of printf statements using %d and % f:
printf(“the marks of subject 1 are: %d”,num1);
printf(“the percentage of student is : %f”, percent);
B) Compare while and do-while loop. 4M
Ans.: String is collection of characters, numbers and special symbols. A string is Declaratio
terminated by a null character \0 (NULL Character). n with
Syntax for declaring string : example:
Char string_name[size]; 2 marks,
Declaring string of 8 characters. Initializati
char str[8]; on with
Str[0] 1000 example:
Str[1] 1001 2 marks
Str[2] 1002
Str[3] 1003
Str[4] 1004
Str[5] 1005
Str[6] 1006
Page 5 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
SUMMER– 18 EXAMINATION
Subject Name: Programming in ‘C’ Model Answer Subject Code: 22218
Str[7] 1007
int main()
{
recurse(); /* Sets off the recursion */
return 0;
}
Example:
Page 6 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
SUMMER– 18 EXAMINATION
Subject Name: Programming in ‘C’ Model Answer Subject Code: 22218
#include<stdio.h>
#include<conio.h>
void main()
{
int n,fact;
clrscr();
printf("enter the
number");
scanf("%d",&n);
fact=factorial(n);
printf("factorial of %d=%d",n,fact);
getch();
}
SUMMER– 18 EXAMINATION
Subject Name: Programming in ‘C’ Model Answer Subject Code: 22218
--y is similar to y=y-1 if y=5 then after --y or y--,y become 4. 2Marks)
Or y--is similar to y=y-1
Difference between i++ &++i with Example
Postfix increment operator (i++):
When postfix ++ or( --) is used with a variable in an expression ,the expression
is evaluated first using the original value of the variable and then the variable is
incremented (or decremented)by one.
Example:
main()
{
int a,z,i=10,j=20;
a=i * j++;
z=i * j;
printf(“\n a=%d z=%d”,a,z);
getch();
}
Output:
a=200 z=210
SUMMER– 18 EXAMINATION
Subject Name: Programming in ‘C’ Model Answer Subject Code: 22218
addresses can be manipulated like simple variables. You can increment, mark,
decrement, calculate or compare these addresses manually. List of
operations
C language provides a set of operators to perform arithmetic and comparison of : 1 mark
memory addresses. Pointer arithmetic and comparison in C is supported by Example:
following operators - 2 marks)
Example:
If a structure for student data is defined and it has to be used for 10 different
students, then array of structure can be declared as
struct student
{
int rollno;
Page 9 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
SUMMER– 18 EXAMINATION
Subject Name: Programming in ‘C’ Model Answer Subject Code: 22218
char name[20];
} s[10];
Here data in the form of rollno and name can be stored or accessed for 10
students.
Here s[0].rollno and s[0].name will be the data for first student.
s[1].rollno and s[1].name will be the data for second student and so on.
4. Attempt any THREE of the following 12M
(A) Write a ‘C’ program to enter basic salary. Calculate gross salary with 5% 4M
DA and 15% TA on basic salary. Display calculated gross salary.
Ans. #include<conio.h> Correct
#include<stdio.h> Program:
void main() 3 marks
{ Output: 1
int b_salary,DA,TA,g_salary; mark
clrscr();
printf("Enter basic salary:");
scanf("%d",b_salary);
DA=0.05*b_salary;
TA=0.15*b_salary;
g_salary=b_salary+DA+TA;
printf("Gross salary is:%d",g_salary);
getch();
}
Output:
Enter basic salary:1000
Gross salary is:1200
(B) Write a C program to find whether the given number is prime or not 4M
prime.
Ans: #include <stdio.h> Correct
#include<conio.h> Program:
void main() 3 marks
{ Output: 1
int n, i, c = 0; mark
printf("Enter the number :");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
if (n % i == 0)
{
c++;
}
}
if (c == 2)
{
printf("%d is a Prime number",n);
Page 10 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
SUMMER– 18 EXAMINATION
Subject Name: Programming in ‘C’ Model Answer Subject Code: 22218
}
else
{
printf("%d is not a Prime number",n);
}
return 0;
}
Output:
Enter the number:7
7 is a prime number
(C) Define array and explain how elements of array can be accessed. 4M
Ans: Definition: Array is a collection of variables having same data type referred by Definition
the same name. :1 mark
Accessing
Accessing elements of array: elements
of array:3
while accessing array elements we can use loop. The following code is used to marks
access elements of array,
for(i=0;i<10;i++)
{
printf(“\n Percent of student %d :\t %f”,i+1,percentage[i]);
}
SUMMER– 18 EXAMINATION
Subject Name: Programming in ‘C’ Model Answer Subject Code: 22218
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
Output:
Enter two numbers: 10
20
Numbers before swap:n1=10 n2=20
Output:
Enter roll number:10
Enter name:ABC
Enter marks:75.89
The given information is:
Roll no=10 Name=ABC Marks=75.89
5. Attempt any Two of the following: 12 Marks
A) Explain else-if ladder with syntax and its execution with example. Also 6M
draw flow chart for else-if ladder.
Ans: if-else Ladder Statement: (Introduct
The if-else ladder statement in C programming language is used to test set of ion: 1
Page 12 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
SUMMER– 18 EXAMINATION
Subject Name: Programming in ‘C’ Model Answer Subject Code: 22218
conditions in sequence. if condition is tested only when all previous if mark,
conditions in if-else ladder is false. If any of the conditional expression Syntax: 1
evaluates to true, then it will execute the corresponding code block and exits mark,
Explanati
whole if-else ladder.
on: 1
Syntax of if-else ladder statement: mark,
if(condition_expression_One) Flowchart
{ : 1 mark,
statement1; Example:
} any
else if (condition_expression_Two) program
{ using if-
statement2; else
} ladder: 2
else if (condition_expression_Three) marks)
{
statement3;
}
else
{
statement4;
}
Page 13 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
SUMMER– 18 EXAMINATION
Subject Name: Programming in ‘C’ Model Answer Subject Code: 22218
SUMMER– 18 EXAMINATION
Subject Name: Programming in ‘C’ Model Answer Subject Code: 22218
else if (marks >= 50 && marks < 70)
{
/* Marks between 50-69 */
printf("YOUR GRADE : C\n");
}
else
{
/* Marks less than 50 */
printf("YOUR GRADE : Failed\n");
}
getch();
}
Output:
Enter your marks
96
YOUR GRADE : A
Enter your marks
75
YOUR GRADE : B
Enter your marks
60
YOUR GRADE : C
Enter your marks
35
YOUR GRADE : Failed
B) Write the program to accept 10 (ten) numbers from user using array, 6M
search and print the location of a given number.
Ans: Program: (Syntax: 3
#include <stdio.h> marks,
#include<conio.h> Logic: 3
void main( ) marks)
{
int array[100], search, c;
printf("Enter 10 numbers\n");
for (c = 0; c < 10; c++)
scanf("%d", &array[c]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (c = 0; c < 10; c++)
{
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
Page 15 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
SUMMER– 18 EXAMINATION
Subject Name: Programming in ‘C’ Model Answer Subject Code: 22218
if (c == 10)
printf("%d isn't present in the array.\n", search);
getch( );
}
Output:
Enter 10 numbers
4
3
7
2
9
6
5
1
8
10
Enter a number to search
2
2 is present at location 4.
c) Write a ‘C’ program to print factorial of number n 6M
(i.e. n! = n x (n-1) x (n-2) x …) using recursion function.
Ans: Program: (Syntax: 3
#include<stdio.h> marks,
#include<conio.h> Logic: 3
int factorial(int n); marks)
void main( )
{
int fact,num;
printf("\n Enter Number=");
scanf("%d",&num);
fact=factorial(num);
printf("\n Factorial of a number = %d",fact);
getch( );
}
int factorial(int n)
{
int f;
if(n==1)
return 1;
else
{
f = n * factorial(n-1);
return f;
}
}
OUTPUT:
Page 16 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
SUMMER– 18 EXAMINATION
Subject Name: Programming in ‘C’ Model Answer Subject Code: 22218
Enter Number=5
Factorial of a number=120
6. Attempt any Two of the following: 12 Marks
A) Write a ‘C’ program to copy one string into another without using strcpy 6M
function.
Ans: Program: (Syntax: 3
#include <stdio.h> marks,
#include<conio.h>
void main( ) Logic: 3
{ marks)
char s1[100], s2[100], i;
printf("Enter string s1: ");
scanf("%s",s1);
for(i = 0; s1[i] != '\0'; i++)
{
s2[i] = s1[i];
}
s2[i] = '\0';
printf("String s2: %s", s2);
getch( );
}
Output:
Enter String s1: hello
String s2: hello
B) Write a ‘C’ program to find sum of natural number entered by user. 6M
Ans: #include <stdio.h> (Syntax: 3
#include<conio.h> marks,
void main( )
{ Logic: 3
int n, i, sum = 0; marks)
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=1; i <= n; ++i)
{
sum += i; // sum = sum+i;
}
printf("Sum = %d",sum);
getch( );
}
Output:
Enter a positive integer: 100
Sum = 5050
C) Declare a structure circle containing data members as radius, area, 6M
perimeter. Accept radius for one variable from user and find out perimeter
and area.
Ans: Program: (Syntax: 3
Page 17 of 18
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous)
(ISO/IEC - 27001 - 2013 Certified)
SUMMER– 18 EXAMINATION
Subject Name: Programming in ‘C’ Model Answer Subject Code: 22218
#include<stdio.h> marks,
#include<conio.h> Logic: 3
struct circle marks)
{
float radius;
float area;
float perimeter;
}c;
void main( )
{
printf(" Enter radius:");
scanf("%f",&c.radius);
c.area = 3.14 * c.radius * c.radius;
c.perimeter = 2 * 3.14 * c.radius;
printf("\n Area of circle=%f \n Perimeter of Circle=%f",c.area,c.perimeter);
getch( );
}
Output:
Enter radius:5.0
Area of circle=78.500000
Perimeter of Circle=31.400000
Page 18 of 18