[go: up one dir, main page]

0% found this document useful (0 votes)
6 views26 pages

unit 1

The document provides an introduction to computers, including their definition, characteristics, and a brief history. It outlines the generations of computers, types of computers, and the basic organization of a computer, detailing functional units like the ALU, memory, and input-output units. Additionally, it discusses programming languages, algorithms, flowcharts, pseudocode, and the compilation process.

Uploaded by

abrarhabib75
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)
6 views26 pages

unit 1

The document provides an introduction to computers, including their definition, characteristics, and a brief history. It outlines the generations of computers, types of computers, and the basic organization of a computer, detailing functional units like the ALU, memory, and input-output units. Additionally, it discusses programming languages, algorithms, flowcharts, pseudocode, and the compilation process.

Uploaded by

abrarhabib75
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/ 26

1

UNIT 1

INTRODUCTION TO PROGRAMMING AND PROBLEM SOLVING

Q 1. Defini on of Computer?

• Computer is a programmable machine.

• Computer is a machine that manipulates data according to a list of instruc ons.

• Computer is any device which aids humans in performing various kinds of computa ons or Calcula ons

Q 2. Explain Characteris c of Computer?


Three principles characteris c of computer:

• It responds to a specific set of instruc ons in a well-defined manner.

• It can execute a pre-recorded list of instruc ons.

• It can quickly store and retrieve large amounts of data.

Q 3. Describe brief history of Computers?

Earliest Computer

• Originally calcula ons were computed by humans, whose job tle was computers.

• These human computers were typically engaged in the calcula on of a mathema cal expression.

• The calcula ons of this period were specialized and expensive, requiring years of training in mathema cs.

• The first use of the word "computer" was recorded in 1613, referring to a person who carried out calcula ons, or
computa ons, and the word con nued to be used in that sense un l the middle of the 20th century.

Tally S cks A tally s ck was an ancient memory aid device to record and document numbers, quan es, or
even messages.

Abacus

• An abacus is a mechanical device used to aid an individual in performing mathema cal calcula ons.

• The abacus was invented in Babylonia in 2400 B.C.

• The abacus in the form we are most familiar with was first used in China in around 500 B.C.

• It used to perform basic arithme c opera ons.

Napier’s Bones

• Invented by John Napier in 1614.

• Allowed the operator to mul ply, divide and calculate square and cube roots by moving the rods around
and placing them in specially constructed boards.

Slide Rule

• Invented by William Oughtred in 1622.


2

• Is based on Napier's ideas about logarithms.

• Used primarily for

– mul plica on
– division
– roots
– logarithms
– Trigonometry
• Not normally used for addi on or subtrac on.

Pascaline

• Invented by Blaise Pascal in 1642.

• It was its limita on to addi on and subtrac on.

• It is too expensive.

Arithmometer

• A mechanical calculator invented by Thomas de Colmar in 1820,

• The first reliable, useful and commercially successful calcula ng machine.

• The machine could perform the four basic mathema c func ons.

• The first mass-produced calcula ng machine.

Difference Engine and Analy cal Engine

• It an automa c, mechanical calculator designed to tabulate polynomial func ons.

• Invented by Charles Babbage in 1822 and 1834

• It is the first mechanical computer.

First Computer Programmer

• In 1840, Augusta Ada Byron suggests to Babbage that he use the binary system.

• She writes programs for the Analy cal Engine.

ENIAC

• ENIAC stands for Electronic Numerical Integrator and Computer.

• It was the first electronic general-purpose computer.

• Completed in 1946.

• Developed by John Presper Eckert and John W. Mauchl.

UNIVAC 1

• The UNIVAC I (UNIVersal Automa c Computer 1) was the first commercial computer.
3

• Designed by J. Presper Eckert and John Mauchly.

EDVAC

• EDVAC stands for Electronic Discrete Variable Automa c Computer

• The First Stored Program Computer

• Designed by Von Neumann in 1952.

• It has a memory to hold both a stored program as well as data.

The First Portable Computer

• Osborne 1 – the first portable computer.

• Released in 1981 by the Osborne Computer Corpora on.

Q 4. Explain Genera ons of Computer in detail ?

There are five genera ons of computer:

• First genera on – 1946 - 1958

• Second genera on – 1959 - 1964

• Third genera on – 1965 - 1970

• Fourth genera on – 1971 - today

• Fi h genera on – Today to future

