0% found this document useful (0 votes)
821 views41 pages

PPS-Important Questions With Answers-3!10!18

The document provides information about the functional units of a computer system and C programming concepts. It discusses the five major operations of a computer system as input, storage, processing, output, and control. It also defines key C programming concepts such as algorithms, flowcharts, C program structure, tokens, data types, and more. For example, it states that an algorithm must have well-defined inputs, outputs, steps, and finish in a finite time. It provides examples to illustrate computer system units and C programming topics.

Uploaded by

Vaishnav Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
821 views41 pages

PPS-Important Questions With Answers-3!10!18

The document provides information about the functional units of a computer system and C programming concepts. It discusses the five major operations of a computer system as input, storage, processing, output, and control. It also defines key C programming concepts such as algorithms, flowcharts, C program structure, tokens, data types, and more. For example, it states that an algorithm must have well-defined inputs, outputs, steps, and finish in a finite time. It provides examples to illustrate computer system units and C programming topics.

Uploaded by

Vaishnav Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

1|Page

PPS- Questions & Answers


UNIT-1

1. Explain the various functional units of a computer system?


Ans:

Block diagram of Computer

The computer performs basically five major operations of functions irrespective of their size
and make. These are
1) it accepts data or instruction by way of input
2) it stores the data
3) it can process data as required by the user
4) it gives results in the form of output
5) it controls all operations inside a computer.

1. Input: This is the process of entering data and programs into the computer system.
2. Central Processing Unit (CPU): The ALU and the CU of a computer system are jointly known
as the central processing unit (CPU).
3. Control Unit (CU): The process of input, output, processing and storage is performed under
the supervision of a unit called 'Control Unit'. It decides when to start receiving data, when to
stop it, where to store the data etc. It takes care of step-by-step processing of all operations
inside the computer.
4. Arithmetic Logic Unit (ALU): The major operations performed by the ALU are addition,
subtraction, multiplication, division, logic and comparison etc.
5. Memory Unit: Memory unit is used to store data and instructions.
6. Output: This is the process of producing results from the data for getting useful information.
2|Page

2. What is an algorithm? Explain its properties with an example?


Ans:
Algorithm:
An algorithm is a step by step process to solve a particular problem.
Properties of an Algorithm:
Every algorithm should have the following 5 properties.
1. Input: Algorithm may take one or more inputs.
2. Output: Algorithm should produce atleast one output.
3. Finiteness: Algorithm should be end after a fixed number of steps. It should not enter
into an infinite loop.
4. Definiteness: Each instruction of an algorithm must be clear and well defined. There
should not be any error.
5. Effectiveness: The operations must be simple and must be completed in a finite time.
Example: Write an algorithm to find the largest of three numbers.
Step1: Start
Step2: Declare the variables A, B, C
Step3: Read the values of variables A, B, C
Step4: check if (A>B) and if (A>C) is true then print ‘A is the largest number’
Step5. else print ‘C is the largest number’
Step6: else check if (B>C) is true then print ‘B is the largest number’
Step7. else print ‘C is the largest number’
Step8: Stop.

3. What is a flowchart? Explain the symbols used in a flowchart with an example?


Ans:
Flowchart:
A graphical/pictorial representation of an algorithm is called flowchart.
Based on the algorithm we have to construct the flow chart. The following
symbols are used to construct a flow chart.
3|Page

Example: Draw a flowchart to find the largest number among 3 numbers.

4. Explain the structure of a C program?


Ans:
Structure of a C program:

Documentation section
Link section
Definition section
Global declaration section
main()
{
Declaration part;
Executable part;
}
subprogram section/user defined section
{
function1;
function2;
.
.
function n;
4|Page

1. Documentation section: It is a set of comment lines that gives the complete


information about the program.
Example: /* Write a c program to find the simple interest*/

2. Link section: It provides instructions to the compiler to link the functions from the
system library i.e., header files.
Examples:
#include<stdio.h>
# is the pre-processor directive command sign.
include is a pre-processor directive command.
stdio means a standard input & output.
.h means extension of header file.
#include<conio.h>
conio is a console input & output

3. Definition section: It defines all the symbolic constants.


Example: #define PI 3.14

4.Global declaration section: There are some variables used more than one function
such variables are called global variables. These variables are declared before the main
( ) function section.

5.main( )section: Every C program should contain one main() function and the C
program execution start with main( ). This section contains two parts.
1. Declaration part: This part declares all the variables which are used in the executable
part.
Example:
int a, b, c;
float x, y, z;
char a, b, c;
2. Executable part: This part contains a single statement or a group of statements.
All the statements in the declaration and executable parts should be end with semicolon
(;).
6. subprogram section/user defined section: It is also called user defined function
section i.e. it is developed by the user at the time writing a C program. All the user defined
functions are placed before or after the main() function section.

5. Define C tokens and explain in detail?


Ans:
C Tokens:
The basic or smallest units in C program are called C Tokens. They are 6
types of tokens in C language. C programs are written using these tokens
and syntax of the language.
1. Keywords/Reserved words
2. Identifiers
3. Constants/Literals
4. Strings
5. Special symbols
6. Operators
5|Page

1. Keywords: Keywords are fixed and predefined meaning in C


language.There are 32-Keywords.

Rules for declaring keywords:-


1. Keywords must be written in the lower case letters.
2. Keywords should not be declared as variables or identifiers.

2. Identifiers:Identifiers are names given to the variables,functions and arrays defined


by the user.Identifier is a sequence of letters,digits and one special character i.e., _
(Underscore).
Ex:- a, b, abc , abc _123,a123,_xyz etc.
Rules for declaring Identifiers:-
1. Identifiers must be start with a letter or _ (Underscore).
2. Identifiers should not start with a digit.
3. No special symbols other than _ (Underscore) are allowed within
the identifiers.
4. Identifiers are case sensitive i.e., the upper case and lower case
letters are different.
5. Identifiers should not be declared as keywords.
6. Identifiers can be a length of first 31 characters.

3.Constant/Literals:-
Constants are the fixed values that do not change during the execution of a
program.
Constant are divided into two types.
1. Numeric constants
2. Non-Numeric /Character constants
1.Numeric Constants: There are two types of Numeric constants
1. Integer constants
2. Real or floating point constants

1.Integer constants:- It is a sequence of digits without decimal points.


Ex: - 10 ,20 ,30 etc.

Rules for declaring Integer constants:-


1. It must contain at least one digit.
2. It should not contain any decimal point.
3. It could be either positive integer or negative integer constant.
4. If no sign precedes an integer constant, it is assumed to be positive.
6|Page

2.Real/Floating point constants: It is a sequence of digits with decimal


points.
Ex:- 10.32 , 2.02 etc.

Rules for declaring Real constants:-


1. It must contain at least one digit.
2. It should contain decimal point.
3. It could be either positive or negative real constant.
4. If no sign precedes a real constant, it is assumed to be positive.
2.Non-Numeric Constants/Character constant:- There are two
types of character constants
1. Single character constants
2. String character constants
1.Single character constants:- Any single letter or digit enclosed within single
quotation marks is called single character constant.
Ex:- ‘A’,’5’
2.String character constant:- String is a group of characters enclosed
within the double quotation marks is called String character constants.

Ex:- “MRCET”, “CSEA”


5.Special symbol:- { } , ( ) , [ ] , $ , # , @ are the special symbols.
6.Operators:- Operator is a symbol which specifies an operation to be
performed on the operands.
Ex:-Arithmetic operators, Logical operators, Relational operators, Increment and
Decrement operators etc.

6. Explain all primary/fundamental data types in C?


Ans:
Data types:
1. Data type indicates what kind of data can be stored in the variables
and identifiers.
2. It can be used for storage representations.
3. Data types are fixed and predefined meaning in C language.
4. Data types can be divided into four types: -
i) Primary / fundamental data types
Ex: - int,short int,long int,float,double,long double,char.
ii) Derived data types
Ex: - arrays,pointers,functions,strings, structures,unions.
iii) User defined data types
Ex: - typedef,enum.
iv) Empty data type
Ex: - void.
I.Primary/Fundamental data types: -
There are three types:-
1. Integer Data types
2. Character Data types
3. Real/Floating point Data types
1.Integer Data type: -
It can be used to represent/store integer constants.
There are 3 types.
1.int
2.short int
3.long int
7|Page

