Nothing Special   »   [go: up one dir, main page]

Unit - L: List and Explain The Functions of Various Parts of Computer Hardware and Software

Download as pdf or txt
Download as pdf or txt
You are on page 1of 50

UNIT - l

Q1.List and explain the functions of various parts of computer hardware and software.

Ans : Computer System :


A computer is a system made up of two major components:
l. Computer Hardware.
II. Computer Software.

The following figure shows a computer system.

Fig : A Computer System


I. Computer Hardware:
The computer hardware is the physical equipment.
The hardware component of computer system consists of 5 parts
A. Input Devices.
B. Central Processing Unit ( CPU ).
C. Primary Storage.
D. Output Devices.
E. Auxiliary Storage Devices.
The following figure shows the hardware components.

Fig: Hardware Components


A. Input Devices: The input device is usually a keyboard where programs
and data are entered in to the computer.
Other Input Devices : a touch screen , a mouse , a pen , an audio input unit.
B. Central Processing Unit ( CPU ): It is responsible for executing instructions
such as arithmetic calculations , comparisons among data and movement
of data inside the system. Today’s computers may have one or more CPU’s

C. Primary Storage: It is also known as main memory . It is a place where


the programs and data is stored temporarily during processing.
The Data in primary storage is erased , when we turn off a personal
computer or when we log off from a time-sharing computer ( volatile ).

D. Output Devices: The output device is usually a monitor or a printer


to show output. If the output is shown on the monitor, it is a soft copy
and if the output is printed on the printer, it is a hard copy.

E. Auxiliary Storage Devices ( secondary storage devices ):


It is used for both input and output. It is also known as secondary storage.
It is a place where the programs and data are stored permanently.
when we turn off the computer the programs and data remain in the
secondary storage, ready for the next time when we need them.

II. Computer Software :


Software is the collection of Programs ( instructions ) that allow the
hardware to do its job.
They are two types of Computer Software.
A. System Software
B. Application Software

The following figure shows the Computer Software.

Fig: Computer Software


A. System Software:
System Software consists of programs that manage the hardware resources
of a computer and perform required information processing tasks.

These programs are divided in to three classes.


i. Operating System Software.
ii. System Support Software.
iii. System Development Software.

i. Operating System Software:


It provides services such as a user interface, files and data base access
and interfaces to communication systems such as Internet protocols.
The primary purpose of this software is to keep the system operating
in an efficient manner while allowing the users access to the system.
ii. System Support Software :
System Support Software provides system utilities and other operating services.
Examples of system utilities are sort programs and disk format programs.
Operating services consist of programs that provide performance statistics for
the operational staff and security monitors to protect the system and data.
iii. System Development Software:
It includes language translators that convert programs in to machine language
for execution , debugging tools to ensure that programs are error - free
and computer -assisted software engineering ( CASE ) systems.

B. Application Software:
It is directly responsible for helping users to solve their problems.
Application software is broken in to two classes.
i. General - Purpose Software
ii. Application - Specific Software

i. General Purpose Software:


It is purchased from a software developer and can be used for more than
one application. Examples: word processors, database management systems,
computer – aided design systems. They are labeled general purpose because
they can solve a variety of user computing problems.

ii. Application Specific Software:


It can be used only for its intended purpose.
Example: A general ledger system used by accountants.
They can be used only for the task for which they were designed.
They can not be used for other generalized tasks.
Relationship between system and application software is shown in the figure:
Fig: Relationship between System and Application Software

Each circle is an interface point. The inner core is hardware. The user is
represented by the outer layer. To work with the system , the typical user
uses some form of application software. The application software in turn
interacts with operating system ( OS ), which is part of system software layer.
The System software provides the direct interaction with the hardware.
The opening at the bottom of the figure is the path followed by the
user who interacts directly with the Operating System when necessary.

Q2. Explain Creation and Running of Programs ?


or
Describe how the Developers will Write the Programs ?

Ans: CREATING AND RUNNING PROGRAMS :

It is the job of programmer to write and test the program.


The following are four steps for creating and running programs:
A. Writing and Editing the Program.
B. Compiling the Program.
C. Linking the Program with the required library modules.
D. Executing the Program.

A. Writing and Editing Program :


The Software to write programs is known as text editor.
A text editor helps us enter , change and store character data.
Depending on the editor on our system, it could be used to write letters,
create reports or write programs.
Example : word processor.
The text editor could be generalized word processor, but every compiler
comes with associated text editor. Some of the features of editors are
Search : To locate and replace statements.
Copy , Paste : To copy and move statements.
Format : To set tabs to align text.
After the program is completed the program is saved in a file to disk.
This file will be input to the compiler, it is known as source file.
The following figure shows the various steps in building a C – program.

Fig: Building a C - program

B. Compiling Programs:
The code in a source file on the disk must be translated in to
machine language.
This is the job of compiler which translates code in source file stored
on disk in to machine language. The C compiler is actually two
separate programs: the preprocessor and the translator.
The preprocessor reads the source code and prepares it for the compiler.
It scans special instructions known as preprocessor commands.
These commands tell the preprocessor to take for special code libraries,
make substitutions in the code. The result of preprocessing is called
translation unit. The translator reads the translation unit and writes
resulting object module to a file that can be combined with other
precompiled units to form the final program.
An object module is the code in machine language. This module is not ready
for execution because it does not have the required C and other functions included.

C. Linking Programs:
C programs are made up of many functions.
Example: printf ( ) , cos ( )… etc.
Their codes exists else where , they must be attached to our program.
The linker assembles all these functions , ours and the systems, in to a
final executable program.
D. Executing Programs :
Once our program has been linked, it is ready for execution.
To execute a program, we use operating system command, such as
run to load the program in to main memory and execute it.
Getting program in to memory is the function of an Operating System
programs called loader.
Loader locates the executable program and reads it in to memory.
In a typical program execution, the program reads data for processing,
either from user or from file.
After the program processes the data, it prepares output. Data output
can be to user’s monitor or to a file. When program has executed,
Operating System removes the program from memory.