The First Genera on

• The first computers used vacuum tubes for circuitry and magne c drums for memory, and were o en enormous,
taking up en re rooms.

• They were very expensive to operate and in addi on to using a great deal of electricity, generated a lot of heat,
which was o en the cause of malfunc ons.

• First genera on computers relied on machine language, the lowest-level programming language understood by
computers, to perform opera ons, and they could only solve one problem at a me.

• Input was based on punched cards and paper tape, and output was displayed on printouts.

The Second Genera on

• Transistors replaced vacuum tubes and ushered in the second genera on of computers.

• One transistor replaced the equivalent of 40 vacuum tubes.

• Allowing computers to become smaller, faster, cheaper, more energy-efficient and more reliable.
4

• S ll generated a great deal of heat that can damage the computer.

• Second-genera on computers moved from cryp c binary machine language to symbolic, or assembly, languages,
which allowed programmers to specify instruc ons in words.

• Second-genera on computers s ll relied on punched cards for input and printouts for output.

• These were also the first computers that stored their instruc ons in their memory, which moved from a magne c
drum to magne c core technology.

The Third Genera on

• The development of the integrated circuit was the hallmark of the third genera on of computers.

• Transistors were miniaturized and placed on silicon chips, called semiconductors, which dras cally increased the
speed and efficiency of computers.

• Much smaller and cheaper compare to the second genera on computers.

• It could carry out instruc ons in billionths of a second.

• Users interacted with third genera on computers through keyboards and monitors and interfaced with an
opera ng system, which allowed the device to run many different applica ons at one me with a central program
that monitored the memory.

• Computers for the first me became accessible to a mass audience because they were smaller and cheaper than
their predecessors.

The Fourth Genera on

• The microprocessor brought the fourth genera on of computers, as thousands of integrated circuits

were built onto a single silicon chip.

• As these small computers became more powerful, they could be linked together to form networks, which

eventually led to the development of the Internet.

• Fourth genera on computers also saw the development of GUIs, the mouse and handheld devices.

The Fi h Genera on

• Based on Ar ficial Intelligence (AI).

• S ll in development.

• The use of parallel processing and superconductors is helping to make ar ficial intelligence a reality.

• The goal is to develop devices that respond to natural language input and are capable of learning and self-
organiza on.

• There are some applica ons, such as voice recogni on, that are being used today.
5

Q 5. What is a Computer? Explain Different types of Computers?

A computer can be defined as a fast electronic calcula ng machine that accepts the (data) digi zed input informa on
process it as per the list of internally stored instruc ons and produces the resul ng informa on.

List of instruc ons are called programs & internal storage is called computer memory.

The different types of computers are

1. Personal computers: - This is the most common type found in homes, schools, Business offices etc., It is
the most common type of desk top computers with processing and storage units along with various input and
output devices.

2. Note book computers: - These are compact and portable versions of PC

3. Work sta ons: - These have high resolu on input/output (I/O) graphics capability, but with same
dimensions as that of desktop computer. These are used in engineering applica ons of interac ve design
work.

4. Enterprise systems: - These are used for business data processing in medium to large corpora ons that
require much more compu ng power and storage capacity than work sta ons. Internet associated with
servers have become a dominant worldwide source of all types of informa on.

5. Super computers: - These are used for large scale numerical calcula ons required in the applica ons like
weather forecas ng etc.,

Q 6. Explain Basic Organiza on of Computer? Describe briefly about Func onal units?

A computer consists of five func onally independent main parts input, memory, arithme c logic unit (ALU), output
and control unit.

ALU stands for arithmetic logic unit. It is responsible for performing all arithmetic and logical operations, such as
addition, subtraction, multiplication, division, AND, OR, and NOT.
Input-output units allow the computer to communicate with the outside world. They include devices such as the
keyboard, mouse, monitor, printer, and speakers.
Memory is where the computer stores data and instructions.
It is divided into two main types:
1. Random Access Memory (RAM) And
2. Read-Only Memory (ROM).
 RAM is used to store data and instructions that are currently being used by the CPU.
 ROM is used to store permanent data, such as the computer's startup instructions.
Program counter is a register that keeps track of the address of the next instruction to be executed by the CPU.
These four components work together to execute computer programs. The CPU first fetches the next instruction from
memory using the program counter. It then decodes the instruction and determines what operation needs to be
6