1.int: -
1. It is indicated by the keyword ‘int’.
2. The control string of ‘int’ is %d.
3. It occupies of 2 bytes of memory location i.e., 16bits.
4. The range of int is -32768 to 32767.
Ex: - int x,y;
scanf (“%d%d”,&x,&y);
2.short int: -
1. It is indicated by the keyword ‘short’.
2. When the given value is less than the int range then short int is used.
3. The control string of short int is %d.
4. It occupies 1byte of memory location i.e., 8bits.
5. The range of short int is -128 to 127.

Ex: - short int x,y; or short x,y;


scanf(“%d%d”,&x,&y);
3.long int: -
1.It is indicated by the keyword ‘long’.
2.When the given value is greater than the ‘int’ range then long int
is used
3.The control string of long int is “%ld”
4.It occupies 4bytes of memory location i.e., 32bits.
5.The range of long int is -2,147,483,648 to 2,147,483,647.
Ex: - long int x,y; or long x,y;
scanf(“%ld%ld”,&x,&y);
2.Character Data types: -
It can be used to represent/store either single character constants or string
character constants.
1.It is indicated by the keyword ‘char’.
2The control string char is ‘%c’ for single character constants and
‘%s’ for string character constants.
3.It occupies 1byte of memory location i.e., 8bits.
4.The range of char is -128 to 127.
Ex1: char x,y;
scanf(“%c%c”,&x,&y);
Ex2: char name, college;
scanf(“%s%s”,&name,&college);
3.Real/Floating point data types: -
It can be used to represent/store Real/Floating point constants.
There are 3 types.
1.float
2.double
3.long double
1.float: -
1) It is indicated by the keyword ‘float’.
2) The control string of float is “%f”.
3) It occupies 4bytes of memory location. i.e., 32bits.
4) The range of float is 3.4E-38 to 3.4E+38.

Ex: - float a,b;


scanf(“%f%f”,&a,&b);
2.double: -
1) It is indicated by the keyword ‘double’.
8|Page

2) When the given value is greater than the float range then double is
used.
3) The control string of double is “%lf”.
4) It occupies 8bytes of memory location i.e., 64bits.
5) The range of double is 1.7E-308 to 1.7E+308.
Ex: - double a,b;
scanf(“%lf%lf”,&a,&b);
3.long double: -
1) When the given value is greater than the double range then long
double is used.
2) The control string of long double is “%Lf”.
3) It occupies 10 bytes of the memory location i.e., 80bits.
4) The range of long double is 3.4E-4932 to 1.1E+4932.
Ex: long double a,b;
scanf(“%Lf%Lf”,&a,&b);

7. Explain different types of operators in C?


Ans:
Operators:-
Operator is a symbol which specifies an operation to be performed
between the operands.
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. sizeof() operator
5. Assignment operators
6. Conditional/Ternary operator
7. Increment and decrement operators
8. Bitwise operators
9. Special operators(Comma operator(,),arrow operator(->),address
operator(&),dot operator(.),Value at address operator(*))
1.Arithmetic operator:-
1. Arithmetic operators are used to perform basic arithmetic
operations between the operands.
2. There are 5 types of Arithmetic operators in C language they are +,
-,* ,/ ,%.
3. Division operator(/) gives the result in quotient.
4. Modulus operator(%) gives the result in remainder.
5. % operator cannot be worked with the real constants.
2.Relational operators:-
1. Relational operators are used to compare the relation between the
operands.
2. There are 6 types of relational operators in C language they are <,
>, <=, >=,! = , ==.
3. Relational operator gives the result either in 1 (true) or 0 (false).

3.Logical operators:-
1. Logical operators are used to combine two or more relational
conditions.
2. Logical operators gives the result either in 1 (true) or 0 (false).
3. There are 3 types of Logical operators in c language
1. Logical AND (&&).
2. Logical OR (||).
9|Page

3. Logical NOT (!).


4.sizeof operator:- This operator can be used to find the number of bytes
occupied by a variable or data type.
Syntax:- sizeof(operand);

5.Assignment operator:-Values can be assigned to a variables using