Q3. Describe the Basic Steps in the System Development Life Cycle?
or
Explain about SDLC?

Ans: System Development :


It tells how a program is developed. The careful design of each
program using structured development techniques makes program
efficient , error-free and easy to maintain.

System Development Life Cycle:


Modern programming projects are built using a series of inter related
phases commonly referred to as System Development Life Cycle.

Depending on the type of life cycle the number of phases changes.


Most popular one is Waterfall model.
The following figure shows the Waterfall model.

Fig: Waterfall Model

The software development life cycle consists of six phases. They are
A. System requirements
B. Analysis
C. Design
D. Code
E. System test
F. Maintenance
A. System requirements :
System analyst defines requirements that specify what the proposed system is
to accomplish. Requirements are usually stated in terms that user understands.

B. Analysis :
The analysis phase looks at different alternatives from a system point of view.

C. Design:
The design phase determines how the system will be built.
The functions of the individual programs that will make up the system are
determined and the design of the files and /or the databases is completed.

D. Code:
We write programs. After the programs have been written and tested
for programmer satisfaction.

E. System test:
We test the entire program together, to make sure the system works as a whole.

F. Maintenance:
Keeps the system working once, it has been put onto production. As each phase
is developed, errors and omissions will often be found in the previous work .When
this happens, it is necessary to go back to the previous work to rework it for
consistency and analyze the impact caused by the changes.

Q4. List and Explain about the Program Development?


or
Explain how the Developers will Test the Program?
or
What do you mean by ‘program development cycle’? Explain the steps involved in
it?

Ans: Program Development :

Program Development is a multi step process as follows


A. Understand the Problem
B. Develop a Solution - Algorithm - Pseudo code - Flowchart
C. Write a Program
D. Test it.
For program development , developers are given

• Program requirement statement


• Design of program interfaces
• Overview of the complete project
A. Understand the problem :
The first step in solving any problem is understand it. We begin by reading
the requirements statements carefully.When we think that we fully understand it,
we review our understanding with the user and the system analyst.
Example: Calculate the square footage of house.
The questions that arise are
1. What is definition of square footage ?
2. How is it useful ?

B. System requirement:
We do planning of functional requirements - Software’s Version, Non-
functional requirements - Resources - Internet , table , A/cs , Coffee.
User functional requirements - Problem Oriented and Language used is
in simple i.e. English.

C. Analysis:
User requirements are transformed to technical terms like Blueprint and
also look for different alternatives considering all risks.

D. Design:
In UML software users will develop flowcharts, structure charts, data flow
diagrams
1. Choose the best way to find solution among different alternatives.
2. Choose in analysis , design files , databases and functions.

E. Code:
Unimportant in Software development life cycle
1. Uses software for code generation ( forward energy ).
2. Otherwise go for manual coding.
3. Basic testing is done ( compilation ).

F. System Test :
Test if user requirements are met
Blackbox Testing: Test programs together, make sure system works as a whole.
Whitebox Testing: Release ά-version to customer, according to his/her feed back
we develop β-Version: β - version is released to selected group of people.
G. Maintenance :
Keeps the system working once it has been put in to production ?

Q5. Explain about Algorithm ?


or
Define Algorithm and State Properties of it ?
Ans: Algorithm :

Program = Algorithm + Data


Algorithm is a finite set of instructions that , it followed accomplishes a
particular task.
Algorithm should satisfy the following criteria
1. Input : Zero or more quantities are externally supplied.
2. Output : At least one quantity is produced.
3. Definiteness : Each instruction is clear and unambiguous. Ex Add B or C
to A
4. Finiteness : Algorithm should terminate after finite number of
steps
when traced in all cases
Ex: Go on adding elements to an array
5. Effectivenes: Every instruction must be basic i.e., it can be carried
out,
by a person using pencil and paper.
Algorithm must also be general to deal with any situation.
Example: Calculate Fibonacci series.
There are two forms of representing an activity. They are
1. By describing the process step - by - step – Algorithm
2. By representing the various steps in the form of a diagram –flowchart.

Q 6. Explain the Algorithm with Examples ?

Ans: Algorithm:
The same problem can be solved with different methods. So, to solve a problem
different algorithm, may be accomplished. Algorithm may vary in time, space utilized.
User writes algorithm in his / her own language.
So, it can not be executed on computer. Algorithm should be in sufficient detail
that it can be easily translated in to any of the languages.

Example1 : Add two numbers.


Step 1: Start
Step 2: Read 2 numbers as A and B
Step 3: Add numbers A and B and store result in C
Step 4 : Display C
Step 5: Stop
Example2: Average of 3 numbers.
1. Start
2. Read the numbers a , b , c
3. Compute the sum and divide by 3
4. Store the result in variable d
5. Print value of d
6. End

Example3: Average of n inputted numbers.


1. Start
2. Read the number n
3. Initialize i to zero
4. Initialize sum to zero
5. If i is greater than n
6. Read a
7. Add a to sum
8. Go to step 5
9. Divide sum by n & store the result in avg
10. Print value of avg
11 . End

Q7. Explain about Pseudo Code ?

Or
Write the rules for writing Psuedocode with example.What are the advantages and disadvantages
of Psuedocode? [10+6]

Ans: Pseudo Code:


Pseudo code is part English, part program logic. It describes algorithm
what the program being designed is to do. Detail, is a kind of structured
English for describing algorithms.
It was made up of 2 words : pseudo and code.
Pseudo means imitation and code refers to instructions, written in a
programming language.
It does not obey the syntax rules of any particular language. It is not
real programming code.

Keywords for writing Pseudo Code.

Input: READ , OBTAIN , GET


Output: PRINT , DISPLAY , SHOW
Compute: COMPUTE , CALCULATE , DETERMINE
Initialize: SET , INIT
Add one: INCREMENT , BOMP.