performed. The ALU then performs the operation and stores the result in memory. Finally, the program counter is
incremented to point to the next instruction.
Here is a simplified example of how the ALU, input-output units, memory, and program counter work together to
execute a simple program:
Program:
1. ADD 1 to A
2. PRINT A
Execution:
1. The CPU fetches the first instruction from memory (ADD 1 to A).
2. The CPU decodes the instruction and determines that it needs to perform an addition operation.
3. The CPU loads the operand A from memory.
4. The ALU adds 1 to A and stores the result back in memory.
5. The program counter is incremented to point to the next instruction.
6. The CPU fetches the next instruction from memory (PRINT A).
7. The CPU decodes the instruction and determines that it needs to print the value of A to the console.
8. The CPU loads the value of A from memory.
9. The CPU writes the value of A to the console.
10. The program counter is incremented to point to the next instruction, which is the end of the program.

Q 7. Define Programming Language? Summarize the classifica on of Programming languages?

 A programming language is a set of symbols, grammars and rules with the help of which one is able to translate
algorithms to programs that will be executed by the computer.
 The programmer communicates with a machine using programming languages.
 Most of the programs have a highly structured set of rules.

The primary classifica ons of programming languages are:

1. Machine Languages
2. Assembly Languages
3. High level Languages.

Machine Language

 Machine language is a collec on of binary digits or bits that the computer reads and interprets.
 Machine language is the only language a computer is capable of understanding.
 Machine level language is a language that supports the machine side of the programming or does not provide
human side of the programming.
 It consists of (binary) zeros and ones.
 Each instruc on in a program is represented by a numeric code, and numerical addresses are used
throughout the program to refer to memory loca ons in the computer’s memory.
 Microcode allows for the expression of some of the more powerful machine level instruc ons in terms of a
set of basic machine instruc ons.

Assembly language

 Assembly language is easier to use than machine language.


7

 An assembler is useful for detec ng programming errors.


 Programmers do not have the absolute address of data items.
 Assembly language encourage modular programming

High level language

 High level language is a language that supports the human and the applica on sides of the programming.
 A language is a machine independent way to specify the sequence of opera ons necessary to accomplish a
task.
 A line in a high level language can execute powerful opera ons, and correspond to tens, or hundreds, of
instruc ons at the machine level.
 Consequently, more programming is now done in high level languages.
 Examples of high level languages are BASIC, FORTRAN etc

Q 8. Define algorithm. List out its proper es.

Algorithm:

Algorithm is a step by step process to solve a problem.

OR

A procedure for solving a mathema cal problem in a finite number of steps that frequently involves recursive
opera ons

Proper es of Algorithm:

1. Input: An algorithm must accept zero or more input.

2. Output: An algorithm must produce at least one output.

3. Finiteness: An algorithm must be terminated at finite number of steps.

4. Definiteness: Every step of algorithm must be clear and unambiguous.

5. Effec veness: Every step must be basic and easy to convert into program.

Example:

Write an algorithm to perform addi on of two numbers

Algorithm:

Step-1: Start

Step-2: Read a number into A

Step-3: Read another number into B

Step-4: Perform addi on and store result in total


8

Step-5: Display total value

Step-6: Stop

Q 9. What is FlowChart?

 A flowchart is a type of diagram that represents a workflow or process.


 A flowchart can also be defined as a diagramma c representa on of an algorithm,
 A step-by-step approach to solving a task.

Q 10. Define flowchart. List the different symbols used to draw a flowchart ?

Pictorial representa on of algorithm is known as flowchart.

Symbols used in flowchart:

Q 11. What Is Pseudo-Code in C?

Programming language descrip on of algorithm is called as pseudocode

OR
9

 The pseudocode in C is an informal way of wri ng a program for be er human understanding.


 It is wri en in simple English, making the complex program easier to understand.
 Pseudocode cannot be compiled or interpreted.
 It doesn't follow the programming language's syntax;
 It is thus wri en in pseudocode so that any programmers or non-programmers can easily understand it.

Example Pseudo code:

1. Start
2. Input two numbers
3. Calculate the sum of the two numbers
4. Divide the sum by 2 to get the average
5. Display the average
6. End

Q 12. What is a Complier ? Explain about Compila on process?

 The compiler program translates the instruc ons of a high level language to a machine level language.
 A separate compiler is required for every high level language.
 High level language is simply a programmer’s convenience and cannot be executed in their source.
 The actual high - level program is called a source program.
 It is compiled (translated) to machine level language program called object program for that machine by the