assignment operator in c language. The symbol of assignment operator is
‘=’.
Assignment operator supports 5 short hand assignment operators in C
language they are += , - = , * = , /= , %=
6.Conditional operator/Ternary opertor:-
In C language ‘?’ and ‘:’ are called conditional operator or ternary operators
in C language.
Syntax:- Exp1?Exp2:Exp3;
Here first Exp1 is evaluated, if Exp1 is true then Exp2 is executed otherwise
Exp3 is executed.
7.Increment and Decrement operator:-
1. These operators are used to increment or decrement the value of
the variable by 1.
2. ‘ ++ ’ is the increment operator in C language.
3. ‘-- ’ is the decrement operator in C language.
4.There are 4 types of operators in C language.
1. Pre-increment operator
2. Post-increment operator
3. Pre-decrement operator
4. Post-decrement operator
1.Pre-increment operator:
1. ++ operator is placed before the operand is called pre-increment
operator. Ex: ++x,++a
2. Pre-increment operator specifies first increment the value of the
variable by 1 and then assigns the incremented value to other
variable.
2.Post-increment operator:
1. ++ operator is placed after the operand is called post-increment
operator. Ex: x++,a++
2. Post-increment operator specifies first assigns the value of the
variable to other variable and then increments the value of the
variable by1.
3.Pre-decrement operator:
1. -- operator is placed before the operand is called pre-decrement
operator. Ex:--x,--a
2. Pre-decrement operator specifies first decrement the value of the
variable by 1 and then assigns the decremented value to other
variable.
4.Post-decrement operator:
1. -- operator is placed after the operand is called post-decrement
operator. Ex:x--,a--
2. Post-decrement operator specifies first assigns the value of the
variable to other variable and then decrements the value of the
variable by1.
8.Bitwise operators:The data stored in a computer memory as a sequence
of 0’s and 1’s.There are some operators works with 0’s and 1’s are called
bitwise operators. Bitwise operators cannot be worked with real constants.
10 | P a g e

There are 6 types of operators in C language.


1. Bitwise AND (&)
2. Bitwise OR (|)
3. Bitwise X-OR (^)
4. Bitwise left shift (<<)
5. Bitwise right shift (>>)
6. Bitwise 1's complement ( ~)
1.Bitwise AND (&):The result of the bitwise AND is 1 when both the bits
are 1 otherwise 0.
2.Bitwise OR (|):The result of the bitwise OR is 0 when both the bits are 0
otherwise 1.
3.Bitwise X-OR (^):The result of the bitwise X-OR is 1 when one bit is 0
and other bit is 1 otherwise 0.
4.Bitwise left shift (<<): This operator can be used to shift the bit
positions to the left by ‘n’ positions.
5.Bitwise right shift (>>):This operator can be used to shift the bit
positions to the right by ‘n’ positions.
6.Bitwise 1's complement (~): This operator can be used to reverse
the bit i.e., it changes from 0 to 1 and 1 to 0.
9.Comma operator(,):
The comma operator is used to separate the variables list and it is mostly
used in for statement.
Ex: sum=(x=10,y=20,x+y);
The calculated value is assigned to sum.

UNIT-2

1. Explain conditional statements/decision making/selection statements in C?


Ans:
Conditional/Decision making statements.
There are 4 types of Conditional/Decision making statements in C language.
They are
1. if
2. if else
3. nested if else
4. switch statement

1.if statement: It can be used to execute a single statement or a group of statements based
on the condition. It is a one way branching statement in C language.
Syntax:
if (condition)
{
Statements;
}
next statement;
Operation: First the condition is checked if it is true then the statements will be executed
and then control is transferred to the next statement in the program.
if the condition is false, control skips the statements and then control is transferred to the
next statement in the program.

2.if else statement: It can be used to execute a single statement or a group of statements
based on the condition. It is a two way branching statement in C language.
Syntax:
11 | P a g e

if (condition)
{
statement1;
}
else
{
statement2;
}
next statement;
Operation: First the condition is checked if the condition is true then statement1 will be
executed and it skips statement2 and then control is transferred to the next statement in
the program.
if the condition is false then statement1 is skipped and statement2 will be executed and
then control is transferred to the next statement in the program.

3.nested if else statement: If we want to check more than one condition then nested if else
is used. It is multi way branching statement in C language.
Syntax:
if (condition1)
{
if (condition2)
{
statement1;
}
else
{
statement2;
}
}
else
{
statement3;
}
next statement;
Operation: First the condition1 is checked if it is false then statement3 will be executed
and then control is transferred to the next statement in the program.
If the condition1 is true then condition2 is checked, if it is true then statement1 will be
executed and then control is transferred to the next statement in the program.
If the condition2 is false then statement2 will be executed and then control is transferred
to the next statement in the program.

4.switch statement: If we want to select one statement from more number of statements
then switch statement is used. It is a multi way branching statement in C language.
Syntax:
switch(expression)
{
case value1:block1;
break;
case value2:block2;
break;
--------
--------
case valuen:blockn;
12 | P a g e

break;
default :default block;
break;
}
next statement;
Operation: First the expression value (integer constant/character constant) is compared
with all the case values in the switch. if it is matched with any case value then the
particular case block will be executed and then control is transferred to the next
statement in the program.
If the expression value is not matched with any case value then default block will be
executed and then control is transferred to the next statement in the program.

2. Explain loops/repetition/iterative statements in C?


Ans:
Loops or Repetition statements:-
There are 3 types of Loops/Repetition statements in C language.
They are
1. while
2. do while
3. for

1.while loop statement: It is used when a group of statements are executed repeatedly
until the specified condition is true. It is also called entry controlled loop. The minimum
number of execution takes place in while loop is 0.
Syntax:
while(condition)
{
body of while loop;
}
next statement;
Operation: First the condition is checked if the condition is true then control enters into
the body of while loop to execute the statements repeatedly until the specified condition
is true.
if the condition is false, the body of while loop is skipped and control comes out of the
loop and continues with the next statement in the program.

2. do while loop statement:- It is used when a group of statements are executed


repeatedly until the specified condition is true. It is also called exit controlled loop.
The minimum number of execution takes place in do while loop is 1.
Syntax:-
do
{
body of do while loop;
}
while(condition);
next statement;
In do-while loop condition should be end with semicolon.
Operation: In do-while loop the condition is checked at the end i.e., the control first enters
into the body of do while loop to execute the statements. After the execution of statements
it checks for the condition. if the condition is true then the control again enters into the
body of do while loop to execute the statements repeatedly until the specified condition
is true.
if the condition is false then control continues with the next statement in the program.
13 | P a g e

3.for loop statement:- It is used when a group of statements are executed repeatedly until
the specified condition is true. It is also called entry controlled loop. The minimum
number of execution takes place in for loop is 0.
Syntax:-
for (initialization;condition;increment/decrement )
{
body of for loop;
}
next statement;
Operation: First initial value will be assigned. Next condition is checked if the condition
is true then control enters into the body of for loop to execute the statements. After the
execution of statements the initial value will be incremented/decremented. After initial
value will be incremented/decremented the control again checks for the condition. If the
condition is true then the control is again enters into the body of for loop to execute the
statements repeatedly until the specified condition is true.
if the condition is false, the body of for loop is skipped and control comes out of the loop
and continues with the next statement in the program.