Pseudo Code Guidelines :

Any language ( English ) should be used for writing Statements.


Statements should be long independent.
1. It must produce solution.
2. Logic must be decomposed to a level of single loop or decision.
Example: Not like search the list and find the customer with highest balance.
3. Write each instruction in separate line.
4. Should be concise : Ignore unnecessary details.
5. Use programming shorthands – if - then - else etc. with out writing
equivalent English.
6. If it can not be translated to working code means something is wrong.
7. Keywords should be highlighted by capitalizing them.
8. Type of variable used is clear from context.

Benefits or advantages of Pseudo Code :

1. Allow designer to focus on logic of algorithm, not on details of long syntax.


2. It is long independent.
3. It can be translated to any computer language code.
4. Unlike flowcharts , pseudo codes are concise, more readable and
easier to modify.
5. It allows designer to express the design in plain natural languages.

Q8. How to Write the Algorithm ?

Ans: Algorithm has 3 sections:

Header: Algorithm Name.


Declarations: A description of variables and their types.
Algorithm body’s: A sequence of algorithm steps.
………..
………..

Halt

Example1: Add two numbers.

1. Start
2. [ Obtain input values ]
Read ( A , B )
3. [ Add numbers and store the result ]
CA+B
4. [ Print result ]
Write ( C )
5. Stop

Example2: Find xn
1. Start
2. [ obtain input values ]
Read ( x , n )
3. [ Initialize result so far ]
result → x
4. [ Initialize loop counter ]
i →1
5. [ Print result ]
repeat thru step 7 while i < n
6. [ Recomputed result so far ]
result <-- result * x
7. ( Increment counter )
i <-- i + 1
8. ( Display result )
Write ( x , n , result )
( Finished )
9. Halt.
Guidelines :

In text and lectures , algorithms are often expressed in pseudo code.


Both are almost same.
Guidelines for algorithm and pseudo code are.
1. Each Algorithm will be logically enclosed by 2 statements START,
STOP.
2. To accept data from user, INPUT or READ statement is to be
used.
3. To display any used message or content of variable, PRINT statement
is used. Message should be enclosed within quotes.
4. Assignment : =
5. Arithmetic Operators :
+ - add – - Sub * - Mul / - Div
6. Relational: > , > = , < , < = , == , ! =
7. Logical: AND - && , OR – ll , NOT - !.

Notations used :

Beginning or Ending: Start , Stop


Input: Read ( variables )
Example: Read ( var 1 , var 2 )
Output: Write ( “ Test to be displayed “ , variables )
Example: Write ( “ Learning C “ , var 1 , var 2 )
Comment: [ Information ]
Operators:
&& , > , < , + , - , * , / , % , and , or , not
Q9. Explain How to Test a Program?

Ans: Test the Program :


Program Testing can be a very tedious and time consuming part of program
development. There are two types of testing.
Blackbox Testing - Done by the system test engineer and the user.
Whitebox Testing - Responsibility of the programmer.
Blackbox Testing assumes that the tester knows nothing about the program.
Whitebox Testing assumes that the tester knows everything about the program.

Blackbox Testing :

• Testing the program with out knowing what is inside it – with out
knowing how it works.
• Test Plans are developed by looking only at the requirements statement:
We should ask to see this Test Plan before we write our Program.

Whitebox Testing :

• It tests every instruction and every possible situation.


• Verify every line of code has been executed at least once .
• Verify every Conditional Statement has executed both the true and
false branches , even if one of them is null.
• For every Condition that has a range , make sure the tests include
first and last items in the range as well as the items below the first
and above the last.
• If error conditions are being checked, make sure all error logic is tested.

Q10. Explain about a Flowchart ?

Ans: Flowchart :

A flowchart is a visual representation of the sequence of steps for


solving a problem . A flowchart is a set of symbols that indicate various
operations in the program. For every process , there is a corresponding symbol
in the flowchart . Once an algorithm is written , its pictorial representation
can be done using flowchart symbols. In other words, a pictorial representation
of a textual algorithm is done using a flowchart.

A flowchart gives a pictorial representation of an algorithm.


• The first flowchart is made by John Von Neumann in 1945.
• It is a symbolic diagram of operations sequence, dataflow, control flow
and processing logic in information processing.
• The symbols used are simple and easy to learn.
• It is a very helpful tool for programmers and beginners.

Purpose of a Flowchart :
• Provides Communication.
• Provides an Overview.
• Shows all elements and their relationships.
• Quick method of showing Program flow.
• Checks Program logic.
• Facilitates Coding.
• Provides Program revision.
• Provides Program documentation.

Advantages of a Flowchart :
• Flowchart is an important aid in the development of an algorithm itself.
• E asier to Understand than a Program itself.
• Independent of any particular programming language.
• Proper documentation.
• Proper debugging.
• Easy and Clear presentation.

Limitations of a Flowchart :
• Complex logic.
• Drawing is time consuming.
• Difficult to draw and remember.
• Technical detail.

Q11. Explain the Symbols in a Flowchart ?


Ans: Symbols : Symbols are divided in to the following two parts.
l. Auxiliary Symbols.
ll. Primary Symbols.
Some of the common symbols used in flowcharts are shown below :
Fig: Flow chart Symbols

with flowcharting , essential steps of an algorithm are shown using the


shapes above.
The flow of data between steps is indicated by arrows or flow lines.

Q13. Write an algorithm and flow chartfor swapping two numbers


Ans: To Swap two integer numbers:
Algorithm : a. using third variable

Step 1 : Start
Step 2 : Input num1 , num2
Step 3 : temp = num1
Step 4 : num1 = num2
Step 5 : num2 = temp
Step 6 : Output num1 , num2
Step 7 : Stop

Algorithm : b. with out using third variable