compiler.
 Such compilers are called self-resident compilers.
 Compiler compiles the full program and reports the errors at the end

Compila on Process

The compila on and execu on process of C can be divided in to mul ple steps:
10

Preprocessing Using a Preprocessor program to convert C source code in expanded source code. "#include"
and "#define" statements will be processed and replaced actually source codes in this step.

Compila on Using a Compiler program to convert C expanded source to assembly source code.

Assembly Using a Assembler program to convert assembly source code to object code.

Linking Using a Linker program to convert object code to executable code. Mul ple units of object codes are
linked to together in this step.

Loading Using a Loader program to load the executable code into CPU for execu on. Compila on

LINKING

 A er all of the files are compiled, they must be "merged together" to produce a single executable file that the
user use to run the program.
 In C, most compiled programs produce results only with the help of some standard programs, known as library
files that reside in the computer.
 This process is called linking. The result obtained a er linking is called the executable file.
 The linker′s primary func on is to bind symbolic names to memory addresses.
 To do this, it first scans the files and concatenates the related file sec ons to form one large file.
 Then, it makes a second pass on the resul ng file to bind symbol names to real memory addresses.
 Loading is loading the executable into memory prior to execu on.
 There are two types of linking: Sta c linking. Dynamic linking.
o Sta c linking occurs at compila on me; hence it occurs prior to loading a program.
o With sta c linking the external symbols that are used by the program (e.g. func on names) are resolved
at compile me.
o Dynamic linking occurs at run me, so it occurs a er or at the me of the loading of a program.
o With dynamic linking the symbols are resolved either at loading me, or at run me when the symbol
is accessed (lazy binding).

LOADING

 A er the files are compiled and linked the executable file is loaded in the computer′s memory for execu ng by
the loader. This process is called Loading.
 Program loading is basically copying a program from secondary storage into main memory so it ′s ready to run.
 In some cases, loading us just not copying the data from disk to memory, but also se ng protec on bits, or
arranging for virtual memory map virtual addresses to disk pages.
11

Q13. Explain execu on process of a c program.

 The file which contains c program instruc ons in a high-level language is said to be source code.
 Every c program source file is saved with .c extension, for example, Sample.c.
 Whenever we press compila on bu on the source file is submi ed to the compiler.
 Compiler checks for the errors, if there are any errors, it returns a list of errors, otherwise generates object
code in a file with name Sample.obj and submit it to the linker.
 The linker combines the code from specified header file into an object file and generates executable file as
Sample.exe.
 With this compila on process completes. Now, we need to run the executable file (Sample.exe).
 To run a program, we press run bu on.
 When we press run bu on the executable file is submi ed to the CPU.
 Then CPU performs the task according to the instruc ons wri en in that program and place the result into
User Screen.

Q 14. Explain in detail about structure of C Program.

 C is a structured programming language. Every C program and its statements must be in a par cular structure.
 Every C program has the following general structure.
12

1. Documenta on Sec on:


 This sec on is used to provide a small descrip on of the program.
 The comment lines are simply ignored by the compiler, that means they are not executed.
 In C, there are two types of comments.
1. Single Line Comments: Single line comment begins with // symbol. We can write any number of single line
comments.
2. Mul ple Lines Comments: Mul ple lines comment begins with /* symbol and ends with */. We can write
any number of mul ple lines comments in a program.
All the comment lines in a C program just provide the guidelines to understand the program and its code.
2. Pre-processing Commands:
 Pre-processing commands are used to include header files and to define constants.
 Pre-processing commands are beginning with # symbol.
 C Pre-processing commands are: #include, #define, #if, #else etc…,
3. Global Declara ons:
 The global declara on is used to define the global variables, which are common for all the func ons a er
its declara on.
 We also use the global declara on to declare func ons.
4. Main Func on:
 Every C program must contain this main func on.
 Here, main ( ) is a user-defined func on which tells the compiler that this is the star ng point of the
program execu on.
 The open brace ( { ) indicates the beginning of the main func on.
 Local declara on contains local variables which are used with in main ( ) func on.
 Executable statements perform tasks like reading data, displaying the result, calcula ons, etc.,
 Closing Brace ( } )indicates the end of the main ( ) func on.
 The statement return 0 returns a value zero to the Opera ng System a er comple ng the main ( )