3. Explain unconditional statements/jump statements in C?


Ans:
Unconditional statements:- Normal flow of control can be transferred from one place to
another place in the program. This can be done by using the following unconditional
statements in the C language.
There are 3 types of Unconditional statements in C language.
They are
1. goto
2. break
3. continue
1.goto statement:- goto is an unconditional statement used to transfer the control from
one statement to another statement in the program.
Syntax:-
goto label;
label:
Statements;
2.break statement:- break is an unconditional statement used to terminate the loops or
switch statement.
When it is used in loops(while,do while,for) control comes out of the loop and continues
with the next statement in the program.
When it is used in switch statement to terminate the particular case block and then
control is transferred to the next statement in the program.
Syntax:-
statement;
break;
3.continue statement:- continue is an unconditional statement used to control to be
transferred to the beginning of the loop for the next iteration without executing the
remaining statements in the program.
Syntax:-
statement;
continue;

4. Write a C program to check whether the given number is even or odd?


Ans:
14 | P a g e

#include<stdio.h>
#include<conio.h>
main()
{
int n;
clrscr();
printf("Enter any number\n");
scanf("%d",&n);
if (n%2==0)
printf("The given number is even");
else
printf("The given number is odd");
getch();
}

5. Write a C program to find the roots of a quadratic equation?


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,root1,root2;
clrscr();
printf(" Enter the values of a,b,c\n");
scanf(" %f %f %f ", &a, &b, &c);
d= (b * b) - (4 * a * c);
if(d > =0)
{
root1=-b+sqrt(d)/(2*a);
root2 =-b-sqrt(d)/(2*a);
printf(“root1=%f,root2=%f”,root1,root2);
}
else
printf("Roots are imaginary");
getch();
}

6. Write a C program to find the largest number among 3 numbers?


Ans:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf("Enter the values of a,b,c\n");
scanf("%d%d%d",&a,&b,&c);
if (a>b)
{
if (a>c)
printf("a is largest number");
else
15 | P a g e

printf("c is largest number");


}
else
{
if(b>c)
printf("b is largest number");
else
printf("c is largest number");
}
getch();
}

7. Write a C program to perform all arithmetic operations between 2 operands using


switch statement?
Ans:
#include<stdio.h>
#include<conio.h>
main()
{
int x,y,sum,sub,mul,div,mod,choice;
clrscr();
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Enter your choice 1.Addition\n2.Subtraction\n3.Product\n4.Quotient\n5.Remainder\n”);
scanf("%d",&choice);
switch(choice)
{
case 1:sum=x+y;
printf("The sum of x and y is %d",sum);
break;
case 2:sub=x-y;
printf("The sub of x and y is %d",sub);
break;
case 3:mul=x*y;
printf("The mul of x and y is %d",mul);
break;
case 4:div=x/y;
printf("The div of x and y is %d",div);
break;
case 5:mod=x%y;
printf("The mod of x and y is %d",mod);
break;
default :printf("Invalid Choice");
break;
}
getch();
}

8. Write a C program to find the sum of individual digits of a given number?


Ans:
#include<stdio.h>
16 | P a g e

#include<conio.h>
void main()
{
int digit,n,sum=0;
clrscr();
printf("Enter any number\n");
scanf("%d",&n);
while(n!=0)
{
digit=n%10;
sum=sum+digit;
n=n/10;
}
printf("The sum of individual digits of a given number is %d",sum);
getch();
}

9. Write a C program to find the reverse of a given number?


Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int digit,n,reverse=0;
clrscr();
printf("Enter any number\n");
scanf("%d",&n);
while(n!=0)
{
digit=n%10;
reverse=reverse*10+digit;
n=n/10;
}
printf("The reverse of a given number is %d",reverse);
getch();
}

10. Write a C program to check whether the given number is Armstrong or not?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int digit,n,sum=0,temp;
clrscr();
printf("Enter any number\n");
scanf("%d",&n);
temp=n;
while(n!=0)
{
digit=n%10;
sum=sum+(digit*digit*digit);
n=n/10;
17 | P a g e

}
if(temp==sum)
printf("The given number is Armstrong");
else
printf("The given number is not a Armstrong");
getch();
}

11. Write a C program to generate first n terms of fibonacci sequence?


Ans:
#include <stdio.h>
#include<conio.h>
void main()
{
int i, n,first=0,second=1,nextterm;
clrscr();
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("Fibonacci Series is\n ");
for (i=1;i<=n;i++)
{
printf("%d\t",first);
nextterm=first+second;
first=second;
second=nextterm;
}
getch();
}

12. Write a C program to generate prime numbers from 1 to n?


Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact,j;
clrscr();
printf(‘Enter any number\n”);
scanf(“%d”,&n);
printf(“Prime numbers from 1 to n are\n”);
for(i=1;i<=n;i++)
{
fact=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
fact++;
}
if(fact==2)
printf(“%d\t”,i);
}
18 | P a g e

getch();
}

13. Write a C program to check whether the given number is strong number or not?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact,digit,sum=0,temp;
clrscr();
printf("Enter any number\n");
scanf("%d",&n);
temp=n;
while(n!=0)
{
i=1;
fact=1;
digit=n%10;
while(i<=digit)
{
fact=fact*i;
i++;
}
sum=sum+fact;
n=n/10;
}
if(temp==sum)
printf("The given number is strong number");
else
printf("The given number is not a strong number");
getch();
}

14. Write a C program to check whether the given number is perfect number or not?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1,sum=0;
clrscr();
printf(“Enter any number\n“);
scanf(“%d”,&n);
for(i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(sum==n)
printf(“The given number is a perfect number”);
19 | P a g e

else
printf(“The given number is not a perfect number”);
getch();
}

15. Write a C program to find the factorial of a given number?


Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact=1;
clrscr();
printf("Enter any number\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("The factorial of a given number is %d",fact);
getch();
}

16. Write a C program to print the multiplication table of a given number?


Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("Enter any number\n");
scanf("%d",&n);
printf(“Multiplication table of a given number is\n”);
for(i=1;i<=20;i++)
{
printf(“%d * %d=%d\n”,n,i,n*i);
}
getch();
}

UNIT-3

1. Define a function and explain the different types of functions with an example
program?
Ans:
1. A Function is a sub program/self-contained block of one or more statements that
performs a special task when called.
2. Every C program execution starts with main().
3. main() calls other functions to share the work.
4. The large programs can be divided into small programs using functions.
20 | P a g e

5. Functions increase the readability of the program.