Step 1 : Start
Step 2 : Input num1 , num2
Step 3 : calculate num1 = num1 + num2
Step 4 : calculate num2 = num1 – num2
Step 5 : calculate num1 = num1 – num2
Step 6 : Output num1 , num2
Step 7 : Stop

Flowcharts :

Start Start

READ READ
num1,num num1,num
2 2

temp = num1 num1 = num1 + num2


num1 = num2 num2 = num1 - num2
num2 = temp num1 = num1 - num2
Fig a: With using third variable Fig b: Without using third
variable

Q14.Write an algorithm and flowchart to find the largest among two numbers.
Ans:
Algorithm:

Step 1 : Start
Step 2 : Input A , B
Step 3 : if A > B then output A
else output B
Step 4 : Stop

Flowchart :
START

Input A , B

Yes No
Output-A a Output-B

STOP

Q15.Write an algorithm and flowchart to find the largest among three numbers.
Algorithm: (method 1)
Step 1 : Start
Step 2 : Input A, B, C
Step 3 : if A > B go to step 4 , otherwise go to step 5
Step 4 : if A > C go to step 6 , otherwise go to step 8
Step 5 : if B > C go to step 7, otherwise go to step 8
Step 6 : print “ A is largest ” , go to step 9
Step 7 : print “ B is largest ”, go to step 9
Step 8 : print “ C is largest ” , go to step 9
Step 9 : Stop

Flowchart:

START

Input A,B,C

Output B b a a Output A
Yes No Yes Yes

No Output CNo

STOP

Algorithm: b.

Step 1 : Start
Step 2 : Input A, B, C
Step 3 : Let max = A
Step 4 : if B > max then max = B
Step 5 : if C > max then max = C
Step 6 : output max is largest
Step 7 : Stop

Flowchart:

Start

Read A , B ,C

max = A
False

True

T
False

True

Q16.Write an algorithm and flowchart to find factorial of a number


Hint: fact (4) = 1 * 2* 3* 4
Sol. Algorithm:
Step 1: Start
Step 2: Input n
Step 3: Initialize counter variable, i , to 1 and factors = 1
Step 4: if i <= n go to step 5 otherwise go to step 7
Step 5: calculate factors = factors * i
Step 6: increment counter variable, i, and go to step 4
Step 7: output factors.
Step 8: stop
Flow chart:

Start

READ n
F

Q17.Write an algorithm and flowchart to find factors (divisors) of a number

Hint: For any given number n, the set of possible factors are 1, 2, 3, … n/2. Divide the
given number by a sequence of numbers 1,2,3,….n/2 and print those numbers for which
the remainder is zero.
Algorithm:
Step 1: Start
Step 2: Input n
Step 3: Initialize counter variable, i , to 1
Step 4: if i > n/2 go to step 7
Step 5: if (n % i == 0) output i
Step 6: increment i and go to step 4
Step 7: stop
Flow chart:

START

READ n
True

F
False
True False

Q18.Write an algorithm and flowchart to find whether a number is prime or not.

Hint: A number is said to be prime number for which the only factors are 1 and itsel
Algorithm:
Step 1: Start
Step 2: Input n
Step 3: Let i = 1, count=0
Step 4: if i > n/2 go to step 7
Step 5: if (n % i = = 0) count = count + 1
Step 6: increment i and go to step 4
Step 7: if count=2 then print “prime number”
else print “not prime number”
Step 8: Stop
Flow chart:
START

READ n

i = 1
count=0
Yes

No
Yes No

No

Yes

Q19.Write an algorithm and flowchart to find whether a number is perfect or


not.
Integer N is called a perfect number if sum of all its factors is equal to number itself.
Algorithm:
Step 1: Start
Step 2: Input n
Step 3: Let i = 1, sum=0
Step 4: if i > n/2 go to step 7
Step 5: if (n % i = = 0) sum = sum + i
Step 6: increment i and go to step 4
Step 7: if sum = = n then print “perfect number”
else print “not perfect number”
Step 8: Stop
Flow chart:

START

READ n

i=1
sum=0
Yes

No
Yes No

No

Yes

Q20.Write an algorithm and flowchart to find the sum of first 20 natural numbers
Hint: Sum = 1+2+3+………+20
Algorithm:
Step 1: Start
Step 2: k = 1, sum = 0
Step 3: if k > 20 then go to step 7
Step 4: sum = sum + k
Step 5: k=k+1
Step 6: go to step3
Step 7: output sum
Step 8: Stop

Flow chart:
Q21.Write an algorithm and flowchart to find sum of digits of a number.
Hint: Ex: 371 = 3 + 7 + 1 = 11
Algorithm:
Step 1: Start
Step 2 : Input n
Step 3 : Let sum=0
Step 4 : if n<0 go to step 8
Step 5 : digit = n % 10
Step 6: sum = sum + digit
Step 7 : n = n / 10, go to step 4
Step 8: output sum
Step 9 : Stop

Flow chart:
Start

Read n

sum = 0

No

n >0

digit=n%10
yes

Q22.Write an algorithm and flowchart to find whether a number is Armstrong


or not.
Hint: An integer number for which sum of cubes of its digits is equal to the
number itself.
Example: 371 = 33 + 73 + 13
Algorithm:
Step 1 : Start
Step 2 : Input n
Step 3 : Let sum=0, m = n
Step 4 : if n<0 go to step 8
Step 5 : digit = n % 10
Step 6 : sum = sum + (digit * digit * digit)
Step 7 : n = n / 10 then go to step 4
Step 8 : if sum = m then print “Armstrong number”
else print “ Not Armstrong number”
Step 9 : Stop

Flow chart:
START

READ n

sum=0
m=n

n>0
No No
yes
Yes

No
yes

Q23.Write an algorithm and flowchart to find reverse of a number.


Hint: Ex: Reverse of number 371 is 173
Algorithm:
Step 1: Start
Step 2: Input n
Step 3: Let rev=0
Step 4: if n<=0 go to step 8
Step 5: digit = n % 10
Step 6: rev = (rev * 10) + digit
Step 7: n = n / 10 then go to step 4
Step 8: output rev
Step 9: Stop

