You are on page 1of 23

L. J.

Institute of Engineering and Technology


Programming with JAVA Notes

Chapter 1

➢ Basic Model of Von-Neumann Architecture (block diagram of computer)

Input Unit
It is used to enter data in Computer for processing. Keyboard is the one of the most
commonly used input device. Other commonly used input devices are the mouse, scanner,
webcam, joystick.

Storage Unit(Memory)
It is used for storing the data in computer.
The various storage devices of a computer system are divided into two categories:
1. Primary Storage:
• Store current program or data
• Fast in operation
• Less storage capacity
• Costly
• Two types : RAM and ROM
• RAM – Random Access Memory. Its volatile in nature (Program gets lost when
power is turned off)
• ROM – Read Only Memory. Its non-volatile in nature. (Program stays
permanent)

2. Secondary Storage:
• Store program or data permanently
• Slow in operation
• More storage capacity
• Cheap
• Example : Hard disk, pen drive, CD, DVD

Central Processing Unit:


The CPU is like brain of the computer.

1|P a ge
L. J. Institute of Engineering and Technology
Programming with JAVA Notes

The Control Unit (CU) and Arithmetic Logical Unit (ALU) of the computer are together
knownas the Central Processing Unit (CPU).

Arithmetic Logical Unit (ALU):


• It performs all arithmetic and logical operations.
• Arithmetic operations such as addition, subtraction, multiplication, division.
• Logical operations >, <, =. The result of Logical operation is True or False.

Control Unit (CU):


• It controls all other units in the computer.
• It receives data from main memory
• Determines storage capacity

Output Unit:
The output unit of a computer provides the information and results of a operation to
outsideworld.
Example : Monitor, Printers ,Speakers, Projector.

➢ Various types of computer languages with their advantages and disadvantages


Computer language is used for communicating with computers. It is also commonly
known as programming language. Programming languages are categorized into
following:

1. Machine Level Language (Low Level Language)


Language written in 1’s and 0’s (binary language) which computer understands easily is
machine level language
Advantages:
• Translation free: It’s the only language computer understands. So once written can be
directly executed without any conversion
• High Speed: Execution is fast.
• Occupies Less Memory
Disadvantages:
• Time Consuming: Writing programs is time consuming process.

2. Assembly Level Language


Programs written using Mnemonic or operation code or opcode is assembly language,
System cannot understand this language directly so we require translator that convert
assemblylanguage to machine language. This translator is called assembler.

Example: 8086 Instruction Set(Microprocessor).

Advantages:
• Easy to understand: Compared to machine language. It’s easier for people to read, write
and understand programs written in assembly language.
• Efficiency: It requires less memory and other resources as compared to high level
language.
2|P a ge
L. J. Institute of Engineering and Technology
Programming with JAVA Notes

Disadvantages:
• Harder to learn: Different architecture have different instruction for writing assemblylanguage code.
• Time Consuming: Writing programs is time consuming process.

3. High Level Language


This language consists of set of English like words and symbols. The interpreters or compilers are used for
converting these programs into machine language. Higher level language like C, Java
Advantages:
i. Readability: High Level Languages use English like words. So easy to learn andunderstand.
ii. Coding is easy
Disadvantages:
• Poor control on hardware: It doesn’t not have any control at hardware level.More execution
time.

➢ Difference between Compiler and Interpreter.

Interpreter Compiler
Translates program into machine Scans the entire program and then translates into
languageline by line machine language
Execution time is slower. Execution time is faster.
No intermediate object code is Generates intermediate object code which further
generated,hence are memory efficient. requires linking, hence requires more memory.
Continues translating the program until It generates the error message only after scanning
thefirst error is met, in which case it stops. the whole program.
Programming language like Python, Programming language like C, C++ use
HTML use interpreters. compilers.

➢ Some important terms:


I. Operating System
It provides interface between the user and the hardware of the computer. It handles recognition of input
from the keyboard, keeping track of files and directories, sending output to display screen etc.

II. Compiler
It is a program which translates the program written in a high level language into computer readable
machine language (binary form)

III. Application software


A software for user specific needs like library management, school management etc.