6.How function works: Once a function is defined and called, it takes some data from the
calling function and returns a value back to the calling function. Whenever a function is
called, control passes to the called function and working of the calling function is
temporarily stopped. When the execution of the called function is completed, then control
return back to the calling function and executes the next statement.
Types of functions:
There are two types:
1) Library functions/Standard functions
2) User defined functions
1.Library functions:Library functions are fixed and pre defined meaning in C language.
Ex: printf(),scanf(),sqrt(),pow() etc.
2.User defined functions:These functions are developed by the user at the time of writing
a program.
Eg: main(),MRCET(),add(),factorial() etc.
Declaration of a function:
Syntax:
returntype functionname(datatype argument1,datatype argument2,...datatype argument
n)
{
body of a function;
return(expression);
}
In the above syntax returntype may be any basic data type such as int, float, char etc.
return statement is used to send a value back to the calling function.
Example:
main() //calling function
{
MRCET(A,B,C); //called function
}
MRCET(X,Y,Z)
{
body of a function;
}
Actual arguments/Actual parameters:The arguments of the calling function are called
actual arguments.In the above example A,B,C are actual arguments.
Formal arguments/Formal parameters:The arguments of the called function are called
formal arguments.In the above example X,Y,Z are called formal arguments.
Ex: Write a C program to find the addition of two numbers using functions.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the values of a,b \n");
scanf(" %d%d",&a,&b);
c=add(a,b);
printf("The sum of a,b is %d",c);
getch();
}
int add(int x, int y)
{
21 | P a g e

int p;
p = x+ y;
return (p);
}

2. Explain the categories of functions based on the arguments and return values?
Ans:
Function Categories (Based on Arguments and Return Values)
There are 4 types
1. Functions with arguments and functions with return values.
2. Functions without arguments and functions with no return values.
3. Functions with no arguments and functions with return value.
4. Functions with arguments and functions with no return values.

1. Functions with arguments and functions with return values


Functions with arguments i.e. the called function receive the data from the calling
function.
Function with return values i.e. calling function receives the value from the called
function.
In effect there is a data transfer between calling and called function.

Example program
#include<stdio.h>
#include<conio.h>
int add(int x, int y);
void main()
{
int a,b,c;
clrscr();
printf(“enter the values of a,b\n”);
scanf(“%d%d”,&a&b);
c=add(a,b);
printf(“the sum is %d”,c);
getch();
}
int add(int x , int y)
{
return(x+y);
}

2. Functions with no arguments and functions with no return values.


Functions with no arguments i.e. the called function does not receive any data from
calling function.
Functions with no return values i.e. the calling function does not receive any value from
the called function.
In effect there no data transfer between the calling and called function.

Example program
#include<stdio.h>
#include<conio.h>
void add();
void main()
{
22 | P a g e

clrscr();
add();
getch();
}
void add()
{
int a,b,c;
printf(“Enter the values of a,b\n”);
scanf(“%d%d”,&a&b);
printf(“the sum is %d”,a+b);
}

3. Functions with no arguments and functions with return values


Functions with no arguments i.e. the called function does not receive any data from the
calling function.
Functions with return values i.e. the calling function receives the value from the called
function.
In effect there is no data transfer from calling function to called function, but there is a
data transfer from the called function to calling function.

Example program
#include<stdio.h>
#include<conio.h>
int add();
void main()
{
int c;
clrscr();
c=add();
printf(“the sum is %d”,c);
getch();
}
int add()
{
int a,b;
printf(“Enter the values of a,b\n”);
scanf(“%d%d”,&a,&b);
return(a+b);
}

4.Functions with arguments and functions with no return values


Functions with arguments i.e. the called function receives the data from the calling
function.
Functions with no return values i.e. the calling function does not receives the value from
the called function.
In effect there is a data transfer from calling function to called function, but there is no
data transfer from the called function to calling function.

Example program

#include<stdio.h>
#include<conio.h>
void add(int x, int y);
23 | P a g e

void main()
{
int a,b;
clrscr();
printf(“Enter the values of a,b\n”);
scanf(“%d%d”,&a&b);
add(a,b);
getch();
}
void add(int x,int y)
{
printf(“The sum is %d”,x+y);
}

3. Explain parameter passing techniques with an example programs?


Ans:
Parameter passing:
The process of information interchanging between the calling function and the called
function is known as parameter parsing.
There are two methods
1) Call by value/Pass by value
2) Call by address/Call by reference/Pass by address

1.Call by value:
Sending or passing the values as actual arguments to the called function is known as call
by value.
In this method the changes are made in formal arguments of the called function will not
effect the actual arguments of the calling function.
Ex: Write a C program to interchange the values of two variables using call by value.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=20;
clrscr();
printf("Before swapping a=%d, b=%d \n", a,b);
swap(a,b);
printf(" After swapping a=%d, b=%d", a,b);
getch();
}
int swap(int x, int y)
{
int temp;
printf(" Before swapping x=%d, y=%d \n",x,y);
temp=x;
x=y;
y=temp;
printf(" After swapping x=%d, y=%d \n",x,y);
}

2.Call by address:
Sending or passing addresses as an actual arguments to the called function is known as
call by address.
24 | P a g e

In this method changes are made in formal arguments of the called function will effect
the actual arguments of the calling function.
Ex: Write a C program to interchange the value of two variable using call by address.
#include<stdio.h>
#include<conio.h>
void main()
{
int a =10,b=20;
clrscr();
printf("Before swapping a=%d, b=%d \n",a,b);
swap(&a,&b);
printf(" After swapping a=%d, b=%d",a,b);
getch();
}
int swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}

4. Explain the different types of storage classes in C?


Ans:
Storage Classes:
Storage class of a variable defines the following terms:
1. Where the variable would be stored.
2. What is the scope of the variable.
3. What is the default value of the variable.
4. What is the life time of the variable.
Based on the above terms storage classes are of four types:
1. Automatic storage class
2. Register storage class
3. Static storage class
4. External storage class

1.Automatic storage class:


1. It is indicated by the keyword ‘auto’.
2. It is stored in main memory.
3. The scope of auto storage class is local.
4. The default value of auto storage class is garbage value.
5. Lifetime of auto storage class is till the control remains within the function.
6. Syntax: auto datatype variable;
7. Example: auto int a;
2.Register storage class:
1. It is indicated by the keyword ‘register’.
2. It is stored in CPU registers.
3. The scope of register storage class is local.
4. The default value of register storage class is garbage value.
5. Lifetime of register storage class is till the control remains within the function.
6. Syntax: register datatype variable;
7. Example: register int a;
3.Static storage class:
25 | P a g e

1. It is indicated by the keyword ‘static’.