execu on.
 If we don't want to return any value, we can use it as void.

5. User Defined Func ons:

 This is the place where we implement the user-defined func ons.

Q 15. Define Data Type. List out different Data Types in C.

 A data type specifies the type of data that a variable can store such as integer, floa ng, character, etc.
 There are the following data types in C language.
13

Q 16. Give brief discussion on primi ve data types.


 The primary data types in the C programming language are the basic data types.
 Primary data types are also called as Built-In data types.
 The following are the primary data types in c programming language.

1. Integer:

 The integer data type is a set of whole numbers.


 Every integer value does not have the decimal value.
 We use the keyword "int" to represent integer data type in C.
 The following table provides complete details about the integer data type according to the 32-bit
architecture.
14

2. Floa ng Point:

 Floa ng-point data types are a set of numbers with the decimal value.
 We use the keyword "float" to represent floa ng-point data type and "double" to represent double data type
in C.
 The following table provides complete details about floa ng-point data types.

3. Character:

 The character data type is a set of characters enclosed in single quota ons.
 The following table provides complete details about the character data type.

Q 17. Write short note on format specifiers in C.

 The format specifiers are used in C for input and output


purposes.
 Using this concept, the compiler can understand that what
type of data will be taking input using the scanf() func on
and prin ng using prin () func on.
 Here is a list of format specifiers
 The Most commonly used format specifiers
15

Q 18. Define variable. Explain how to declare and ini alize a variable in C.

Variable:

A variable is a name of the memory loca on which can store a value.

Declara on:

Declara on of a variable tells the compiler to allocate the required amount of memory with the specified
variable name and allows only specified datatype values into that memory loca on.

We can declare a variable using below syntax.

Syntax:

Example:

int number;

The above declara on tells to the compiler that allocates 2 bytes of memory with the name number and allows only
integer values into that memory loca on.

Ini aliza on:

 Ini aliza on of a variable means assigning a value to the variable.


 We can ini alize a variable with a value using below syntax.

Q 19. What are the rules to define a variable?

Rules to define Iden fiers:

1. Variables can be a combina on of le ers (a to z/ A to Z) or digits (0 to 9) or an underscore ( _ ).

Example: myClass, var_1, print_this_to_screen, _number are valid variables.

2. A variable can start with a le er or an underscore ( _ ), but not with a digit.

Example: 1_variable is invalid

Variable_1 is Valid
16

3. Keywords cannot be used as variables.

Example: if is invalid variable

4. Special symbols like !, @, #, $, % etc. are not allowed in variables except one special symbol underscore ( _ ).

Example: company#name, $name, email@id are invalid variables

Q 20. Define Constant in C? Explain various types of constants in C?

A constant is a value assigned to the variable which will cannot be changed.

There are two ways of declaring constant:

1. Using const keyword

2. Using #define pre-processor


17

Q 21. Explain about Basic Input and Output forma ed func on in C?


 C language has standard libraries that allow input and output in a program.
 The stdio.h or standard input output library in C that has methods for input and output.
Forma ed Input:
scanf()
The scanf() method, in C, reads the value from the console as per the type specified.
Syntax:
scanf(“%X”, &variableOfXType);
Explana on:
where %X is the format specifier in C. It is a way to tell the compiler what type of data is in a variable and & is
the address operator in C, which tells the compiler to change the real value of this variable, stored at this address in
the memory.
Forma ed Output:
prin ()
The prin () method, in C, prints the value passed as the parameter to it, on the console screen.
Syntax:
prin (“%X”, variableOfXType);
Explana on:
where %X is the format specifier in C. It is a way to tell the compiler what type of data is in a variable and & is
the address operator in C, which tells the compiler to change the real value of this variable, stored at this address in
the memory.
To take input and output of basic types in C?
The basic type in C includes types like int, float, char, etc. In order to input or output the specific type, the X in
the above syntax is changed with the specific format specifier of that type. The Syntax for input and output for these
are:
Example for Integer input and output:
Input: scanf("%d", &intVariable);
Output: prin ("%d", intVariable);
Q 22. Define Operator. Explain different operators in C.
Operator:
Operator is symbol is used perform opera on in between operands.

Different types of Operators: C operators are classified as:

1. Arithme c Operators

2. Rela onal Operators

3. Logical Operators

4. Assignment Operator
18

5. Increment & Decrement Operators

6. Bitwise Operators