Flow chart: Start

READ n

rev=0

No
n>0

digit = n%10
rev=(rev*10)+digit
yes

Q24.Write an algorithm and flowchart to find whether a number is palindrome


or not.
Hint: A number is said to be palindrome if its reverse is equal to the number.
Ex: The reverse of number 12321 is 12321 is a palindrome
Algorithm:
Step 1: Start
Step 2: Input n
Step 3: Let rev=0, m=n
Step 4: if n<=0 go to step 8
Step 5: digit = n % 10
Step 6: rev = (rev * 10) + digit
Step 7: n = n / 10 then go to step 4
Step 8: if m = rev then output “Palindrome number”
else output “Not palindrome number”
Step 9: Stop

Flow chart:

START

READ n

rev=0
m=n

n>0
No

No

Yes

No

Yes

Q25.Write an algorithm and flowchart to find the Average of n numbers


Algorithm :
Step 1 : Start
Step 2 : read n , sum = o
Step 3 : for I = o to n-1 step by 1
3.1 : read a ;
3.2 : sum = sum + a ;
Step 4 : End for too
Step 5 ; Avg = sum/n
Step 6 : Write Avg
Step 7 : Stop

Flowchart :
F

Read n

sum = 0

i++ i=0

i<=n
i<i<=n11

Read a
F

Q26.Write an algorithm and flowchart to find the Prime numbers 1 to n


Algorithm :
Step 1 : Start
Step 2 : read n
Step 3 : for i = 1 to n step by 1
3.1 cnt = 0
3.2 for j = 1 to i step by 1
3.2.1 if i % j = = 0
Cnt ++
3.3 : End for
3.4 : if cnt = 2
3.4.1 : write i
Step 4 : End for
Step 5 : Srop
Flowchart :-

Start

Read n

i++ i=0

i<=n
Ent=0
F T

T
P

A
Write

Q27.Write an algorithm and flowchart to find the Sum of individual digits if a given number
Stop
Algorithm :-
Step 1 : Start
Step 2 : read n
Step 3 : Sum = 0
Step 4 : While n > 0 do
4.1 : r = n % 10
4.2 : Sum = Sum + r
4.3 : n = n/10
Step 5 : End while
Step 6 : Write Sum
Step 7 : Stop
Flowchart :-

Start

Read n
T

Q28.Write an algorithm and flowchart to find the Factorial of a given number

Algorithm :
Step 1 : Start
Step 2 : read n
Step 3 : fact = 1
Step 4 : for i=1 to n step by 1
4.1 : fact = fact * i
Step 5 : End for
Step 6 : Write fact
Step 7 : Stop
Flowchart :-

Start

Read n
F