2. It is stored in main memory.
3. The scope of static storage class is local.
4. The default value of static storage class is garbage value.
5. Lifetime of static storage class is till the control remains within the function.
6. Syntax: static datatype variable;
7. Example: static int a;
4.External storage class:
1. It is indicated by the keyword ‘extern’.
2. It is stored in main memory.
3. The scope of extern storage class is global.
4. The default value of extern storage class is zero.
5. Lifetime of extern storage class is till the program terminates.
6. Syntax: extern datatype variable;
7. Example: extern int a;

5. Explain recursion and write a C program to find factorial of a given number using
recursion?
Ans:
Recursion:
When a called function in turn calls the same function then chaining occurs. Recursion is
a special case of this process or a repetitive process where a function calls itself.
C program to find the factorial of a given number using recursion.
#include<stdio.h>
#include<conio.h>
void main()
{
int r,n;
clrscr();
printf("Enter any number\n");
scanf("%d",&n);
r=factorial(n);
printf("the factorial of the given number is %d",r);
getch();
}
int factorial(int x)
{
int fact;
if(x= =0)
return(1);
else
fact=x*factorial(x-1);
return(fact);
}

6. Explain recursion and write a C program to find GCD of a given number using recursion?
Ans:
Recursion:
When a called function in turn calls the same function then chaining occurs. Recursion is
a special case of this process or a repetitive process where a function calls itself.

C program to find GCD(Greatest Common Divisor) of two numbers using recursion.


#include<stdio.h>
26 | P a g e

#include<conio.h>
void main()
{
int a,b,r;
clrscr();
printf(“Enter the two numbers\n”);
scanf((“%d%d”,&a,&b);
r=gcd(a,b);
printf(“GCD of a,b is %d”,r);
getch();
}
int gcd(int x,int y)
{
if(y= =0)
return (x);
else
return gcd(y,x%y);
}

UNIT-4

1. Define array and explain how one dimensional arrays are declared and initialized in C?
Ans:
1. Array is a collection of elements of the same data type.
2. Array is a collection of homogeneous elements.
3. Array range starts from 0 to n-1.
4. Array elements are stored in contiguous memory locations or consecutive memory
locations or successive memory locations.
1.One dimensional arrays (1DA) :-
If an array contains one subscript then it is called one dimensional array.
Declaration of one dimensional arrays (1DA)
Syntax:
datatype arrayname[size];
In the above syntax data type maybe any basic data type such as int,float, etc.
arrayname is any valid identifier.
size indicates number of elements can be stored in an array name.
Example: int arr[10];

Here int is the data type, arr is the name of the array and 10 is the size of array. It means
array arr can only contain 10 elements of int type. Index of an array starts from 0 to size-
1 i.e first element of arr array will be stored at arr[0] address and last element will occupy
arr[9].

Initialization (or) Assigning values to one dimensional arrays (1DA) :


27 | P a g e

Syntax :
datatype arrayname[size] ={ value1, value2,……, value n};
Example:
int x[5]={10, 20, 30, 40, 50};
In the above example five integer elements can be stored in the array name ‘x’
Here x[0] refers to the 1st element stored in the array i.e.10
x[1] refers to the 2nd element stored in the array i.e.20
x[2] refers to the 3rd element stored in the array i.e.30
x[3] refers to the 4th element stored in the array i.e.40
x[4] refers to the 5th element stored in the array i.e.50

2. Write a C program to find both the largest and smallest elements from the given array?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int x[5],i,large,small;
clrscr();
printf(“ Enter any five integer array elements\n”);
for(i=0;i<5;i++)
{
scanf(“%d”,&x[i]);
}
large=x[0];
small=x[0];
for(i=0;i<5;i++)
{
if(x[i]>large)
large=x[i];
if(x[i]<small)
small=x[i];
}
printf(“The largest element from the given array is %d \nThe smallest element from the
given array is %d”,large,small);
getch();
}

3. Explain how two dimensional arrays are declared and initialized in C?


Ans:
Two Dimensional Arrays (2DA):
If an array contains two subscripts then it is called two dimensional arrays.
It is also called multi dimensional array.
Two dimensional array elements are stored in consecutive memory locations.
Two dimensional arrays are mainly used for performing matrix operations like matrix
addition, subtraction,multiplication,transpose,etc.

Declaration of Two Dimensional Arrays (2DA):


datatype arrayname[size1][size2] ;
In the above syntax data type maybe any basic data type such as int,float, etc.
arrayname is any valid identifier.
28 | P a g e

size1 indicates no. of rows and size2 indicates no. of columns stored in an array name.
Example:
int a [3][4];

Initialization(or)Assigning values to two dimensional arrays (2DA) :


Syntax :
datatype arrayname[size1][size2] ={ value1, value2,……, value n};
Example:
int x[2][2] = {10,20,30,40};
In the above example 2 rows and 2 columns can be stored in the array name ‘x’
Here x[0][0] refers to the 1 st element stored in the array i.e.10
x[0][1] refers to the 2nd element stored in the array i.e.20
x[1][0] refers to the 3rd element stored in the array i.e.30
x[1][1] refers to the 4th element stored in the array i.e.40

4. Write a C program to perform transpose of a matrix of order 3*3?


#include<stdio.h>
#include<conio.h>
void main()
{
int A[3][3],Transpose[3][3],i,j;
clrscr();
printf(“Enter the values of elements of matrix A\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&A[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
29 | P a g e

Transpose[i][j]=A[j][i];
}
}
printf(‘The Transpose of matrix A is\n”);
for(i=0;i<3<i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,Transpose[i][j]);
}
printf(“\n”);
}
getch();
}

5. Write a C program to perform addition of two matrices and print the result in another
matrix of order 3*3?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int A[3][3], B[3][3], C[3][3],i,j;
clrscr();
printf(“Enter the elements of matrix A\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&A[i][j]);
}
}
printf(“Enter the elements of matrix B\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(“The result in C matrix is \n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
30 | P a g e

{
printf(“%d\t”,C[i][j]);
}
printf(“\n”);
}
getch();
}

6. Write a C program to perform multiplication of two matrices and print the result in
another matrix of order 3*3.
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int A[3][3], B[3][3], C[3][3],i,j,k;
clrscr();
printf(“Enter the elements of matrix A\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&A[i][j]);
}
}
printf(“Enter the elements of matrix B\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]=0;
for(k= 0;k<3; k++ )

{
C[i][j]=C[i][j]+(A[i][k]*B[k][j]);
}
}
}
printf(“The result in C matrix is \n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,C[i][j]);
31 | P a g e

}
printf(“\n”);
}
getch();
}