7. Condi onal Operator

1. Arithme c Operators:

 The arithme c operators are used to perform basic mathema cal opera ons like addi on, subtrac on,
mul plica on, division and percentage modulo.
 The following table provides informa on about arithme c operators.

Example Program:

// C program to illustrate the arithmatic operators


#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a % b = %d\n", a % b);
return 0;
}
Output:
a + b = 30
a - b = 20
a * b = 125
a/b=5
a%b=0

2. Rela onal Operators:


19

 The rela onal operators are used to check the rela onship between two values.
 Every rela onal operator has two results TRUE or FALSE.
 In simple words, the rela onal operators are used to define condi ons in a program.
 The following table provides informa on about rela onal operators.

Example Program:

// C program to illustrate the relational operators


#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("a < b : %d\n", a < b);
printf("a > b : %d\n", a > b);
printf("a <= b: %d\n", a <= b);
printf("a >= b: %d\n", a >= b);
printf("a == b: %d\n", a == b);
printf("a != b : %d\n", a != b);
return 0;
}

Output:

a<b :0
a>b :1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1
3. Logical Operators:

 The logical operators are used to check logical rela onship between two condi ons.
 The following table provides informa on about logical operators.
20

Example Program:

// C program to illustrate the logical operators


#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("a && b : %d\n", a && b);
printf("a || b : %d\n", a || b);
printf("!a: %d\n", !a);
return 0;
}
Output:
a && b : 1
a || b : 1
!a: 0

4. Assignment Operator:

 Assignment operator is used to assigning values to the variables.


Example:
a= 5

Shorthand Assignment Operators:

The following table illustrates different types of shorthand operators.

Example Program:

// C program to illustrate the assignment operators


#include <stdio.h>
int main()
{
int a = 25, b = 5;
21

// using operators and printing results


printf("a = b: %d\n", a = b);
printf("a += b: %d\n", a += b);
printf("a -= b: %d\n", a -= b);
printf("a *= b: %d\n", a *= b);
printf("a /= b: %d\n", a /= b);
printf("a %%= b: %d\n", a %= b);
return 0;
}
Output:
a = b: 5
a += b: 10
a -= b: 5
a *= b: 25
a /= b: 5
a %= b: 0

5. Increment & Decrement Operators:

 Increment operator is used to increase the value of variable by one.


 Decrement operator is used to decrease the value of variable by one.
 We can use these operators before or a er variable name.
 The below table illustrates the different increment and decrement operators.

Example Program:

// C program to illustrate the arithmatic operators


#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("+a = %d\n", +a);
printf("-a = %d\n", -a);
printf("a++ = %d\n", a++);
printf("a-- = %d\n", a--);
return 0;
}
22

Output:
+a = 25
-a = -25
a++ = 25
a-- = 26

6. Bitwise Operators:

 The bitwise operators are used to perform bit-level opera ons in the C.
 When we use the bitwise operators, the opera ons are performed based on the binary values.

Example Program:

// C program to illustrate the bitwise operators


#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("a & b: %d\n", a & b);
printf("a | b: %d\n", a | b);
23

printf("a ^ b: %d\n", a ^ b);


printf("~a: %d\n", ~a);
printf("a >> b: %d\n", a >> b);
printf("a << b: %d\n", a << b);
return 0;
}
Output:
a & b: 1
a | b: 29
a ^ b: 28
~a: -26
a >> b: 0
a << b: 800

7. Condi onal Operator:

 The condi onal operator is also called a ternary operator because it requires three operands.
 This operator is used for decision making.
o Syntax:
Condi on? TRUE Part : FALSE Part;
If the condi on is TRUE the TRUE Part is performed, if the condi on is FALSE the FALSE Part is performed.

Q 23. Write short note Type Cas ng and Conversion in C.


 In a c programming language, the data conversion is performed in two different methods as follows...
1. Type Conversion
2. Type Cas ng
1. Type Conversion:
 The type conversion is the process of conver ng a data value from one data type to another data type
automa cally by the compiler.
 Some mes type conversion is also called implicit type conversion.
 The implicit type conversion is automa cally performed by the compiler.

Example:

int i = 10 ;

float x = 15.5 ;

char ch = 'A' ;

i = x ; =======> x value 15.5 is converted as 15 and assigned to variable i

x = i ; =======> Here i value 10 is converted as 10.000000 and assigned to variable x