IV. System Software


System Software refers to a computer program that manages and controls hardware components of a
computer system. Types of system software are operating system, compilers,interpreters etc. System
software performs functions like Process management, Memory management, File management, Device
Configuration, etc.

V. Assembler
Assembler in C Programming is defined as a program that converts Assembly languageinto
machine code.
L. J. Institute of Engineering and Technology
Programming with JAVA Notes

Flowchart and Algorithm

What is Flowchart and Algorithm? Differentiate between Algorithm and Flowchart.


Explain Various Symbol used in Flowchart.
Solution:

Flowchart:
A flowchart is graphical representation of sequence of any problem to be solved by computer
programming language. Following are the flowchart symbols:

Name Symbol Use in Flowchart


Oval Start/stop (Denotes the beginning or end of a program)
Denotes either an input operation or an output
Parallelogram
operation
Rectangle Denotes a process or calculation or expression

Denotes a decision or condition. The program should


Diamond
continue along one of two routes.

Arrow Denotes direction of logic flow in a program


Circle Flowchart Connector

Algorithm:

Algorithm is a finite sequence of well-defined steps for solving a problem in systemic


manner.

Differentiate between Algorithm and Flowchart.

Algorithm Flowchart
1 It is a finite sequence of well-defined 1 A flowchart is graphical
steps for solving a problem in systemic representation of sequence of any
manner. problem to be solved by computer
programming language.
2 It is a bit difficult to understand. 2 It is easy to understand.
3 It doesn't use any specific symbols. 3 It uses various kinds of symbols
which are interlinked with arrows.
L. J. Institute of Engineering and Technology
Programming with JAVA Notes
1. Write flowchart or algorithm to find area of a triangle.

Solution:
Algorithm:-

Step1: Start

Step 2: Input b,h

Step 3: calculate area=0.5*b*h

Step 4: print area

Step 5: Stop

Flowchart:
L. J. Institute of Engineering and Technology
Programming with JAVA Notes

2. Sketch flowchart and write an algorithm to convert seconds into hour, minute and second.
(Hint : 3700 seconds => 1 hr , 1 minute , 40 second)

Solution:
Algorithm:

Step1: Start

Step 2: Input second

Step 3: calculate h=second/3600

m= (second%3600)/60

s= (second%3600)%60

Step 4: print h ,m,s.

Step 5: Stop

Flowchart:
L. J. Institute of Engineering and Technology
Programming with JAVA Notes

3. Write an algorithm and flowchart to determine whether the given year is a leap year
or not.
Solution:
Algorithm:

Step1: Start

Step 2: Input year

Step 3: if ((year%400==0) || (year%4==0 && year%100!=0) )then goto next step otherwise goto
step 6.

Step 4: print “year is leap year.”

Step 5: stop

Step 6: print “year is not leap year.”

Step 7: stop

Flowchart:
L. J. Institute of Engineering and Technology
Programming with JAVA Notes
4. Write an algorithm for finding odd and even number from given two numbers.

Solution:

Algorithm:

Step1: Start

Step 2: Input a,b

Step 3: if a%2==0 then goto next step otherwise goto step 5.

Step 4: print “a is even.” then goto step 6.

Step 5: print “a is odd”

Step 6: if b%2==0 then goto next step otherwise goto step 8.

Step 7: print “b is even.” then goto step 9.

Step 8: print “b is odd”

Step 9: Stop

Flowchart:-
L. J. Institute of Engineering and Technology
Programming with JAVA Notes

5. Write an algorithm for finding largest Number out of three given Numbers. Draw the Flowchart
for finding largest Number out of three given Numbers.

Solution:

Algorithm:
Step-1: Input a, b, c
Step-2: if a > b then goto next step otherwise goto step 8
Step-3: if a > c then goto next step otherwise goto step 6
Step-4: print, “Max num is”, a
Step-5: stop
Step-6: print, “Max num is”, c
Step-7: stop
Step-8: if b > c then goto next step otherwise goto step 11
Step-9: print, “Max num is”, b
Step-10: stop
Step-11: print, “Max num is”, c
Step-12: stop