7. Define string and explain how strings are declared and initialized in C?
Ans:
1.String is a group of characters enclosed within double quotation marks
2.String is also called array of characters or character arrays.
3.Character Array elements are stored in contiguous memory locations or consecutive
memory locations or successive memory locations.
4.The string should end with “\0” (null character) in C language.
5.The size of the string is equal to no.of characters in the string + 1 (required for null
character(‘\0’)).

Declaration of strings/character arrays:


Syntax:
datatype arrayname[size];
In the above syntax data type must be ‘char’ data type.
array name is any valid identifier
size indicates no.of characters in the string + 1 (required for null character(‘\0’)).
Example: char name[20];
Initilization of strings/character arrays:
Syntax: datatype arrayname[size]={character1,character2,…,character n};
Example:
char city[6]=“DELHI”;
(or)
char city[6]={‘D’,’E’,’L’,’H’,’I’,’\0’};
city[0] refers to 1st character in string i.e. D
city[1] refers to 2ndcharacter in string i.e. E
city[2] refers to 3rdcharacter in string i.e. L
city[3] refers to 4thcharacter in string i.e. H
city[4] refers to 5thcharacter in string i.e. I
city[5] refers to 6thcharacter in string i.e. ‘\0’.

8. Explain the string handling/manipulation functions in C with an example programs?


Ans:
String Manipulation Functions/String Handling Functions:-
All the string handling functions are pre-defined in the system library i.e., in the header
file # include<string.h>
The following are commonly used string handling functions in C are:-
1. strcpy()
2. strcat()
3. strrev()
4. strlen()
5. strlwr()
6. strupr()
7. strcmp()

1.strcpy()
This function is used to copy one string into another string.
Syntax: strcpy(string1,string 2);
32 | P a g e

Here, string 2 is copied into string 1 and after copying both the contents of string 1 and
string 2 are same.
Example: Write a c program to copy one string into another string using strcpy().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[20],name2[20];
clrscr();
printf(“Enter the first name\n”);
gets(name1);
printf(“Enter the second name\n”);
gets(name2);
strcpy(name1,name2);
printf(“After copying the name1=%s,name2=%s”,name1,name2);
getch();
}
2.strcat():-
This function is used to combine two strings.
Syntax: strcat (string1,string2);
Here, string2 is added to the end of the string1.
Example : Write a C program to combine two strings using strcat().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[20],name2[20];
clrscr();
printf(“Enter the first name\n”);
gets(name1);
printf(“Enter the second name\n”);
gets(name2);
strcat(name1,name2);
printf(“After concatenation the name is %s”,name1);
getch();
}
3.strrev() :-
This function is used to reverse a given string.
Syntax: strrev(string);
Example: Write a c program to find the reverse of given string using strrev().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[20];
clrscr();
printf(“Enter any name\n”);
gets(name);
strrev(name);
printf(“The reverse of a given name is %s”,name);
33 | P a g e

getch();
}
4.strlen():-
This function is used to find the length of the given string.
This function does not include the ‘\0’(null character).
Syntax: strlen(string);
Example: Write a c program to find the length of a given string using strlen().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[20];
int x;
clrscr();
printf(“Enter any name\n”);
gets(name);
x=strlen(name);
printf(“The length of a given string is %d”,x);
getch();
}
5.strlwr():-
This function is used to convert any uppercase letters into lowercase letters.
Syntax: strlwr(string);
Example: Write a c program to convert any uppercase letters into lowercase letters using
strlwr().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name [20];
clrscr();
printf(“Enter any name\n”);
gets(name);
strlwr(name);
printf(“The converted name is %s”,name);
getch();
}
6.strupr():-
This function is used to convert any lowercase letters into uppercase letters.
Syntax: strupr(string);
Example: Write a c program to convert any lowercase letters into letters uppercase using
strupr().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name [20];
clrscr();
printf(“Enter any name\n”);
gets(name);
34 | P a g e

strupr(name);
printf(“The converted name is %s”,name);
getch();
}
7.strcmp():-
This function is used to compare two strings character by character based on ASCII values
(American Standard Code for Information Interchange) and returns zero when both the
strings are equal.
Syntax: strcmp(string1,string2);
Example1 : Write a c program to check whether the two strings are equal or not.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name1[20],name2[20];
clrscr();
printf(“Enter the first name\n”);
gets(name1);
printf(“Enter the second name\n”);
gets(name2);
if(strcmp(name1,name2)= =0);
printf(“Both the given strings are equal”);
else
printf(“Both the given strings are not equal”);
getch();
}

9. Write a C program to check whether the given string is palindrome or not?


Ans:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string[20];
int i,length,flag=0;
clrscr();
printf(“Enter any string\n);
scanf(“%s”,&string);
length=strlen(string);
for(i=0;i<length;i++)
{
if(string[i]!=string[length-i-1])
{
flag=1;
break;
}
}
if(flag)
printf(“The given string is a palindrome”)
35 | P a g e

else
printf(“The given string is not a palindrome”)
getch();
}

UNIT-5

1. Define pointer and explain how the pointer variable is declared and initialized in C?
Ans:
Pointer is a variable that stores the address of another variable. C Pointer is used to
allocate the memory dynamically i.e. at run time.
Declaration of a pointer:
datatype *variable;
Example: int *p;
Here p is a pointer variable which always points to integer data type.

Reference operator (&)

& is called reference operator/address operator which specifies where the value would
be stored.

Dereference operator/Indirection operator (*)

* is called indirection operator or dereferencing operator which indicates value at


address.

Advantages or Uses of pointers in C


1. Pointers provide direct access to memory
2. Pointers provide a way to return more than one value to the functions
3. Reduces the storage space and complexity of the program
4. Reduces the execution time of the program
5. Provides an alternate way to access array elements
6. Pointers can be used to pass information back and forth between the calling
function and called function.
7. Pointers helps us to build complex data structures like linked list, stacks, queues,
trees, graphs etc.

Example: Write a c program to print the values and its corresponding addresses on the
monitor.
#include<stdio.h>
#include<conio.h>
void main()
{
int *p,x=10;
clrscr();
p=&x;
printf(“the address of p is %u\n”,&p);
printf(“the address of x is%u\n”,&x);
printf(“the value of p is%u\n”,p);
printf(“the value of x is%d\n”,x);
36 | P a g e

printf(“the value at p is %d\n”,*p);


printf(“the value at x is %d\n”,*(&x));
getch();
}

2. Explain pointers with arrays with an example program?


Ans:
Pointers and Arrays

