0% found this document useful (0 votes)
8 views10 pages

Essential Algorithms for Number Operations

waohwdihauigisgiudgiuw ahusdgiuwg

Uploaded by

deathmangers
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)
8 views10 pages

Essential Algorithms for Number Operations

waohwdihauigisgiudgiuw ahusdgiuwg

Uploaded by

deathmangers
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

Ex: No: 1a

Date: Find the Largest of Two Numbers

Aim:

To develop a program to find the largest of two numbers using problem-solving techniques.

Problem Analysis Chart

Input Process Output

Two numbers (a, b) Compare a and b values Largest number

Algorithm

Step 1: Start

Step 2: Read two numbers a and b

Step 3: If a > b then

o Display a

Else

o Display b

Step 4: Stop

Flowchart:
Pseudocode:
START
READ a, b
IF a > b THEN
PRINT a
ELSE
PRINT b
ENDIF
STOP

Result:
The largest of two numbers is displayed successfully.

Ex: No: 1b
Check Whether a Number is Even or Odd
Aim:
To check whether a given number is even or odd using problem-solving techniques.
Problem Analysis Chart

Input Process Output

One number (n) Check remainder when


Even or Odd
divided by 2

Algorithm
Step 1: Start
Step 2: Read number n
Step 3: If n % 2 == 0 then
o Display "Even"
Else
o Display "Odd"
Step 4: Stop
Flowchart:

Pseudocode:
START
READ n
IF n % 2 == 0 THEN
PRINT "Even"
ELSE
PRINT "Odd"
ENDIF
STOP

Result:
The given number is identified as even or odd successfully.
Ex: No: 1c
Calculate the Factorial of a Number
Aim:
To calculate the factorial of a given number using problem-solving techniques.
Problem Analysis Chart

Input Process Output

One number(n) Multiply numbers from 1 to n Factorial value

Algorithm
Step 1: Start
Step 2: Read number n
Step 3: Initialize fact = 1
Step 4: Repeat steps 5–6 for i = 1 to n
Step 5: fact = fact * i
Step 6: Increment i
Step 7: Display fact
Step 8: Stop

Flowchart:
Pseudocode:
START
READ n
fact ← 1
FOR i = 1 TO n
fact ← fact * i
ENDFOR
PRINT fact
STOP

Result:
The factorial of the given number is calculated successfully.

Ex: No: 1d
Calculate the Sum of N Natural Numbers
Aim:
To develop an algorithm and pseudocode to calculate the sum of first N natural
numbers.
Problem Analysis Chart

Input Process Output

Add numbers from 1 to N


N Sum of N natural numbers

Algorithm
Step 1: Start
Step 2: Read number N
Step 3: Initialize sum = 0
Step 4: Repeat steps 5–6 for i = 1 to N
Step 5: sum = sum + i
Step 6: Increment i
Step 7: Display sum
Step 8: Stop
Flowchart

Pseudocode
START
READ N
sum ← 0
FOR i = 1 TO N
sum ← sum + i
ENDFOR
PRINT sum
STOP

Result:
The sum of N natural numbers is calculated successfully.
Ex: No: 1e
Reverse the Digits of a Number
Aim:
To reverse a given integer using algorithmic problem-solving techniques.
Problem Analysis Chart

Input Process Output

Extract digits using modulus


A number (n) and division, build reverse Reversed number
number

Algorithm
1. Start
2. Read number n
3. Initialize rev = 0
4. Repeat steps 5–6 while n > 0
5. digit = n % 10
6. rev = rev * 10 + digit
7. n = n / 10 (integer division)
8. Display rev
9. Stop
Flowchart:
Pseudocode:
START
READ n
rev ← 0
WHILE n > 0
digit ← n % 10
rev ← rev * 10 + digit
n ← n // 10
ENDWHILE
PRINT rev
STOP

Result:
The given number is reversed successfully.

Ex: No: 1f
Check Whether a Number is Prime
Aim:
To determine whether a given number is prime or not using problem-solving
techniques.
Problem Analysis Chart

Step Description

Input A number n

• Check if n is divisible by any number from 2 to n/2.


Process • If divisible, then it is not prime.
• Otherwise, it is prime.

Output Prime or Not Prime

Algorithm
Step 1: Start
Step 2: Read number n
Step 3: If n <= 1, then display "Not Prime" and stop
Step 4: Set flag = 0
Step 5: Repeat for i = 2 to n/2
o If n % i == 0 then
▪ Set flag = 1
▪ Break loop
Step 6: If flag == 0 then display "Prime" else display "Not Prime"
Step 7: Stop
Flowchart
Pseudocode
START
READ n
IF n <= 1 THEN
PRINT "Not Prime"
STOP
ENDIF

flag ← 0
FOR i = 2 TO n/2
IF n % i == 0 THEN
flag ← 1
BREAK
ENDIF
ENDFOR

IF flag == 0 THEN
PRINT "Prime"
ELSE
PRINT "Not Prime"
ENDIF
STOP

Result:
The program successfully checks whether the given number is prime or not.

You might also like