answers of 'c' for s.r.k.goenka college students of bca i
DESCRIPTION
This is collection of some guess questions of C language for S.R.K.G college, Sitamarhi and other C learning students.TRANSCRIPT
ANSWERS OF ‘C’
BY AMIT RANJAN
7. a. What is identifier? What are the rules to be followed for naming an identifier? (-09)
Ans. Identifier refers to the name of variables, function and arrays. These are user defined
names. The names may be in upper case and lower case.
Rules for naming an identifier are given below:
1. First character must be an alphabet (or underscore).
2. Name must consist of only letters, digits or underscore.
3. Name must be up to 31charecters long.
4. We can not use any keyword.
5. Name must not contain white space.
int float; is wrong identifier name where as int sum; is right.
b. Differentiate between Global & Local variables with examples. (-09)
Ans.
Local Variable Global Variable
Local variables are declared inside a
function.
Global Variables are declared before the
main function.
Local Variables cannot be accessed outside
the function.
Global Variables can be accessed in any
function.
Local Variables are alive only for a function. Global Variables are alive till the end of the
program.
Example of local variable.
//program to add any two integers
void main()
{
int a,b,sum;
clrscr();
printf("Enter any two integer value");
scanf("%d%d",&a,&b);
sum=a+b;
printf("\nSum of two integers %d and %d is %d",a,b,sum);
}
Here, a,b, and sum are local variables which are declared in main function.
Example of global variable.
//program to find the sum and difference between two numbers
int a,b,result;
void main()
{
clrscr();
sum();
sub();
getch();
}
sum()
{
printf("Enter two numbers to find their sum");
scanf("%d%d",&a,&b);
result=a+b;
printf("\n the sum of two numbers is %d",result);
return 0;
}
sub()
{
printf("Enter two numbers to find their difference");
scanf("%d%d",&a,&b);
result=a-b;
printf("\n the difference between two numbers is %d",result);
return0;
}
Here, a,b and result are global variables which are declared before the main function.
8. a. How are arrays usually processed in C? Can entire array be processed with single
instructions without repetition? (-09)
Ans. C supports a derived data type known as array that can be used for a powerful data
type that would facilitate efficient storing, accessing and manipulation of data items.
An array is a fixed size sequenced collection of elements of the same data type.
Yes, we can process entire array with single instruction without repetition.
Program written bellow is an example of that.
main()
{
int a[5];
printf("Enter the elements of an array ");
scanf("%d%d%d%d%d",&a[0],&a[1],&a[2],&a[3],&a[4]);
printf("%d%d%d%d%d",&a[0],&a[1],&a[2],&a[3],&a[4]);
getch();
}
b. What is Subscript? How are they written? What restrictions apply to the values that
can be assigned to subscripts? (-09)
Ans. We can use an array name to represent a set of elements. We can refer to the individual
element by writing a number called subscript. It is also known as index. Subscripts
always begin at 0 (not 1) and end at size-1. the subscript in a two dimension array
represent rows and columns
Subscripts are written in brackets after the array name.
Following are the restrictions apply to the values that can be assigned to
subscripts:
1. It accept only positive value that is greater than 0.
2. An incorrect or invalid subscript may cause unexpected results.
3. There is no bound checking for the subscript in C.
//program to store the elements in an array & copy to another array & print it
void main()
{
int a[5],b[5],i;
clrscr();
printf("Enter the element of an array");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
for(i=0;i<5;i++)
b[i]=a[i];
for(i=0;i<5;i++)
printf("%d ",b[i]);
getch();
}
Here, a & b are the array names and 5 in bracket [ ] after the array names is known as
subscript
9. a. What is a function? State several advantages to use of functions. (-09)
Ans. A function is a set of codes that performs a particular task. Once a function has been
designed and packed it can be treated as a block box that take some data from the main
program and return a value. It is used to divide the program in small parts.
There are two types of functions in C one is user define function and other is
system define function.
There are three parts of user define function found in C:
1. Function prototype
It describes the syntax of a function it means the function is working on
which type of data should be declared here. The function prototype is declared
above the main function.
2. Function calling
In the function calling we pass only the variable name. It is also known as
actual argument on which the function should work. It is declare within the main
function.
3. Function definition
It is declared outside the main function. It works on the data which is passed
by the calling function. The argument which holds the value of calling function is
known as formal argument.
For systems define function the prototype and definitions are declared in the IDE
or header files. So when we use any system define function, we have to use the library
files.
The main advantages of making programs using functions are given bellow:
1. The complexity of the entire program can be divided into simple subtask and
then function subprograms can be written for each subtask.
2. The subprograms are easier to write, understand, and debug.
3. A function can be shared by other programs by compiling this function
separately and loading and linking them together.
4. In C, a function can call itself again. It is called a recursive function. Many
calculations can be done easily by using recursive functions such as calculation
of factorial of a number, etc.
5. Reduction in size of program due to program code of a function can be used
again and again, by calling it.
6. A library of a function can be designed and tested for use of every programmer.
b. What is a structure? How does a structure differ from an array? (-09,-07)
Ans. Structure is the collection of different data types. It allocates memory for the highest
data items. We can access more than one element at a time of all items. We can access
the elements of structure by using dot (.) operator in case of normal variable or by
using (→) arrow operator in case of pointer.
//Demonstration of structure
struct student
{
char name[10];
int rall;
float age;
}s;
struct student s;
main()
{
printf("Enter the name, rall no., and age of a student ");
scanf("%s%d%f",s.name,&s.rall,&s.age);
printf("%s%d%f",s.name,s.rall,s.age);
getch();
}
Structure Array
It does not allocate memory till the elements
of the structure are access.
It allocates memory only for the elements of
the subscripts.
It allocates the memory for the highest data
type.
It allocates memory of same files that is if
we declare integer type array then it
allocates 2v byte memory for each cell.
It contain array within itself. It does not contain structure within itself.
It can contain only non-homogeneous data
types.
It can contain only homogeneous data types.
The elements of structure are accessed by
using dot (.) operator with structure
reference name.
We can access the array by using index
number.
10.a. Write a program in C to generate even & odd number between 1 to any number and
their sum. (-09)
Ans. //program to generate even & odd number between 1 to any number and their sum
main()
{
int a,b,sum=0,sum1=0;
printf("Enter any number upto when you want to generate even and odd number and
their sum ");
scanf("%d",&a);
printf("The even numbers between 1 and %d is given below:- \n",a);
for(b=2;b<=a;b+=2)
{
printf("%d ",b);
sum=sum+b;
}
printf("\nThe odd numbers between 1 and %d is given below:- \n",a);
for(b=1;b<=a;b+=2)
{
printf("%d ",b);
sum1=sum1+b;
}
printf("\nThe sum of all even numbers is %d\nThe sum of all odd numbers is
%d",sum,sum1);
printf("\nThe sum of all even and odd numbers is %d",sum+sum1);
getch();
}
Output
Enter any number upto when you want to generate even and odd number and their
sum 50
The even numbers between 1 and %d is given below:-
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
The odd numbers between 1 and %d is given below:-
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
The sum of all even numbers is 650
The sum of all odd numbers is 625
The sum of all even and odd numbers is 1275
b. Write a program in C to check a given number is palindrome or not.
Ans. //program in C to check a given number is palindrome or not
void main()
{
int a,b,c,d=0;
clrscr();
printf("Enter any number ");
scanf("%d",&a);
for(b=a;b>0;b/=10)
{
c=b%10;
d=d*10+c;
}
if(d==a)
printf("%d is Palindrome",a);
else
printf("%d is not palindrome",a);
getch();
}
Output
Enter any number 2882
2882 is Palindrome
11.a. Write a program in C to find the smallest and largest element of an array.
Ans. //program to find the highest and smallest element within an array
void main()
{
int a[5],i,j,k;
clrscr();
printf("Enter 5 element of an array ");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
j=k=a[0];
for(i=0;i<=4;i++)
{
if(a[i]>j)
{
j=a[i];
}
else if(a[i]<k)
{
k=a[i];
}
}
printf("%d is largest and %d is the smallest number of the array",j,k);
getch();
}
Output
Enter 5 element of an array 27 3 28 92 24
92 is largest and 3 is the smallest number of the array
b. Write a program in C that reads & displays the information of a student using
structure.
Ans. struct student
{
char name[10];
int roll;
float age;
}s;
struct student s;
main()
{
printf("Enter the name, rall no., and age of a student ");
scanf("%s%d%f",s.name,&s.rall,&s.age);
printf("The name of the student is %s and his roll no. is %d and he is %f years old ",
s.name,s.roll,s.age);
getch();
}
Output
Enter the name, roll no., and age of a student amit 243 18
The name of the student is amit and his roll no. is 243 and he is 18 years old
7. a. What are Operators? Describe six types of operator. (-08)
Ans. An Operator is a symbol that tells the computer to perform certain mathematical or
logical manipulations. It is used to manipulate data and variables. They usually form a
part of the mathematical or logical expressions e.g., y=a + b. here = and + are operators
and y, a and b are operands.
Different types of operators are given bellow:
1. Arithmetic operator
C provides all the basic arithmetic operators
Operators Meaning
+ Addition or unary plus (single operand)
− Subtraction or unary minus (single operand)
* Multiplication
/ Division
% Modulo division
When both the operands in a single arithmetic expression such as a + b are
integers, the expression is called an integer expression, and the operation is called
integer arithmetic.
If a and b are integers, then for a = 14 and b = 4 we have the following results:-
a – b = 10, a + b = 18, a * b = 56, a / b = 3 (decimal part truncated / quotient) and
a % b = 2 (remainder of division)
2. Relational operator
The symbols used to compare two quantities are called relative operators. It
compare the operands and give its result is either true or false or one or zero.
There are six relational operators:
Operator Meaning
< Is less than.
<= Is less than or equal to.
> Is greater than.
>= Is greater than or equal to.
== Is equal to.
!= Is not equal to.
For example, 10 < 20 is true but 20 < 10 is false. Among the six relational
operators, each one is a complement of another operator.
> is complement of <=
< is complement of >=
== is complement of !=
3. Logical operator
There are three logical operators, which are used to test more than one condition
and make decisions. E.g. a > b && x = = 10
C has the following three logical operators
&& meaning logical AND
¦¦ meaning logical OR
! meaning logical NOT
Like the simple relational expression, a logical expression also yields a value of
one or zero. The logical expression given above is true only if a > b is true and x == 10
is true. If either or both of them are false, the expression is false.
4. Assignment operator
Assignment operators are used to assign the result of an expression to a variable.
The usual assignment operator is „=‟.
5. Increment and decrement operator
C allows two very useful operators not generally found in other languages. These
are the ++ (increment) and −− (decrement) operators.
The operator ++ adds 1 to the operand, while −− subtract 1. both are unary
operators and taken the following form:
++a; or a++;
−−a; or a−−;
6. Bitwise operator
Bitwise operators are used for manipulation of data at bit level. These operators
are used for testing the bits, or shifted them right or left. These may not be applied to
float or double.
Operator Meaning
& Bitwise AND
¦ Bitwise OR
^ Bitwise exclusive OR
<< Shift left
>> Shift right
b. What are the characteristic features of C language? (-08,-06)
Ans. Following are the characteristics features of C language:-
1. It is a general purpose structured programming language that is powerful, efficient
and compact.
2. It is a robust language which contains rich set of built in functions and operators.
3. It has varieties of data types and powerful operators, so it is efficient and fast to
write the program.
4. It is highly portable i.e. program written in C for a computer can be run on another
with little or no modification.
5. It is well suited for structure programming.
6. it has properties to extend itself i.e. we can add our own function to C libraries.
8. Write notes on any of three: (-08)
a. gets()
The name of the function gets() stands for “get string”. The gets() function reads
till the next new line character Enter key and can include spaces and taken in a string
thus reading whole sentences and adds the NULL character to it and assign it to the
required variables which comes as an argument of the function. Following example
demonstrates the use of the function gets().
/*Demonstration of gets() function */
main()
{
char employee_name[20];
gets(employee_name);
printf("Employee: %s\n",employee_name);
}
b. malloc ( ) function
The malloc() function allocates a specified number of bytes in memory. The allocated
region is not filled with zero. The starting address is returned if the function is
successful. A zero is returned if the function fails to allocate memory. To assign
sufficient memory for x, we can make use of the library function malloc(), as follows:
X = malloc(10*size of(int))
This function reserves a block of memory whose size (in bytes) is equivalent to the
size of an integer.
c. Pointer
A pointer is a derive data type in C. it is built from one of the fundamental data types
available in C. Pointer contains memory address contains the instruction and data as a
record.
Pointer is used to access and manipulate data stored in the memory. It is also known
as referential variable.
d. Union
Unions are concept borrowed from structures and therefore follow the same syntax as
structures. However, there is a major distinction between them in terms of storage. In
structures, each member has its own storage location; where as all the members of a
union uses the same location. This implies that, although a union may contain many
members of different types, it can handle only one member at a time. Like structure, a
union can be declared using the keyword union as follows:
union item
{
int m;
float x;
char c;
}code;
This declare a variable code of type union item.
e. Recursion (-08,-07)
Recursion is a special case of chaining of a function, where a function calls itself. A
very simple example of recursion is given below:
main()
{
printf("This is an example of recursion\n");
main();
}
When executed this program will produce an output something like this:
This is an example of recursion
This is an example of recursion
This is an example of recursion
Execution is terminated abruptly; otherwise the execution will continue indefinitely.
9. a. What is user define function? Mention some advantages. (-08)
Ans. C functions can be classified into two categories, namely, library functions and user-
define functions. Main is an example of user defined functions. Printf and scanf
belongs to the category of library functions. The main distinction between these two
categories is that library functions are not required to be written by us whereas a user-
defined function has to be developed by the user at the time of writing a program.
However, a user define function can later become a part of the C program library.
The main advantages of user-defined functions are given bellow:
1. The complexity of the entire program can be divided into simple subtask and
then function subprograms can be written for each subtask.
2. The subprograms are easier to write, understand, and debug.
3. A function can be shared by other programs by compiling this function
separately and loading and linking them together.
4. In C, a function can call itself again. It is called a recursive function. Many
calculations can be done easily by using recursive functions such as calculation
of factorial of a number, etc.
5. Reduction in size of program due to program code of a function can be used
again and again, by calling it.
6. A library of a function can be designed and tested for use of every programmer.
b. What are actual and formal parameters? (-08,-06)
Ans. Actual parameters are the parameter on which the function should work. It is declare
within the main function. In actual parameter we pass only the variable name.
Formal parameters are declared outside the main function. It works on the data
which is passed by the calling function. Formal parameter holds the value of calling
function.
10. a. Write a program to enter three numbers and find the smallest of them. (-08)
Ans. /*program to find the smallest number within three numbers*/
void main()
{
int a,b,c;
clrscr();
printf("Enter three numbers ");
scanf("%d%d%d",&a,&b,&c);
if(a<b && a<c)
printf("%d is smallest",a);
else if(b<a && b<c)
printf("%d is smallest",b);
else
printf("%d is smallest",c);
}
Output
Enter three numbers 24 53 12
12 is smallest
b. Write a program to generate Fibonacci series up to given number of terms. (-08)
Ans. //program to print Fibonacci series upto 21
void main()
{
int a=-1,b=1,x,c;
clrscr();
printf("Enter a number ");
scanf("%d",x);
c=0;
while(c<=x)
{
c=a+b;
a=b;
b=c;
printf("%d\t",c);
}}
Output
Enter a number 13
0 1 1 2 3 5 8 13
11. a. Write a program to calculate the area of a triangle using function (if wrong set of
values is entered, error message should be displayed accordingly). (-08)
Ans. /*program to read three numbers from the key board and print the area of triangle by
using function*/
#include<math.h>
float triangle(float,float,float,float,float);
void main()
{
float s,a,b,c,ar;
clrscr();
printf("Enter three values for triangle ");
scanf("%f%f%f",&a,&b,&c);
triangle(a,b,c,s,ar);
//if(r==0)
getch();
}
float triangle(float a,float b,float c,float s,float ar)
{
if(c>a&&c>b&&a+b>c||b>a&&b>c&&a+c>b||a>b&&a>c&&b+c>a)
{
s=((a+b+c)/2);
ar=sqrt(s*(s-a)*(s-b)*(s-c));
printf("\nThe area of triangle is %f",ar);
}
else
printf("\nSORRY try again, triangle of this measurement doesn‟t exist ");
return 0;
}
Output
Enter three values for triangle 3 4 5
The area of triangle is 6.000000
Or
Enter three values for triangle 2 5 8
SORRY try again, triangle of this measurement doesn‟t exist
b. Write a program to find the factorial of a given number using recursion function. (-
08,-06)
Ans. /*program to show the factorial of any number which is inputed by the user by
recurtion*/
void main()
{
int a;long int c=1,f;
clrscr();
printf("Enter any number to know its factorial ");
scanf("%d",&a);
f=fact(a,1,c);
printf("\nFactorial of %d is %ld ",a,f);
getch();
}
fact(int a,int b,long int c)
{
if(b<=a)
{
c=c*b;
return fact(a,(b+1),c);
}
return c;
}
Output
Enter any number to know its factorial 5
Factorial of 5 is 120
7. a. What is difference between structure and union? Explain the use of typedef statement.
(-07)
Ans. . Structure is the collection of different data types. It allocates memory for the highest
data items. We can access more than one element at a time of all items. We can access
the elements of structure by using dot (.) operator in case of normal variable or by
using (→) arrow operator in case of pointer.
Unions are concept borrowed from structures and therefore follow the same syntax as
structures. However, there is a major distinction between them in terms of storage. In
structures, each member has its own storage location; where as all the members of a
union uses the same location. This implies that, although a union may contain many
members of different types, it can handle only one member at a time. Like structure, a
union can be declared using the keyword union as follows:
union item
{
int m;
float x;
char c;
}code;
This declare a variable code of type union item
Typedef statement is only used for making user define keywords. For example,
main()
{
typedef int amit;
amit num;
printf("enter a number ");
scanf("%d",num);
printf("your entered number is %d",num);
return 0;
}
Here, amit become an integer keyword.
b. What is the pointer? Explain the importance of pointer.
Ans. A pointer is a derive data type in C. it is built from one of the fundamental data types
available in C. Pointer contains memory address contains the instruction and data as a
record.
Pointer is used to access and manipulate data stored in the memory. It is also
known as referential variable.
The importance of pointer is given bellow:-
1. It is more efficient to handling data stored in array or table.
2. Pointer can be used to turn multiple values from a function via function arguments.
3. Pointer permits references to functions and there by facilitating passing from function
as arguments to other functions.
4. Pointer allows dynamic memory management.
5. It provides an efficient to manage structures, link list, stacks and binary trees.
6. It reduces length and complexity of program.
7. It increases the execution speed and thus deduces the program execution time.
8. a. Explain the do-while structure with flow chart. (-07,-06)
Ans. With the help of do-while statement we can execute the body of the loop before the
test is performed.
On reaching the do statement, the program proceeds to evaluate the body of the
loop first. At the end of the loop, the test-condition in the while statement is evaluated.
If the condition is true the program continues to evaluate the body of the loop once
again. This process continues as long as the condition is true. When the condition
becomes false, the loop will be terminated and the control goes to the statement that
appears immediately after the while statement.
true
false
b. Write a program to get the sum of digits of a given number using while loop.(-07,-06)
Ans. //program to get the sum of digits of a given number using while loop
void main()
{
int num,sum=0,a,b;
clrscr();
printf("Enter any number to get the sum of its digits ");
scanf("%d",&num);
a=num;
while(a>0)
{
b=a%10;
sum=sum+b;
a=a/10;
}
printf("\nThe sum of digits of %d is %d",num,sum);
getch();
}
Output
Enter any number to get the sum of its digits 1234
The sum of digits of 1234 is 10
9. a. Differentiate between getch(), getche(), and getchar(). (-07)
Ans. The two functions getch() and getche() are very similar, as they respond without
pressing the Enter key. The difference is that with the function getche() the echo of the
pressed key is displayed on the screen (the letter “e” stands for “echo”), but with the
function getch(), there is no echoing. The program shown in the figure 1 demonstrates
start
initialise
Body of loop
increment
test
stop
the use of both functions. Similarly, the program shown in the figure 3 demonstrate the
difference between getche(), getch(), and getchar() function.
getchar() function reads one character from the keyboard after the new line
character is received, when we press Enter key. The program shown in figure 2
demonstrates the use of the function getchar(). It begins with declaring an integer
variable “ascii”, then the function itself is assigned to this variable.
This means that the character received by the function will be contained in the
variable “ascii”. The first printf() function displays the contents of the variable “ascii”
in the character format (%c) preceded by the string “the character”. The second printf()
function displays the same variable in the decimal format (%d) preceded by the string
corresponds to the ASCII.
Figure 1
//demonstration of getche() and getch() function
main()
{
int option;
printf("\nmake a choice and press a number : ");
option=getche();
printf("\nmake a choice and press a number : ");
option=getch();
}
Figure 2
//demonstration of getchar() function
main()
{
int ascii;
printf("\nType a character and press ENTER : ");
ascii=getchar();
printf("The character %c ",ascii);
printf("Corresponding to the ASCII %d ",ascii);
}
Figure 3
//demonstration of getch(), getche() and getchar() function
main()
{
char ch;
printf("Press any key to continue ");
getch();
printf("\nType any character ");
ch=getche();
printf("\nType any character ");
getchar();
}
10. Write notes on any of the three:
b. Array
Ans. C supports a derived data type known as array that can be used for a powerful data
type that would facilitate efficient storing, accessing and manipulation of data items.
An array is a fixed size sequenced collection of elements of the same data type.
c. Break
Ans. We often come across situation where we want to jump out of a loop instantly, without
waiting to get back to the conditional test. The keyword break allows us to do this. When
break is encountered inside any loop, control automatically passes to the first statement
after the loop. A break is usually associated with an if.
d. Continue
Ans. In some programming situation, we want to take the control to the beginning of the
loop, by passing the statements inside the loop, which has not yet been executed. The
keyword continue allows us to do this. When continue is encountered inside any loop,
control automatically passes to the beginning of the loop. A continue is usually associated
with an if.
e. scanf()
Ans. scanf() is a function to receive the value from the keyboard by the user. The
ampersand (&) before the variables in the scanf() function is a must, & is an „Address of‟
operator. It gives the location number used by the variable in memory. A blank, a tab or a
new line must separate the value supplied to scanf().
11. a. Write a program to find whether a given number is odd or even using bitwise AND
operator.
Ans. //program to find whether a number is odd or even by using bitwise AND operator
void main()
{
int a=1,num;
clrscr();
printf("Enter any number ");
scanf("%d",num);
if((num & a)==0)
printf("Number is even");
else
printf("Number is odd");
getch();
}
Output
Enter any number 25
Number is odd
b. Write a program to find the prime numbers in between 1 to 200.
Ans. //program to find the prime numbers in between 1 to 200
main()
{
int i,j;
clrscr();
printf("Prime numbers in between 1 to 200 is given bellow \n");
i=2;
while(i<=200)
{
j=2;
while(j<=i)
{
if (i%j==0)
break;
j++;
}
if (i==j)
{
printf("%d\t", i);
}
i++;
}
getch();
return 0;
}
Output
Prime numbers in between 1 to 200 is given bellow
2 3 5 7 11 13 17 19 23 29 31 37 41
43 47 53 59 61 67 71 73 79 83 89 97 101
103 107 109 113 127 131 137 139 149 151 157 163 167
173 179 181 191 193 197 199
8.a. What is the difference between static and auto variables? (-06)
Ans.
Static variable Auto variable
We have to specify the storage class to
make a variable static.
It is the default storage class.
If it is not assigned any value then it will
give 0 as out put.
If it is not assigned any value then it will
give garbage value as output.
It is visible to the block in which it is
declared and also in the function where it
It is visible to the block in which the variable
is declared.
will passed.
It retains its value between different function
calls. It holds its last value.
It retains its value till the control remains in
the block in which the variable is declared.
Static variable should be compile by
compiler first.
Auto variable will compile by the compiler
after the static variable.
b. Write a program to calculate the sum of first fifty prime numbers.
Ans. //program to calculate the sum of first fifty prime numbers
main()
{
int i,j,p=0;
float sum;
clrscr();
printf("The sum of first fifty prime numbers is given bellow \n");
i=2;
while(i<=300)
{
if(p<50)
{
j=2;
while(j<=i)
{
if (i%j==0)
break;
j++;
}
if (i==j)
{
sum=sum+i;
printf("%d\t", i);
p++;
}
i++;
}
else
break;
}
printf("\nSum of first fifty prime numbers is %g",sum);
getch();
return 0;
}
Output
The sum of first fifty prime numbers is given bellow
2 3 5 7 11 13 17 19 23 29 31 37 41
43 47 53 59 61 67 71 73 79 83 89 97 101
103 107 109 113 127 131 137 139 149 151 157 163 167
173 179 181 191 193 197 199 211 223 227 229
Sum of first fifty prime numbers is 5117
10. Write a program to accept a line of text and count the number of vowels and consonant
in it.
Ans. //program to accept a line of text and count the number of vowels and consonant in it
main()
{
char a[100];
int i,v=0,c=0;
clrscr();
printf("Enter a sentence \n");
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]=='a'||a[i]=='A'||a[i]=='e'||a[i]=='E'||a[i]=='I'||a[i]=='i'||a[i]=='O'||a[i]=='o'||a[i]=='U'|
|a[i]=='u')
c++;
else if(a[i]=='\t',a[i]==' ')
continue;
else
v++;
}
printf("%d vowels and %d consonants are present in the sentence",c,v);
getch();
return 0;
}
Output
Enter a sentence
We are the students of BCA
8 vowels and 13 consonants are present in the sentence
11. a. What do you mean by modular programming? Write down the advantages of modular
programming.
Ans. Division of longer program into number of smaller subprograms is called modular
programming.
7. a. Explain the different types of control statement with flow chart and syntax. (-05)
Ans. C language has four types of control statement described below:
1. if statement
The if statement is a powerful decision making statement and is used to control the
flow of execution of statements. It is basically a two way decision statement and is
used in conjunction with an expression. It takes the following form: if (test condition)
The if statement may be implemented in different forms depending on the
complexity of conditions to be tested. The different forms are:
a. Simple if statement Entry
The general form of simple if statement is
If (test expression)
{
Statement block;
}
Statement x;
The „statement block‟ may be a single statement or a group of statements. If the test
expression is true, the statement-block will be executed; otherwise the statement block
will be skipped and the execution will jump to the statement-x.
b. if…else statement
The if…else statement is an extension of the simple if statement. The general form
is
if (test expression)
{
True block statement(s)
}
else
{
False block statement(s)
}
Statement-x
If the test expression is true, then the true-block statement(s), immediately
following the if statements are executed; otherwise, the false-block statement(s) are
executed. In either case, either true-block or false-block will be executed, not both.
c. nested if…else statement
When a series of decisions are involved, we may have to use more than one
if…else statement in nested form as shown bellow:
if (test condition-1)
{
if (test condition-2)
{
Statement-1;
}
else
Test expression?
Statement block
Statement-x Next statement
{
Statement-2;
}
}
else
{
Statement-3;
}
Statement-x;
The logic of execution is illustrated. If the condition-1 is false, the statement-3
will be executed; otherwise it continues to perform the second test. If the condition-2 is
true, the statement-1 will be evaluated; otherwise the statement-2 will be evaluated and
then the control is transferred to the statement-x.
d. else if ladder
There is another way of putting ifs together when multipath decisions are
involved. A multipath decision is a chain of ifs in which the statement associated with
each else is an if. It takes the following general form:
if (condition 1)
statement 1;
else if (condition 2)
statement 2;
else if (condition 3)
statement 3;
else
default-statement;
statement – x;
This construct is known as the else if ladder. The conditions are evaluated from
the top (of the ladder), downwards. As soon as a true condition is found, the statement
is associated with it is executed and the control is transferred to the statement – x
(skipping the rest of the ladder). When all the n conditions become false, then the
final else containing the default statement will be executed.
2. The switch statement
C has a built in multiway decision statement known as a switch. The switch
statement teststhe value of a given expression against a list of case values and when a
match is found, a block of statements associated with that case is executed. The
general form of switch statement is as shown bellow:
switch (exepression)
{
case value-1;
block 1
break;
case value-2;
block-2
break;
……
…....
default:
default-block
break;
}
statement - x;
The expression is an integer expression or characters, Value-1, Value-2…..are
constants and constant expressions and are known as case labels. Each of these
values is unique within a switch statement. block-1,block-2….are statement lists and
may contain zero or more statements. There is no need to put braces around these
blocks. Note that case labels end with a colon (:).
The break statement at the end of each block signals the end of a particular case
and cause an exit from the switch statement, transferring the control to the statement-
x following the switch.
The default is an optional case. When present, it will be executed if the value of
the expression does not match with any of the case values. If not present, no action
takes place if all matches fail and the control goes to the statement-x.
3. Conditional operator statement
Conditional operator is a combination of ? and :, and takes three operands. It is
useful for making two-way decisions. The general form of use of the conditional
operator is as follows:
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. The Goto statement
C supports the goto statement to branch unconditionally from one point to another
in the program. The goto requires a label in order to identify the place where where
the branch is to be made. A label is any valid variable name, and must be followed by
a colon. The label is placed immediately before the statement where the control is to
be transferred. The general form of goto and label statements are shown below:
goto labe; label:
……… statement;
……… ………….
……… ………….
label: ………….
Statement; goto label;
The label: can be anywhere in the program either before or after the goto label;
statement
b. Write a program to multiply the two given 2 x 2 matrixes. (-05)
Ans. //program to multiply two matrix and print its result
void main()
{
int a[2][2],b[2][2],c[2][2],i,j,k;
clrscr();
printf("\nEnter the elements for matrix A ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}}
printf("\nEnter the elements for matrix B ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}}
printf("\nThe multiplication result of the above matrices is given below\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=0;
for(k=0;k<2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
getch();
}
Output
Enter the elements for matrix A
1 2
3 4
Enter the elements for matrix B
5 4
6 7
The multiplication result of the above matrices is given below
17 18
39 40
8. a. What is function argument?(-05)
Ans. Function arguments are given inside the parenthesis, preceded by their types and
separated by commas. If a function does not use any argument, the word void is used
inside the parenthesis. For example:
int getchar(void);
double pow(double x,double y);
The first function int getchar(void); return an integer value and does not take any
arguments.
The second function double pow(double x,double y); takes two double arguments
and return a value of the type double.
9. b. What is the difference between while statement & do-while statement? (-05)
Ans. The while is an entry controlled loop statement. The test condition is evaluated and if
the condition is true, then the body of the loop is executed. After execution of the body,
the test condition is once again evaluated and if it is true, the body is executed once
again. The process of repeated execution of the body continues until the test condition
finally becomes false and the control is transferred out of the loop. On exit, the
program continues with the statement immediately after the body of the loop.
With the help of do-while statement we can execute the body of the loop before
the test is performed.
On reaching the do statement, the program proceeds to evaluate the body of the
loop first. At the end of the loop, the test-condition in the while statement is evaluated.
If the condition is true the program continues to evaluate the body of the loop once
again. This process continues as long as the condition is true. When the condition
becomes false, the loop will be terminated and the control goes to the statement that
appears immediately after the while statement.
7. b. Explain the different types of constant in C. (-04)
Ans. Constants in C refer to fixed values that do not change during the execution of a
program. C supports several types of constants as illustrated bellow:
Integer constants
An integer constant refers to a sequence of digits. There are three types of integers,
namely decimal integer, octal integer, and hexadecimal integer.
Decimal integer consist of digits, 0 through 9, preceded by an optional – or + sign.
123 -324 0 +78 are the examples of valid decimal integer where as 15 750
20,000 $10000 are the examples of illegal numbers.
An octal integer constant consists of any combination of digits from the set 0
through 7, with a leading 0. 037 0472 0551 are the examples of octal integer.
A sequence of digits preceded by 0x or 0Xis considered as hexadecimal integer.
They may also include alphabets A through F or a through f. the letter A through F
represents the numbers 10 through 15. 0X2 0x93f 0Xbcd are the examples of valid
hex integers.
Real constants
Integer numbers are inadequate to represent quantities that vary continuously, such
as distance, heights, prices and so on. These quantities are represented by numbers
containing fractional part like 17.548. Such numbers are called real constants.
Single character constants
A single character constant contains a single character enclosed within a pair of
single quote marks. Example of character constants are: „5‟ „r‟ „D‟ „;‟
String constants
A string constant is a sequence of characters enclosed in double quotes. The
characters may be letters, numbers, special characters and blank space. Examples are:
“Hello” “1989” “WELL DONE” “?...!” “5+3” “X”
8. a. What are escape sequences? Explain any three escape sequences with example.(-04)
Ans. An escape sequence provides special formatting control. An escape sequence consists
of a backslash followed by a single character. Some character such as line feed, form
feed, vertical tab, alert etc. cannot be typed through the keyboard. Such invisible
characters can be made understood to the „C‟ compiler through the use of execution
characters or escape sequences. The term execution characters and escape sequences
are used interchangeably. Escape sequence begins with \ sign.
The three escape sequences are describe below:
\n takes the cursor to the beginning of the next line; \t takes the cursor to the next
horizontal tab stop; \v takes the cursor to the next vertical tab stop;
b. Write a program to add the two given 3 x 3 matrices.(-04)
Ans. //program to add the two matrices
void main()
Constants
Integer constants Real constants Single character constants
String constants
{
int i,j,a[3][3],b[3][3],c[3][3];
clrscr();
printf("Enter the elements of 3*3 A matrix ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of 3*3 B matrix ");
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("The result after adding the above mentioned matrixes is given bellow\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
getch();
}
Output
Enter the elements of 3*3 A matrix
1 2 3
4 5 6
7 8 9
Enter the elements of 3*3 B matrix
3 2 1
6 5 4
9 8 7
The result after adding the above mentioned matrixes is given bellow
4 4 4
10 10 10
16 16 16
10.a. What are pointers? Distinguish between the address stored in the pointer and the value
at that address.(-04)
Ans. A pointer is a derive data type in C. it is built from one of the fundamental data types
available in C. Pointer contains memory address contains the instruction and data as a
record.
Pointer is used to access and manipulate data stored in the memory. It is also known
as referential variable.
All the variables defined in a program stored at specific memory location. If we
want to get the address of the variable then we use “&” before the variable name. For
example: int x=2;
The above declaration tells the C compiler to perform following actions:
1. Reserve space in the main memory to hold an integer value.
2. Associate the name x with this memory location.
3. Store the value 2 at this location.
x → location name
2 → value at the location
6485 → location address
To get the address of an integer variable, it has to be used with unary operator „&‟
preceding the variable name. The memory address for the variable x holding the value 2
is 6485.
b. Explain the relation between an array and a pointer.(-04)
Ans. Array and pointers are the two parts of C language. Array stores all the elements on the
continuous location where as pointers stores the address of a variable. We can access
elements of an array one by one by using its index number. When we store the address
of an array in a pointer only the first element address should be stored. That is called
the basic address because a pointer always stored the basic address. If we got the basic
address of an array then if we increment the pointer by one then it find the next element
of an array. A pointer size can always be 2 byte because it stores address.
main()
{
int a[10],*p,I;
*p=a[0];
printf("Enter the elements of an array ");
for(i=0;i<10;i++)
scanf("%d",p+i);
for(i=0;i<10;i++)
printf("%d",*(p+i));
getch();
}
11. Explain any four of the following: (-04)
a) Fseek ()
This function is related to the file handling of „C‟. This function moves the file
pointer to a specific position in a file. The general syntax of fseek() function is as
following:-
fseek(fptr,offset,mode) where fptr indicates the file pointer of a file, offset indicates
how many position moves forward or backward the file pointer which depends on
mode and mode indicates from where file pointer should move. If mode = 0 it means
from the beginning of the file, if mode = 1 it means from the current position, or if
mode = 2 it means from the end of the file.
b) Stdio.h
Stdio.h is one of the header files provided in library of „C‟ language. It stands for
Standard input output header file. It contains various input output function like scanf,
printf etc. For using input output function we need to include stdio.h at the beginning
of the program.
c) Fread ()
This function is related to file handling of „C‟. It is used to read a block of data from
a given file. It is also used to read one or more structure from a given file to a memory
location.
The general syntax of fread function is:
Fread(ptr,size,nitems,fptr);
Where ptr is a pointer to the location to receive the structure, size is the size in bytes
of each structure to be read, nitems is the number of the structures to be read and fptr is
the pointer to the file to be read.
e) Putchar ()
putchar() is a character handling function supported by C to output the values of
character variables. The function putchar requires one parameter. We can use this
function repeatedly to output a string of characters stored in an array using a loop. For
example:
char name[6] = "PARIS"
for(i=0;i<5;i++)
putchar(name[i]);
putchar("\n");
12.b. Write a program to generate table of number using while loop.
Ans. //program to generate table of any number entered through the key board using while
loop
void main()
{
int a,b,c;
clrscr();
printf("Enter any number for its multiplication table ");
scanf("%d",&a);
b=1;
while(b<=10)
{
c=a*b;
printf("\n%d\t*\t%d\t=\t%d",a,b,c);
b++;
}}
Output
Enter any number for its multiplication table 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
c. Write a program to print the sum of all odd number between 1 to 10 using do while
loop.
Ans. //program to print the sum of all odd numbers between 1 to 10 using do while loop
void main()
{
int i=1,sum=0;
clrscr();
do
{
sum=sum+i;
i+=2;
}while(i<=10);
printf("the sum of all odd numbers between 1 to 10 is %d",sum);
getch();
}
Output
the sum of all odd numbers between 1 to 10 is 25
d. Write a program to reverse the digits of a number using for loop.
Ans. //program to reverse the digits of a number using for loop
void main()
{
int num,rev=0,a,b,i;
clrscr();
printf("Enter any number ");
scanf("%d",&num);
for(a=num;a>0;a/=10)
{
b=a%10;
rev=rev*10+b;
}
printf("\n%d is the reverse of %d",rev,num);
getch();
}
Output
Enter any number 1234
4321 is the reverse of 1234