When an array is declared, compiler allocates sufficient amount of memory to contain all
the elements of the array. The base address is the location of the first element (index 0)
of the array.

Declaration of pointers and arrays

Syntax: datatype arrayname[size],*variable;

Example: Suppose we declare an array arr,

int arr[5]={ 1, 2, 3, 4, 5 };

Assuming that the base address of arr is 1000 and each integer requires two bytes, the
five elements will be stored as follows

Here variable arr will give the base address, which is a constant pointer pointing to the
element, arr[0]. Therefore arr is containing the address of arr[0] i.e 1000.

We can declare a pointer of type int to point to the array arr.

int *p;
p = &arr[0];

Now we can access every element of array arr using p++ to move from one element to
another.

Example: Write a C program to read and print any 5 integer array elements using
pointers.
#include<stdio.h>
#include<conio.h>
void main()
{
int x[5],*p,i;
clrscr();
37 | P a g e

p=&x[0];
printf(“ Enter any five integer array elements\n”);
for(i=0;i<5;i++)
{
scanf(“%d”,&x[i]);
}
printf(“ The 5 integer array elements are \n”);
for(i=0;i<5;i++)
{
printf(“%d is stored at address %u\n”,*p,p);
p++;
}
getch();
}

3. Write a C program to find the sum of all elements stored in array using pointers?
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int x[5],*p,i,sum=0;
clrscr();
p=&x[0];
printf(“ Enter any 5 integer array elements\n”);
for(i=0;i<5;i++)
{
scanf(“%d”,&x[i]);
}
for(i=0;i<5;i++)
{
sum=sum+*p;
p++;
}
printf(“ The sum of array elements using pointers is %d”,sum);
getch();
}

4. Define a structure and explain about declaration, initialization and accessing of


structure members in C with an example program?
Ans:
Structure is a collection of elements of different data types.
Structure is a collection of heterogeneous elements.
Declaration of a Structure:-
Syntax:
struct tagname
{
datatype variable1;
datatype variable2;
datatype variable3;
.
38 | P a g e

.
datatype variable n;
};
In the above syntax struct is a keyword that declares/stores the members or fields of a
structure.
tag name is name of the structure.
datatype maybe any basic datatype such as int,float,char etc.
variable1,variable2,…………….,variable n are structure members or fields of a structure.
The body of a structure should be end with semicolumn(;).
Example:
struct student
{
int rno[20];
char name[20];
char branch[20];
float marks;
};
Declaration of a structure variable:-
Syntax:-
struct tagname variable;
Example:
struct student s;
Inilization of a structure variable:-
Syntax:-
struct tagname variable={member1,member2,…member n};
Example:
struct student s={501,”Dinesh”,”cse”,100.5};
Member operator or dot operator( . ):-
To access the each member of a structure using the structure variable we have to use dot
operator ( . ) in c language.
Syntax:-
structurevariable.structuremember

Example Program:
#include<stdio.h>
#include<conio.h>
struct student
{
int rno;
char name[20];
char branch[20];
float marks;
};
void main()
{
struct student s;
clrscr();
printf(“Enter the student rno,name,branch and marks\n”);
scanf(“%d%s%s%f”,&s.rno,&s.name,&s.branch,&s.marks);
printf(“the student details are\n”);
printf(“%d\t%s\t%s\t%f”,s.rno,s.name,s.branch,s.marks);
getch();
}
39 | P a g e

5. Explain array of structures with an example program?


Ans:
Array of Structures:
The same structure is applied to a group of people or group of items then array of
structures is used.
Syntax:
struct tagname arrayname[size];
Example: Define a structure type student that would contain roll number,name,branch
and marks. Write a C program to read this information for 5 students from keyboard and
print the same on monitor.
#include<stdio.h>
#include<conio.h>
struct student
{
int rno;
char name[20];
char branch[20];
};
void main()
{
struct student s[5];
int i;
clrscr();
printf(“Enter any 5 student details \n”);
printf(“Enter the student rno,name,branch\n”);
for(i=0;i<5;i++)
{
scanf(“%d%s%s”,&s[i].rno,&s[i].name,&s[i].branch);
}
printf(“the 5 students details are\n”);
for(i=0;i<5;i++)
{
printf(“%d\t%s\t%s\n”,s[i].rno,s[i].name,s[i].branch);
getch();
}

6. Differentiate between structure and union.


Ans:
Structure is a collection of elements of different data types.
Structure is a collection of heterogeneous elements.
Declaration of a Structure:-
Syntax:
struct tagname
{
datatype variable1;
datatype variable2;
datatype variable3;
.
.
datatype variable n;
};
40 | P a g e

In the above syntax struct is a keyword that declares/stores the members or fields of a
structure.
tag name is name of the structure.
datatype maybe any basic datatype such as int,float,char etc.
variable1,variable2,…………….,variable n are structure members or fields of a structure.
The body of a structure should be end with semicolumn(;).
Example:
struct student
{
int rno[20];
char name[20];
char branch[20];
float marks;
};
Declaration of a structure variable:-
Syntax:-
struct tagname variable;
Example:
struct student s;
Inilization of a structure variable:-
Syntax:-
struct tagname variable={member1,member2,…member n};
Example:
struct student s={501,”Dinesh”,”cse”,100.5};
Member operator or dot operator( . ):-
To access the each member of a structure using the structure variable we have to use dot
operator ( . ) in c language.
Syntax:-
structurevariable.structuremember
union
union is a collection of elements of different data types.
union is similar to structures except the storage allocation i.e., in structures each member
has its own storage allocation but in unions all the members use the same memory
allocation.
A union can contain any numbers of members but it can handle one member at a time i.e.,
at a time only one member can be stored in union.
Declaration of a union
union tagname
{
datatype variable1;
datatype variable2;
datatype variable3;
.
.
datatype variable n;
};
In the above syntax union is a keyword that declares/stores the members or fields of a
union.
tag name is name of the union.
datatype maybe any basic datatype such as int,float,char etc.
variable1,variable2,…………….,variable n are union members or fields of a union.
The body of a union should be end with semicolumn(;).
Example:
41 | P a g e

union student
{
int rno[20];
char name[20];
char branch[20];
float marks;
};
Declaration of a union variable:-
Syntax:-
union tagname variable;
Example:
union student s;
Inilization of a union variable:-
Syntax:-
union tagname variable={member1,member2,…member n};
Example:
union student s={501,”Dinesh”,”cse”,100.5};
Member operator or dot operator( . ):-
To access the each member of a union using the union variable we have to use dot
operator ( . ) in c language.
Syntax:- unionvariable.unionmember

You might also like