Flowchart:

Start

Input a, b, c

False True
Is (a>b)

True
Is (a>c) ?
Is (b>c)
False True False
Print ,”Max
Print ,”Max Print ,”Max num is”, a
Print ,”Max
num is”, b num is”, c
num is”, c

Stop
L. J. Institute of Engineering and Technology
Programming with JAVA Notes

6. Write an algorithm and draw the flowchart to find root of equation: ax2 + bx + c.

Solution:

Flowchart:

Algorithm:

Step 1: Start
Step 2: Read a,b,c
Step 3: d = b * b – 4 * a * c
Step 4: if d >= 0 then goto next step otherwise goto step 9
Step 5: x1 = ( - b + √d ) / 2 * a
Step 6: x2 = ( - b - √d ) / 2 * a
Step 7: print, x1, x2
Step 8: stop
Step 9: print, “Negative Roots possible.”
Step 10: stop
L. J. Institute of Engineering and Technology
Programming with JAVA Notes

7. Write an algorithm and draw the flowchart to find factorial of given number.

Solution:

Flowchart:

Algorithm:

Step 1: Start
Step 2: Read num
Step 3: count=1, fact=1
Step 4: Repeat steps from 4 to step 6 until count<= num
Step 5: fact = fact * count
Step 6: count = count + 1
Step 7: print, “Ans =”, fact
Step 8: stop
L. J. Institute of Engineering and Technology
Programming with JAVA Notes

8. Write an algorithm and draw the flowchart to find sum of first N element.
Solution:

Flowchart:

Algorithm:

Step 1: Start
Step 2: Read num
Step 3: count=1, sum=0
Step 4: Repeat steps from 4 to step 6 until count<= num
Step 5: sum = sum + count
Step 6: count = count + 1
Step 7: print, “Ans =”, sum
Step 8: stop
L. J. Institute of Engineering and Technology
Programming with JAVA Notes

9. Write an algorithm and draw the flowchart to find sum of 10 elements read from the user.

Solution:

Flowchart:

Algorithm:

Step 1: Start
Step 2: count = 1, sum = 0
Step 3: Repeat steps from 3 to step 6 until count<= 10
Step 4: Read num
Step 5: sum = sum + num
Step 6: count = count + 1
Step 7: print, “Ans =”, sum
Step 8: stop
L. J. Institute of Engineering and Technology
Programming with JAVA Notes

10. Write an algorithm and draw the flowchart to add first N odd numbers.

Solution:

Flowchart:

Algorithm:

Step 1: Start
Step 2: Read n
Step 3: sum = 0, i = 1
Step 4: Repeat steps from 4 to step 6 until i <= 2*n
Step 5: sum = sum + i
Step 6: i = i + 2
Step 7: print, “Ans =”, sum
Step 8: stop
L. J. Institute of Engineering and Technology
Programming with JAVA Notes

11. Write an algorithm and draw the flowchart to accept N numbers & count how many of them
where odd and also compute sum of all these odd numbers.

Solution:

Flowchart:

Algorithm:

Step 1: Start
Step 2: Read N
Step 3: count = 1, sum = 0, odd = 0
Step 4: Repeat steps from 4 to step 9 until count<= N
Step 5: Read num
Step 6: if num % 2 = 1 then goto next step otherwise goto step 9
Step 7: odd = odd + 1
Step 8: sum = sum + num
Step 9: count = count + 1
Step 10: print, “Odd Count =”, odd, “Sum=”, sum
Step 11: Stop.
L. J. Institute of Engineering and Technology
Programming with JAVA Notes

12. Write an algorithm and draw the flowchart to print sum of numbers between 1 to 100 which
are divisible by 3 and 5.

Solution:

Flowchart:

Algorithm:

Step 1: Start
Step 2: count = 1, sum = 0
Step 3: Repeat steps from 3 to step 6 until count<= 100
Step 4: if count % 3 = 0 && count % 5 = 0 then goto next step otherwise goto step 6
Step 5: sum = sum + count
Step 6: count = count + 1
Step 7: print, “Ans =”, sum
Step 8: stop
L. J. Institute of Engineering and Technology
Programming with JAVA Notes