i = ch ; =======> Here the ASCII value of A (65) is assigned to i


24

2. Type Cas ng:

 Typecas ng is also called an explicit type conversion.


 Compiler converts data from one data type to another data type implicitly.
 When compiler converts implicitly, there may be a data loss.
 In such a case, we convert the data from one data type to another data type using explicit type conversion.
 To perform this, we use the unary cast operator.
 To convert data from one type to another type we specify the target data type in parenthesis as a prefix to
the data value that has to be converted.
 The general syntax of typecas ng is as follows.

Example:
int totalMarks = 450, maxMarks = 600 ;
float average ;
average = (float) totalMarks / maxMarks * 100 ;
Explaina on:
In the above example code, both totalMarks and maxMarks are integer data values. When we perform
totalMarks / maxMarks the result is a float value, but the des na on (average) datatype is a float. So we use
type cas ng to convert totalMarks and maxMarks into float data type.
Q 24. What is Top-Down Approach?
 Top-Down Approach is an approach to design algorithms in which a bigger problem is broken down into smaller
parts.
 Thus, it uses the decomposi on approach.
 This approach is generally used by structured programming languages such as C, COBOL, FORTRAN.
 The implementa on of algorithm using top-down approach depends on the programming language and
pla orm.
 Top-down approach is generally used with documenta on of module and debugging code.

Drawbacks
 The top-down approach is that it may have redundancy since every part of the code is developed separately.
 Also, there is less interac on and communica on between the modules in this approach.
25

Q 25. What is Bo om-Up Approach?

 Bo om-Up Approach is one in which the smaller problems are solved, and then these solved problems are
integrated to find the solu on to a bigger problem.
 Therefore, it uses composi on approach.
 It requires a significant amount of communica on among different modules.
 It is generally used with object oriented programming paradigm such as C++, Java, and Python.
 Data encapsula on and data hiding is also implemented in this approach.
 The bo om-up approach is generally used in tes ng modules.

Q 26. Difference between Top-Down Approach and Bo om-Up Approach?

S.No. Top-Down Approach Bo om-Up Approach

1. In this approach, the problem is broken down into In this approach, the smaller problems are solved.
smaller parts.

2. It is generally used by structured programming It is generally used with object oriented


languages such as C, COBOL, FORTRAN, etc. programming paradigm such as C++, Java, Python,
etc.

3. It is generally used with documenta on of module It is generally used in tes ng modules.


and debugging code.

4. It does not require communica on between It requires rela vely more communica on between
modules. modules.

5. It contains redundant informa on. It does not contain redundant informa on.

6. Decomposi on approach is used here. Composi on approach is used here.

7. The implementa on depends on the programming Data encapsula on and data hiding is implemented
language and pla orm. in this approach.
26

Q 27. Explain Time Complexity of an Algorithm?

 Time complexity refers to the amount of me an algorithm takes to run as a func on of the size of its input.
 It is usually expressed in terms of "big O" nota on, which gives an upper bound on the growth rate of the
algorithm's running me.
 For example, an algorithm with a me complexity of O(n) means that its running me grows linearly with the
size of the input.

Q 28. Explain Space Complexity of an Algorithm?

 Space complexity, on the other hand, refers to the amount of memory an algorithm requires to run as a
func on of the size of its input.
 It is also expressed in terms of big O nota on.
 For example, an algorithm with a space complexity of O(n) means that it requires a linear amount of memory
to run as the size of the input grows.

It's important to consider both me and space complexity when analyzing algorithms, as they can have a
significant impact on the performance and efficiency of the algorithm. Generally, we want algorithms with lower me
and space complexi es, as they will be faster and require less memory to run.

Important Ques ons

1. Explain in detail about various func onal units of a computer.

2. What is an algorithm? Explain various characteris cs of an algorithm.

3. Write an algorithm to find largest of 3 numbers.

4. Define flowchart. Discuss in detail about various symbols used in a flowchart.

5. Draw a flowchart to find roots of quadra c equa on.

6. With an example program explain the structure of C program.

7. What is a C token? Explain various C tokens.

8. Describe in detail about various operators in C.

9. Write a C Program

i) Sum and average of 3 numbers


ii) Conversion of Fahrenheit to Celsius and vice versa
iii) Simple interest calcula on

10. Write a C Program

i) Finding compound interest


ii) Area of a triangle
iii) Distance travelled by an object

You might also like