Professional Documents
Culture Documents
Question Paper of
BCA in Programming
in C of Madras
University.
03/29/16
Referred Book:
Programming
Programming
Us C by Yashavant P.Kanetkar
03/29/16
Madras University
(April 2010)
Part A(10*2=20)
Answer any ten questions.
1)
2)
Operator
<
<=
>
>=
==
!=
3)
Meaning
Less than
Less than or equal to
Greater than
Greater Than or equal to
Equal to
Not equal to
Syntax:
Conditional expression?expression1:expression2
The Conditional expression is evaluated first. If the result is nonzero,
expression1 is evaluated and is returned as the value of the conditional
expression. Otherwise, expression2 is evaluated and its value is returned.
4) What is the use of \n?
Ans: C support special backslash character constants that are in output
function.The symbol \n stands for newline character
5) Define: recursion.
Ans: Recursion is a function that call itself repeatedly, until the required
condition is satisfied.
03/29/16
Type variable-name[size]
9)What are self-referential structures?
Ans: Structure which contain a member field that point to the same structure type are called
self-referential structures.
10) Mention the features of 'union' data type.
Ans: Union is similar to that of structure. The difference between Unions and structures is
the member of the union share the same storage area. However, only one variable can use
the memory location at any one time.
03/29/16
03/29/16
Part- B (5 * 5 = 25)
Answer any five questions.
13) Write short notes on : declarations and expressions in c.
Ans: Declaration of Variable:
1. It defines the name of the variable.
2. It defines the type of the data the variable will hold.(int,float,char,etc)
Primary type declaration:
A variable can be used to store a value of any data type. That is, the
name has nothing to do its type. The syntax for declaring a variable is as
follows:
data-type v1,v2vn;
v1,v2,vn are the names of variables. Variables are seperated by commas. A
declaration statement must end with a semi colon. For example valid
declaration are:
int count;
int are the keywords to represent integer type data value respectively.
03/29/16
Expression:
Expression in c are any valid combination of operators, operands, function and
variables. Expression are evaluated using an assignment statement of the form.
variable =expression;
variable is any valid C variable name. When the statement is encountered, the
expression is evaluated first and the result then replaces the previous values of the
variable on the left-hand side. All variable used in the expression must be assigned values
before evaluation is attempted. Examples of evaluated statements are
x= a*b-c;
y=b/c*d;
Expression :
03/29/16
10
Algebra expression
C expression
ab-c
(m+n)(x+y)_
ab
c
a*b-c
(m+n)*(x+y)
a*b/c
03/29/16
11
03/29/16
12
#include<stdio.h>
main()
{
int i=1,sum=0,n;
printf("Enter the number ");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
if(i%2!=0)
{
sum=sum+i;
}
}
printf("sum of odd number=%d",sum);
getch();
}
03/29/16
13
03/29/16
14
Example program :
03/29/16
15
#include<stdio.h>
main()
{
int i=1;
printf(" Display the number from 1 to 100:\n");
while(i<=100)
{
printf("%d\n",i);
i++;
}
getch();
}
03/29/16
16
Do statement:
In the case of do while loop, the statement are executed at least
once before the conditional expression is tested for its truth. The difference
between the while and do-while loop is that the while loop test the conditional
expression before executing the statements, whereas the do-while loop
executes the range of the loop first and then tests the condition. Hence even if
the conditional expression returns a false value, the body of the loop would
have been executed at least once in the case of do-while loop. The general form
of the do-while loop is
do
{
body of the loop;
}while (test condition);
03/29/16
17
Example program:
#include<stdio.h>
main()
{
int x,y=0;
printf("\nEnter a number :");
scanf("%d",&x);
03/29/16
18
do
{
y=y+x%10;
x=x/10;
}while(x>0);
printf("\n The sum of the number is %d",y);
getch();
}
03/29/16
19
Ans: Program:
#include<stdio.h>
main()
{
int m,n,i;
int larg;
printf("Enter the values of n: ");
scanf("%d",&m);
printf("Number %d :",1);
scanf("%d",&larg);
for(i=2;i<=m;i++)
{
printf("Number %d: ",i);
scanf("%d",&n);
03/29/16
20
if(larg<n)
larg=n;
}
printf("Largest number is: %d",larg);
getch();
}
03/29/16
21
Operator
Meaning
&&
||
!
Logical AND
Logical OR
Logical NOT
Example Program:
#include<stdio.h>
long fib(int);
main()
{
int n,i;
printf("Enter the values of n: ");
scanf("%d",&n);
03/29/16
22
for(i=1;i<n;i++)
{
printf("%d ",fib(i));
} getch();
}
long fib(int n)
{
if((n==1)||(n==2))
return(1);
else
return(fib(n-1)+fib(n-2));
}
03/29/16
23
24
Part-C (3 * 10 = 30)
Answer any three questions.
20) Write a C program to evaluate the series:s = 1 + 1/2 + 1/3 + ... + 1/n.
Ans : Program:
#include<stdio.h>
main()
{
double n,sum=0,i;
printf("\n Give The Value of N: ");
scanf("%lf",&n);
for(i=1;i<=n;i++)
{
sum = sum + (1/i);
if(i==1)
printf("\n 1 +");
else
03/29/16
25
if(i==n)
printf(" (1/%lf) ",i);
else
printf(" (1/%lf) + ",i);
}
printf("\n\n THE SUM OF THIS SERIES IS %.2lf",sum);
getch();
}
03/29/16
26
21) Explain about various data input and output functions in c with suitable examples
Ans: Various input and output function available in c language are discussed below
(i) Putchar() function:
The standard function that prints a single character on the standard
output device [Monitor] is called putchar.
Example Program:
#include<stdio.h>
main()
{
putchar('M');
putchar('a');
putchar('d');
putchar('a');
putchar('m');
getch();
}
03/29/16
27
(ii)Getchar() function:
The getchar() function is used to accept a character from
the standard input device [keyboard]. The use of getchar is illustrated in the
following example
#include<stdio.h>
main()
{
char c;
c=getchar();
putchar(c);
}
03/29/16
28
03/29/16
29
(iv)Scanf() function:
The scanf() function may contain only variable following the control string. It
should not contain any constant (or) values because the scanf() function is used to read values.
The syntax of the scanf() function is
03/29/16
30
03/29/16
31
03/29/16
32
03/29/16
33
03/29/16
34
FILE *fp;
Opening a file:
A data file must be opened prior to reading from it or writing on to it. The
library function fopen() is used to open a file
syntax:
fp=fopen(filename,mode);
This function takes two arguments. The first argument is a pointer to a
string containing name of the file to be opened while the second argument is the
mode in which the file is to be opened. The mode can be :
r : Open text file for reading. The stream is positioned at the beginning of
the file.
r+ : Open for reading and writing. The stream is positioned at the beginning of
the file.
03/29/16
35
w : Truncate file to zero length or create text file for writing. The stream is
positioned at the beginning of the file.
w+ : Open for reading and writing. The file is created if it does not exist,
otherwise it is truncated. The stream is positioned at the beginning of the file.
a : Open for appending (writing at end of file). The file is created if it does not
exist. The stream is positioned at the end of the file.
a+ : Open for reading and appending (writing at end of file). The file is created
if it does not exist. The initial file position for reading is at the beginning of the
file, but output is always appended to the end of the file.
Example:
#include<stdio.h>
main()
{
03/29/16
36
FILE *fp1,*fp2;
.
.
fp1=fopen(emp.dat,r);
fp2=fopen(salary.txt,w);
..
}
Closing a file:
Every file opened must be closed when it is no longer required by the
program. When all the input/output operation are completed on it. The fclose()
function is used to close an file.
The general form of fclose() function is
03/29/16
37
fclose(fp);
Example:
#include<stdio.h>
main()
{
FILE *fp;
..
..
fp1=fopen(emp.dat,r);
..
fclose(fp1);
}
03/29/16
38