13. Write an algorithm and draw the flowchart to solve following series 1! + 2! + 3! + …..+ n!

Solution:

Flowchart:

Algorithm:

Step 1: Start
Step 2: Read N
Step 3: count = 1, sum = 0, fact = 1
Step 4: Repeat steps from 4 to step 7 until count<= N
Step 5: fact = fact * count
Step 6: sum = sum + fact
Step 7: count = count + 1
Step 8: print, “Ans =”, sum
Step 9: stop
L. J. Institute of Engineering and Technology
Programming with JAVA Notes
14. Write an algorithm and draw the flowchart to print first N Fibonacci numbers.

Solution:
Flowchart:

Algorithm:
Step 1: Start
Step 2: Read N
Step 3: a = 0, b = 1
Step 4: if N < 0 then goto next step otherwise goto step 7
Step 5: print, “Invalid Number.”
Step 6: Stop
Step 7: print, b
Step 8: count = 2
Step 9: Repeat steps 9 to steps 14 until count < = N
Step 10: c = a + b
Step 11: print, c
Step 12: a = b
Step 13: b = c
Step 14: count = count + 1
Step 15: stop
L. J. Institute of Engineering and Technology
Programming with JAVA Notes

15. Write an algorithm and draw the flowchart to print reverse a given number.

Solution:

Flowchart:

Algorithm:

Step 1: Start
Step 2: Read num
Step 3: sum = 0
Step 4: Repeat step 4 to steps 7 until num > 0
Step 5: r = num % 10
Step 6: sum = sum * 10 + r
Step 7: num = num / 10
Step 8: print, “Ans =”, sum
Step 9: Stop
L. J. Institute of Engineering and Technology
Programming with JAVA Notes

16. Write an algorithm and draw the flowchart to print sum of individual digits of a given number.

Solution:

Flowchart:

Algorithm:

Step 1: Start
Step 2: Read num
Step 3: sum = 0
Step 4: Repeat step 4 to steps 7 until num > 0
Step 5: r = num % 10
Step 6: sum = sum + r
Step 7: num = num / 10
Step 8: print, “Ans =”, sum
Step 9: Stop
L. J. Institute of Engineering and Technology
Programming with JAVA Notes

17. Write an algorithm and draw the flowchart to check whether given number is Palindrome or
not.

Solution:
Flowchart:

Algorithm:
Step 1: Start
Step 2: Read num
Step 3: temp = num, sum = 0
Step 4: Repeat step 4 to steps 7 until num > 0
Step 5: r = num % 10
Step 6: sum = sum*10 + r
Step 7: num = num / 10
Step 8: if sum = temp then goto next step otherwise goto step 11
Step 9: print, “Palindrome Number.”
Step 10: Stop
Step 11: print, “Not a Palindrome Number.”
Step 12: Stop
L. J. Institute of Engineering and Technology
Programming with JAVA Notes
18. Write an algorithm and draw the flowchart to check whether given number is Armstrong
number or not.

Solution:

Flowchart:

Algorithm:
Step 1: Start
Step 2: Read num
Step 3: temp = num, sum = 0
Step 4: Repeat step 4 to steps 7 until num > 0
Step 5: r = num % 10
Step 6: sum = sum + r*r*r
Step 7: num = num / 10
Step 8: if sum = temp then goto next step otherwise goto step 11
Step 9: print, “Armstrong Number.”
Step 10: Stop
Step 11: print, “Not an Armstrong Number.”
Step12:Stop
L. J. Institute of Engineering and Technology
Programming with JAVA Notes

19. Write an algorithm and draw the flowchart to check whether given number is Prime number
or not.

Solution:

Flowchart:

Algorithm:
Step 1: Start
Step 2: Read num
Step 3: count = 2
Step 4: Repeat steps from 4 to steps 8 until count < num
Step 5: if num % count = 0 then goto next step otherwise goto step 8
Step 6: print, “Not a Prime Number.”
Step 7: Stop.
Step 8: count = count + 1
Step 9: print, “Prime Number.”
Step 10: Stop

*********************************************************************************

You might also like