29. (a) Explain briefly about the History of `C' language

(b) Areas where C language is used and what are the Characteristics of C
language?

1a Ans:

Ken Thompson developed B language then Dennis Ritchie give some advance features,
functionality to B Language for developing C. C language is developed by Dennis Ritchie in
1972. C language depends on ANSI (American National Standard Institute). ANSI is a
standard that is made for every compilers working in same manner. This standard tells what
the language should do and what is happening. Some C programs are not portable such as
if we make it on Microsoft windows then we can’t move it on Apple Macintosh or IBM
operating system.
About C language

• Powerful features as simple syntax, portability and improvability etc.


• C is a case-sensitive language.
• C has 32 keywords.
• C language is widely used in the development of operating systems. An Operating
System (OS) is a software (collection of programs) that controls the various
functions of a computer.
• C is a structured programming language, which means that it allows you to
develop programs using well-defined control structures
• C is often called a middle-level language.

1 (b) Ans:

Areas where C language is used:


Embedded Systems
Computer Graphics
Game Programming
Many others

Characteristics of C language :
It is a portable language. Portable means we can run the programs on verity of computers.
C is a structured language.
C is a small size programming language.
C language has pointer implementation.

2a Ans: Advantages Of C Language

• C is a building block for many other currently known languages. Take a look at
Python for example a fully Object-Oriented High-Level programming language. It is
written in C (perhaps C++ too). That tells you if you ever want to know what is
going on under the hood in other languages; understanding C and how it works is
essential.
• C is a compiled language versus an interpreted language. Explained simply, this means
that the code is compacted into executable instruction (in the case of windows anyway)
rather than being "translated" on the fly at run time. This feature also lends heavily to the
speed of C programs.
• A lot of libraries are written in C.
• The main advantages of C language is that there is not much vocabulary to learn, and that
the programmer can arrange for the program is very fast. C programming language is
very easier to learn. C programming language is still a practical and compact language. It
comprises a good semantic. Syntax is of C is clear also. C language is very near to
assembly programming i.e. the hard use of pointers for example is a very powerful
mechanism.
• C has features that allow the programmer to organize programs in a clear, easy, logical
way. For example, C allows meaningful names for variables without any loss of
efficiency, yet it gives a complete freedom of programming style, including flexible ways
of making decisions, and a set of flexible commands for performing tasks repetitively
(for, while, do).
• C is a portable language.
Disadvantages Of C Language

• C does not have OOPS feature that's why C++ is developed. If you know any other
modern programming language then you already know its disadvantages.
• There is no runtime checking in C language.
• There is no strict type checking (for ex: we can pass an integer value for the floating data
type).
• C doesn't have the concept of namespace.
• C doesn't have the concept of constructors and destructors.

Q30. Give and Explain the structure of a C program?


Fig: Structure of a C program

#include< stdio.h> :

#include :
The ‘#include’ in the first line of the program is called a preprocessor directive. A
preprocessor is a program that processes the C program before the compiler. All the lines in
the C program beginning with a hash (#) sign are processed by the preprocessor.

stdio.h :
stdio.h, stands for "Standard Input/Output Header", ‘stdio.h’ refers to a file supplied
along with the C compiler. It contains ordinary C statements. These statements give
information about many other functions that perform input-output roles.
Library functions are declared in header files.
For example. Stdio.h provide the information to printf() that what should it do.

Global declaration: This section declares some variables that are used in more than one
function. These variables are known as global variables. This section must be declared
outside of all the functions.
main() function :
The next statement is the main() function. As you already know, this is the place where the
execution of the C program begins. Without this function, your C program cannot execute.

Open and closing brasses { } :


Open and closinng brasses tells the starting and ending of the statement.

Declaration part:The declaration part declares all variables that are used in the executable
part. Initialization of variables is also done in this section. Initialization means providing
initial value to the variables.

Executable part or Functions or Statements parts: This part contains the statements
following the declaration of the variables. This part contains a set of statements or a single
statement. These statements are enclosed between the braces.

User Defined Functions: These functions defined by the user are called user_defined functions.
These functions are generally defined after the main () function. They can also be defined before
the main () function.

Comments: Comments are not necessary in the program. However, to understand the flow of
program, programmer can include comments in the program. Comments are to be inserted by the
programmer. These are useful for documentation.

Thee compiler doesn’t execute comments. Thus we can say that comments are not a part of the
executable programs

The user can frequently use any number of comments that can be placed any where in the
program

Example: A simple C program


31. List the basic data types, their sizes and range of values supported by ‘C’

Language? Write about space requirements for variables of different data types.

: DATA TYPES

The C language supports a number of data types, all of which are necessary in writing programs.
Because most CPUs generally support these data types directly, it is unnecessary for the
compiler to convert the data types into the types the CPU understands. In addition to the standard
types, new data types are needed, which are often unique to a given application, and C provides
the mechanisms to create and use types of data created by the programmer.

1. Integer Data Type : Integers are whole numbers with a range of values, Generally an
integer occupies 2
bytes memory space and its value range limited to -32768 to +32768. To control the range
of numbers and
storage space, C has three classes of integer storage namely short int, int and long int. All
three data types
have signed and unsigned forms. Integer Data Types are denoted by int.

Syntax For Integer Data Types :


Syntax : int <variable name>;

For Example :
int num1;
short int num2;
long int num3;

Memory Occupied By Integer :

• short int (1 Byte)


• int (2 Bytes)
• long int (4 Bytes)

2. Floating Point Types : The float data type is used to store fractional numbers (real
numbers).
Floating point numbers are denoted by the keyword float. The double is same as float but
it takes
double space (8 bytes) than float.

Syntax For Floating Point Types :


Syntax : float <variable name>;

For Example :
float num1;
double num2;
long double num3;

Memory Occupied by Float:

• float (4 bytes)
• double (8 bytes)
• long double (10 bytes)

3.Character Types : Character type variable can hold a single character. As there are
singed and
unsigned int (either short or long), in the same way there are signed and unsigned chars;
both occupy
1 byte each, but having different ranges. Unsigned characters have values between 0 and
255, signed
characters have values from –128 to 127. Character is declared by char.

Syntax For Character Types :


Syntax : char <variable name>;

For Example :
char ch='m';
char name='P';

Void Type :
The void type has no values therefore we cannot declare it as variable as we did in case of
integer and
float. Void data type used with function to specify its type. Like in a simple C program we
declared
“main()” as void type because it does not return any value.
DATA
RANGE BYTE FORMAT
TYPES
-128 to + 127
signed char

0 to 255 1 %c
unsigned char
-32768 to +32767 1 %c
short signed
int
0 to 65535 2 %d
short unsigned
int -32768 to +32767 2 %u

0 to 65535 2 %d
signed int

-2147483648 to 2 %u
unsigned int
+2147483647
4 %ld
long signed int
0 to 4294967295
4 %lu
long unsigned
int -3.4e38 to
+3.4e38 4 %f
float
-1.7e308 to 8 %lf
+1.7e308
double
10 %Lf
-1.7e4932 to
long double
+1.7e4932
. Differentiate between the following with example: (5+5+5)

A) Long Integer and Short Integer

B) Unsigned Integer and Signed Integer

C) Signed And Unsigned Char

Long Integer

long integer is a data type that can represent a whole number whose range is greater than
or equal to that of a standard integer on the same machine. The range of long integer is -
2147483648 to 2147483647.
Declaration of long int :
Syntax :
long int color;
OR
long color;

Short Integer
The range of short int is -32768 to -32767.'short int' should assign less than or the same
amount of storage as an 'int' and the 'int' should be less or the same bytes than a 'long int'.
short int must be at least 16 bits long.
Declaration of short int :
short int yellow;

Unsigned Integer

unsigned int has a range of 0 - 65535. "int" can be negative, but "unsigned int" cannot
be negative. %u Prints an unsigned integer. The size can range from 2 to 8, or (again)
whatever the implementation provides.

Declaration of unsigned integer :


unsigned int x;
unsigned char grey;

Signed Integer

%d Prints a signed integer. Range of signed int is -32768 to 32767.


Declaration of signed integer :
signed int y;
signed char white;

Signed And Unsigned Char In C

C allows the character type char to be signed or unsigned, depending on the platform and
compiler. With an unsigned char, the variable c takes the value 255, but with a signed char
it becomes -1. If the signed or unsigned version of char is explicitly required at certain
points in a program, it can be specified using the declarations signed char or unsigned char.
Declaration of signed and unsigned char :
unsigned char grey;
signed char white;

32. (a) Describe the different types of constants in C with example? (8)

(b) What are the rules for creating C constants explain with example? (7)

Constants in C refer to fixed values that donot change during the execution of a program. C
supports several types of constants as:

TYPES OF C CONSTANTS
1. Integer constants
2. Real constants
3. Character constants
4. String constants
1. Integer constants: An integer constant refers to a sequence of digits

Examples of Integer Constant :


426
+786
-7605 etc
2. Real constants: These quantities are represented by numbers containing fractional parts
like 18.234. Such numbers are called real(or floating point)constants

Examples Of Real Constants :


+325.34
426.0
-32.67 etc.
The exponential form of representation of real constants is usually used if the value
of the constant is either too small or too large. In exponential form of representation
the Real Constant is represented in two parts. The first part present before 'e' is
called Mantissa and the part following 'e' is called Exponent.
For ex. .000342 can be written in Exponential form as 3.42e-4.

3. Character constants: Asingle character constant contains a single character enclosed within a pair of
single quote marks.

o For ex. 'A',’5’,’;’,’ ‘

Note that the character constant’5’ is not same as the number 5. The last constant is a blank space.

Character constant have integer values known as ASCII values. For example, the statement

Printf(“%d”,a); would print the number 97,the ASCII value of the letter a. Similarly,

the statement printf(“%c”,97); would output the letter ‘a’

String constants: A string constant is a sequence of character enclosed in double quotes. the characters
may be letters, numbers, special characters and balnk space.

Examples are:

“HELLO!”

“1979”

“welcome”

“?.......!”

“5+3”
“X”

5b Ans:

RULES OF CONSTRUCTING INTEGER CONSTANTS


a. an integer constant must have at least one digit.
b. It must not have a decimal point.
c. It can be either positive or negative.
d. The allowable range for constants is -32768 to 32767
In fact the range of integer constants depends upon compiler.
For ex. 435
+786
-7000
RULES OF CONSTRUCTING REAL CONSTANTS
a. A real constants must have at least one digit
b. it must have a decimal point.
c. it could be either positive or negative.
d. default sign is positive.
For ex. +325.34
426.0
In exponential form of representation, the real constants is represented in two parts. The
part appearing before ‘e’ is called mantissa where as the part following ‘e’ is called
exponent.
Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.
Ex. +3.2e-5
RULES OF CONSTRUCTING CHARACTER CONSTANTS
a. a character constant is a single alphabet, a single digit or a single special symbol
enclosed within single inverted commas.
b. The maximum length of character constant can be one character.
Ex `A`

33. (a) What is an identifier? Explain with example? (5)

(b) What are the Escape sequences? List with their uses. (5)

6a Ans:

An identifier is a word formed with characters.


C language has two types of identifiers.
Standard identifier: these are also called as indulged identifier (or) keywords
User defined identifier: The identifiers which are defined by the users are called as user defined
identifiers.
A user defined identifier may be a constants name, a variable a data type (or) even a programme
name.
While defining these identifiers the user has to accept the following rules.
• These identifiers should start with an alphabet with an unscroll symbols(_)
• Maximum number of characters in an identifier is 31.
• No special symbol is allowed in an identifier
• No keyword is allowed as a user defined identifier.
Eg: abc1
Hello
_defined

6b Ans:

Escape sequences

Certain ASCII characters are unprintable, which means they are not displayed on the screen or
printer. Those characters perform other functions aside from displaying text. Escape sequences usually
consist of a backslash and a letter or a combination of a digit. An escape sequence is regarded as a single
character and is therefore valid as a character constant. Escape sequences are typically used to specify
actions such as carriage return and tab movements on terminals and printers. The commonly used
escape sequences are listed below.

Escape sequences Represents

\a Bell (alert)

\b Backspace

\f Form feed

\n new line

\r Carriage Return

\t Horizontal tab

\v Vertical tab

\’ Single quotation Mark

\’’ Double Quotation Mark

\? Literal Quotation Mark

\\ Backslash

\0 Null
34. (a) Program for converting uppercase character into lowercase letter? (5)
(b) Write a program to demonstrating operator’s precedence in expression in evaluation x=a-b/3+c*2-
1. (5)

#include<stdio.h>

#include<conio.h>

main()

int c;

clrscr();

printf(“Enter an upper case letter”);

c=getchar();

printf(“Lowercase=”);

putchar(c+32);

getch();

7b Ans: //program to demonstrating operator’s precedence in expression in evaluation

#include<stdio.h>

#include<conio.h>

void mian()

{ int a=9,b=12,c=3,x;

clrscr();

x=a-b/3+c*2-1; /* expression evaluation */

printf(“value of x=%d”,x); // printing the resultant value

getch();

}
35. (a) Write a program to demonstrate the use of size of operators (5)
(b)Define an expression? How can you evaluate an expression? (5)

8a Ans: /* program for demonstrate ting the use of size of operators*/

#include<stdio.h>

#include<conio.h>

Void main()

Char a;

Int b;

flaot c;

long d;

clrscr();

// using the size of operators to compute the size of different data types

printf(“Size of char a=%u”,sizeof(a));

printf(“\n Size of flaot c=%u”,sizeo(c));

printf(“\n Size of int b=%u”,sizeof(b);

printf(“\n Size of long d=%u” size of(d));

getch();

8b Ans:

An expression in C is some combination of constants, variables, operators

and function calls.

Sample expressions are:

a+b

3.0*x - 9.66553

tan(angle)
• Most expressions have a value based on their contents.

• A statement in C is just an expression terminated with a semicolon.

For example:

sum = x + y + z;

printf("Go Buckeyes!");

the rules given below are used to evaluate an expression,

1)If an expression has parenthesis, sub expression within the parenthesis is evaluated
first and arithmetic expression without parenthesis is evaluated first.

2)the operators high level precedence are evaluated first.

3)the operators at same precedence are evaluated from left to right or right to left depending
on associativity of operators

36. (a) What is a variable? and write the rules for constructing variable?

(b) Differentiate between definition and declaration of variables in ‘C’ with example? (5)

A variable is a data name that may be used to store a data value. Unlike constants that remain
unchanged during the execution of a program, a variable may take different times during execution.

A variable name can be chosen by the programmer in a meaningful way so as to reflect its function or
nature in the program.

Rules For Constructing Variable Names In C

o A Variable name is any combination of 1 to 31 alphabets, digits or


underscore.
o The first character in variable name must be an alphabet or underscore.
o The variable name should not be a keyword.
o White space is not allowed.
o No commas or blanks are allowed within a variable name.
o No special symbol other than an underscore can be in a variable name.
For Example :
si_int;
m_hra; etc

Average

Height
Total

Counter_1

Class_strength

o The maximum allowable length of a variable name is 31 character.

9b Ans:

DIFFERENCE BETWEEN DEFINITION AND DECLARATION

Both objects and functions can be defined and declared. When we declare a variable or
object then its attribute is known to the compiler. When we define a object then the
attribute is known to the compiler and the object is also be created.

Whenever a variable is defined within the function then it has local scope (local scope
means that variable con only access locally). While when a variable is defined outside the
function then it has global scope (global scope means we can access the variable globally or
anywhere).

Declaration does two things:

1. It tells the compiler what the variable name is.


2. It specifies what type of data the variable will hold.

The declaration of variables must be done before they are used in the program.

The syntax for declaring a variable is as follows:

Data-type v1,v2,…….,vn;

V1,v2,…vn are the names of variables. Variables are separated by commas. A


declaration statement must end with a semicolon. For example , valid declarations are:

int count;

int number, total;

double ratio;

The simplest declaration of a variable is shown in the following code fragment:

Ex:

Main()/*…………….Program Name……………..*/

{
/*……………….Declaration…………………………..*/

Float x,y;

Int code;

Short int count;

Long int amount;

Double deviation;

Unsigned n;

Char c;

/*…………………………Computation…………………………*/

…………………

……………………

………………………

}/*…………………………Program ends…………………..*/

Fig: Declaration of variables

(OR)

Example of definition and declaration :

void fun(int num)


{

int n; // Declaration of variable


n= num; // Definition of variable

}
Here we are passing integer type parameter to fun function and we are also declaring n as
integer which is known
by compiler that is called Declaration and are assigning the value num to n is
called definition.

Example For Global Scope Of Variable :


int num;
char letter; // int num and char are global variables
main()
{
num=23;
letter=’A’;
}
Example For Local Scope Of Variable :
main()
{
int num; // int num and char ch are local variables
char ch;
num=2;
ch=’a’;
}

37. (a) Explain the variable initialization and scope and life span of a variable with example? (8)

(b) What is an operator and List different categories of C operators based on their functionality?
Give examples? (7)

Initialization of variable :
Initialize a variable in c to assign it a starting value. Without this we can't get whatever
happened to memory at that moment.
C does not initialize variables automatically. So if you do not initialize them properly, you
can get unexpected results. Fortunately, C makes it easy to initialize variables when you
declare them.
For Example :

int x=45;

int month_lengths[] = {23,34,43,56,32,12,24};

struct role = { "Hamlet", 7, FALSE, "Prince of Denmark ", "Kenneth Branagh"};

Note : The initialization of variable is a good process in programming.

Scope And Life Span Of A Variable :

The area of the program where that variable is valid, the amount of time that a variable is
retained, as well as where it can be accessed from, depends on its specified location and
type. The life span of that variable, i.e. the length of time that the variable remains in
memory.
10b Ans:

What is an operator:
If we take an expression 9+5=14, here 9 and 5 are operands and + is an operator.

Types of operator :
Arithmetic Operators
Logical Operators
Assignment Operators
Bitwise Operator
Misc Operator(sizeof(), *(Pointer), &(address)
)

Arithmetic Operators:
The arithmetic operators are +, -, *, %, /, ++, --
For ex. If a variable A=10 then A++=11 and A--=9

Logical (or Relational) Operators :


==, !=, >, <, &&, ||, <=, >= are the logical operators.
For Example. If variable A =10 and B=23 then (A=B) is false, (A<B) is true etc.

Bitwise Operators: Bitwise operators works on bits and perform bit by bit operation.
Assume if A = 60; and B = 13; Now in binary format they will be as follows:
A = 0011 1100 and B = 0000 1101
A&B = 0000 1000

Assignment Operators :
=, +=, -=, /=, %= &=, |= etc are the assignment operators.
For Example. C = A + B will assign value of A + B into C and C+= A means C=C+A.

Misc Operators : These are some special kind of operators such as sizeof(), *(Pointer),
&(address).
For Example. sizeof (a) means that If a is integer then it returns value 4.
*a means a will be a pointer to 'a' variable.

38. Explain the hierarchy (priority) and associativity(clubbing)of operators in ‘C’ with
example? (15)

Hierarchy Of Operations In C
There are some operators which are given bellow with their mean. The higher the position
of an operator is, higher is its priority.
Operator Type

! Logical NOT

*/ % Arithmetic and modulus

+- Arithmetic

<> <=>= Relational

==!= Relational

&& Logical AND

|| Logical OR

= Assignment

ASSOCIATIVITY OF OPERATOR

When an expression contains two operators of equal priority the tie between them is settled
using the associatively of the operators.

Associatively can be of two types—Left to Right or Right to Left.


Left to Right associatively means that the left operand must be unambiguous. Unambiguous
in what sense? It must not be involved in evaluation of any other sub-expression. Similarly,
in case of Right to Left associatively the right operand must be unambiguous. Let us
understand this with an example.
Consider the expression
a=3/2*5;
Here there is a tie between operators of same priority, that is between / and *. This tie is
settled using the associatively of / and *. But both enjoy Left to Right associatively.

While executing an arithmetic statement, which has two or more operators, we may have
some problem as to how exactly does it get executed.

Priority Operators Description

1st *, / , % multiplication,
division,
modular
division

addition,
2nd +, -
subtraction

3rd = assignment

For Example :
i= 2*3/4+4/4+8-2+5/8
i=6/4+4+8-2+5/8
i=1+4/4+8-2+5/8
i=1+1+8-2+5/8
i=1+1+8-2+0
i=2+8-2+0
i=10-2+0
i=8+0
i=8

You might also like